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
相关推荐
蓝博AI30 分钟前
2027基于yolov8飞机蒙皮缺陷检测,支持图像,视频和摄像实时检测,python源码
开发语言·python·yolo
西柚小萌新1 小时前
【人工智能:LangGraph】--3.控制流(Edge)
java·服务器·数据库
金銀銅鐵1 小时前
带您看懂字节码指令(八):数组里有什么
java·jvm·后端
郝学胜-神的一滴3 小时前
游戏设计、原型与开发 26:四种循环
开发语言·数据结构·游戏·游戏引擎·游戏策划
r_oo_ki_e_3 小时前
牛牛与切割机
java·算法
Rain的Java大神之路3 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
泡海椒4 小时前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑
java·开发语言·pdf
2601_962885724 小时前
如何用 Python 计算终极振荡器(Ultimate Oscillator, UOS)?
开发语言·python
wuminyu4 小时前
OpenJDK学习笔记:Native事件队列与Java回调背压
java·linux·c语言·jvm·c++
源码_空间4 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端