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 小时前
保证金系统入门到实战
java·后端
Nyarlathotep01134 小时前
Java内存模型
java
暮色妖娆丶9 小时前
不过是吃了几年互联网红利罢了,我高估了自己
java·后端·面试
NE_STOP9 小时前
MyBatis-参数处理与查询结果映射
java
狂奔小菜鸡10 小时前
Day40 | Java中的ReadWriteLock读写锁
java·后端·java ee
SimonKing11 小时前
JetBrains 用户狂喜!这个 AI 插件让 IDE 原地进化成「智能编码助手」
java·后端·程序员
狂奔小菜鸡11 小时前
Day39 | Java中更灵活的锁ReentrantLock
java·后端·java ee
NE_STOP1 天前
MyBatis-配置文件解读及MyBatis为何不用编写Mapper接口的实现类
java
后端AI实验室1 天前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai