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
相关推荐
Polaris北27 分钟前
第二十七天打卡
开发语言·c++·算法
BD_Marathon1 小时前
IDEA创建多级包时显示在同一行怎么办
java·ide·intellij-idea
亓才孓1 小时前
【Exception】CONDITIONS EVALUATION REPORT条件评估报告
java·开发语言·mybatis
硅基动力AI1 小时前
如何判断一个关键词值不值得做?
java·前端·数据库
学无止境_永不停歇1 小时前
十一、C++11列表初始化、右值引用和移动语义
开发语言·c++
阿里嘎多学长2 小时前
2026-02-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
mjhcsp2 小时前
C++ 背包DP解析
开发语言·c++
重生之后端学习2 小时前
78. 子集
java·数据结构·算法·职场和发展·深度优先
尘缘浮梦2 小时前
协程asyncio入门案例 2
开发语言·python
juleskk2 小时前
2.15 复试训练
开发语言·c++·算法