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
相关推荐
C雨后彩虹几秒前
ConcurrentHashMap 源码逐行拆解:put/get 方法的并发安全执行流程
java·算法·哈希算法·集合·hashmap
wuguan_1 分钟前
C#:try和catch(保护程序不崩溃)
开发语言·c#·try catch
无限进步_3 分钟前
C++ STL list容器深度解析与模拟实现
开发语言·数据结构·c++·windows·git·list·visual studio
侠客行03174 分钟前
Mybatis入门到精通 二
java·mybatis·源码阅读
2501_909800814 分钟前
Java IO框架
java·学习·io框架
黑贝是条狗5 分钟前
Delphi TCP通信帧设计,类型Websocket通信帧
开发语言·c#
黎雁·泠崖6 分钟前
C 语言动态内存管理高阶:柔性数组特性 + 程序内存区域划分全解
c语言·开发语言·柔性数组
趣知岛1 小时前
初识Java
java·开发语言
步菲3 小时前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
毕设源码-朱学姐3 小时前
【开题答辩全过程】以 基于SpringBoot的中医理疗就诊系统为例,包含答辩的问题和答案
java·spring boot·后端