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
相关推荐
爱笑的眼睛1118 分钟前
深入解析Matplotlib Axes API:构建复杂可视化架构的核心
java·人工智能·python·ai
刺客xs29 分钟前
Qt------信号槽,属性,对象树
开发语言·qt·命令模式
2501_9216494931 分钟前
免费获取股票历史行情与分时K线数据 API
开发语言·后端·python·金融·数据分析
乐观甜甜圈32 分钟前
JDK8 中线程实现方法与底层逻辑详解
java
尤物程序猿36 分钟前
Java如何不建表完成各种复杂的映射关系(鉴权概念、区域概念、通用概念)
java·开发语言
cike_y1 小时前
JSP内置对象及作用域&双亲委派机制
java·前端·网络安全·jsp·安全开发
也许是_1 小时前
大模型应用技术之 Spring AI 2.0 变更说明
java·人工智能·spring
xunyan62341 小时前
面向对象(下)-内部类的分类
java·学习
Insight.1 小时前
背包问题——01背包、完全背包、多重背包、分组背包(Python)
开发语言·python
巴拉巴拉~~1 小时前
KMP 算法通用进度条组件:KmpProgressWidget 多维度 + 匹配进度联动 + 平滑动画
java·服务器·前端