excel vba将选中区域向下复制指定次数

excel vba将选中区域向下复制指定次数

1 需求

将选中区域向下复制4次

2 选中区域 A2:F6

执行VBA,会弹出对话框,输入数字4 (表示向下复制4次)

3 复制完成


VBA code

bash 复制代码
Sub CopySelection()
Dim numCopies As Integer
Dim selectedRange As Range
Dim i As Integer

' Prompt the user to enter a number
numCopies = Application.InputBox("Enter the number of times to copy the selection:", Type:=1)
' Check if a valid number is entered
If numCopies <= 0 Then
MsgBox "Please enter a valid positive number.", vbExclamation
Exit Sub
End If

' Store the selected range
Set selectedRange = Selection
' Copy the selection downwards the specified number of times
For i = 1 To numCopies
selectedRange.Offset(i * selectedRange.Rows.Count, 0).Value = selectedRange.Value
Next i
MsgBox "Selected range copied " & numCopies & " times.", vbInformation
End Sub
相关推荐
组合缺一4 分钟前
Solon AI 开发学习16 - generate - 生成模型(图、音、视)
java·人工智能·学习·ai·llm·solon
谷哥的小弟4 分钟前
Spring Framework源码解析——AnnotationAwareOrderComparator
java·后端·spring·源码
八月瓜科技7 分钟前
八月瓜科技参与“数据要素驱动产业升级”活动,分享【数据赋能科技创新全链条】
java·大数据·人工智能·科技·机器人·程序员创富
谷哥的小弟7 分钟前
Spring Framework源码解析——StringUtils
java·后端·spring·源码
G_whang8 分钟前
win10环境下jdk17下载安装及环境配置
java
非情剑12 分钟前
Java-Executor线程池配置-案例2
android·java·开发语言
小张快跑。14 分钟前
【Java企业级开发】(十)SpringBoot框架+项目实践
java·数据库·spring boot
夏小花花15 分钟前
<editor> 组件设置样式不生效问题
java·前端·vue.js·xss
weixin_3077791316 分钟前
Jenkins Ioncions API 插件:现代化图标库在持续集成中的应用
java·运维·开发语言·前端·jenkins
AnAnCode17 分钟前
【时间轮算法】时间轮算法的详细讲解,从基本原理到 Java 中的具体实现
java·开发语言·算法·时间轮算法