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
相关推荐
乐之者v1 天前
AI编程-- codex并行开发需求
java
weixin_520649871 天前
C#线程底层原理知识
java·开发语言
agilearchitect1 天前
Matlab导入Excel表格教程:从基础到进阶全攻略
数据结构·其他·matlab·excel
AC赳赳老秦1 天前
OpenClaw与Excel联动:批量读取/写入数据,生成可视化报表
开发语言·python·excel·产品经理·策略模式·deepseek·openclaw
code_whiter1 天前
C++9(vector)
开发语言·c++
xieliyu.1 天前
Java手搓数据结构:从零模拟实现单向无头非循环链表
java·数据结构·学习·链表
覆东流1 天前
第5天:Python字符串操作进阶
开发语言·后端·python
吴梓穆1 天前
UE5 C++ 使C++创建动画蓝图
开发语言·c++·ue5
冰暮流星1 天前
javascript之表单事件1
开发语言·前端·javascript