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
相关推荐
geovindu5 分钟前
python:DecisionTree
开发语言·后端·python·算法
Cosolar10 分钟前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
geovindu2 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
IT空门:门主2 小时前
LangChain4j 1.24.0 深度解读:新特性、版本对比与学习路线
java·学习
用户3721574261354 小时前
Java 批量操作 Excel 超链接:添加、读取、修改和删除
java
用户3721574261355 小时前
Java 将 Excel 图表导出为图片:指定图表、批量导出与图表工作表
java
炸膛坦客5 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
这个昵称不容易5 小时前
交流论坛网站设计与实现:SpringBoot 论坛 130 个接口,附论文与运行截图
java·springboot·毕设
夜不会漫长6 小时前
C++:C++11特性
开发语言·c++·算法