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
相关推荐
xiaoye37088 小时前
java接口文档工具 swagger2和swagger3对比
java·服务器·前端
三维频道8 小时前
工业级三维扫描实测:汽车灯具复杂结构件的全尺寸 3D 测量方案分析
java·人工智能·python·数码相机·3d·汽车·汽车轻量化制造
tongyiixiaohuang8 小时前
基于轻易云的数据集成,实现企业系统间灵活对接
java·前端·数据库
码农飞哥8 小时前
从Java后端到AI应用开发,我这两年做了什么
java·开发语言·人工智能
森林猿8 小时前
IDEA-控制台乱码
java·ide·intellij-idea
A-Jie-Y8 小时前
JAVA设计模式-工厂方法模式
java·设计模式
胡童嘉8 小时前
C语言考研《谭浩强C语言》教材第一章理论+实践汇总
c语言·开发语言·考研
千云8 小时前
问题排查报告:一次因元空间溢出导致的CPU飙升与接口超时
java·后端
初心未改HD8 小时前
Go语言Slice切片底层原理深度解析
开发语言·golang
Full Stack Developme8 小时前
MyBatis-Plus 注解教程
java·spring·mybatis