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
相关推荐
xyq20244 分钟前
Java 数组
开发语言
雨辰AI12 分钟前
人大金仓 V9 生产级专用监控大盘(含 120 + 指标 + 告警规则 + 一键导入)
java·开发语言·数据库·mysql·政务
时寒的笔记14 分钟前
day13~14核心案例某采招网
开发语言·javascript·ecmascript
aloha_78920 分钟前
软考信息系统项目管理师错误归纳总结
java·学习
vortex529 分钟前
Linux PAM 配置详解:从原理到实战,彻底阻断非授权提权
java·linux·服务器
invicinble29 分钟前
spring提供的其他机制
java·后端·spring
阿昌喜欢吃黄桃33 分钟前
如果线程池中线程异常后:销毁还是复用?
java·线程·线程池·多线程·juc
奋斗的小乌龟38 分钟前
动态创建Agent01
java·笔记
彦为君1 小时前
Java文件处理效率库Commons-IO(速览)
java·开发语言·mfc
她的男孩1 小时前
后台权限不只是菜单隐藏:Forge Admin 的 RBAC 权限链路拆解
java·后端·架构