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
相关推荐
Ulyanov1 分钟前
基于Impress.js的3D概念地图设计与实现
开发语言·前端·javascript·3d·ecmascript
SunnyDays10112 分钟前
如何使用 Java 自动调整 Excel 行高和列宽
java·自动调整行高和列宽·自适应行高和列宽
A南方故人6 分钟前
一个用于实时检测 web 应用更新的 JavaScript 库
开发语言·前端·javascript
虎头金猫7 分钟前
内网导航站 “出圈”!用 cpolar 解锁 Dashy 远程访问新玩法
java·c++·python·程序人生·职场和发展·php·程序员创富
JosieBook7 分钟前
【WinForm】使用C# WinForm实现带有托盘图标功能的应用程序
开发语言·c#
2301_7903009610 分钟前
C++与量子计算模拟
开发语言·c++·算法
康小庄10 分钟前
SpringBoot 拦截器 (Interceptor) 与切面 (AOP):示例、作用、及适用场景
java·数据库·spring boot·后端·mysql·spring·spring cloud
不会c+13 分钟前
Maven私服的搭建与使用
java·maven
青灯照颦微16 分钟前
【R】三种方式安装R包
开发语言·r语言
weixin_4365250722 分钟前
若依多租户版: RuoYi-Vue-Plus
java