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
相关推荐
散峰而望4 分钟前
C++入门(一)(算法竞赛)
c语言·开发语言·c++·编辑器·github
零雲4 分钟前
java面试:有了解过RocketMq架构么?详细讲解一下
java·面试·java-rocketmq
C_Liu_10 分钟前
13.C++:继承
开发语言·c++
张人玉12 分钟前
c#串口读写威盟士五插针
开发语言·c#·通讯
Deamon Tree16 分钟前
HBase 核心架构和增删改查
java·hbase
路长冬19 分钟前
matlab与数字信号处理的不定期更新
开发语言·matlab·信号处理
卡卡酷卡BUG36 分钟前
Java 后端面试干货:四大核心模块高频考点深度解析
java·开发语言·面试
Yolo566Q39 分钟前
OpenLCA生命周期评估模型构建与分析
java·开发语言·人工智能
安娜的信息安全说1 小时前
深入浅出 MQTT:轻量级消息协议在物联网中的应用与实践
开发语言·物联网·mqtt
lang201509281 小时前
Spring Boot日志配置完全指南
java·spring boot·单元测试