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
相关推荐
知守观11 分钟前
从 4 套 PDF 库的混战,聊聊老项目的依赖管理与“静默失败”
java·后端·设计模式
深盾科技_Virbox13 分钟前
换机或重装后软件许可怎么恢复:先核对哪四项
java·服务器·数据库
Zhuo1Joy13 分钟前
中间件挂了怎么办:兜底的边界与取舍
java
用户0723670666014 分钟前
基于 JavaAgent 的无侵入 Mock:让联调和链路测试不再等下游
java·后端
小蒜学长15 分钟前
基于SpingBoot+Vue的定向越野赛事管理系统的设计与开发(代码+数据库+LW)
java·前端·spring boot·后端·前后端分离·定向越野赛事
海岳云舟15 分钟前
spring自带线程池使用不当导致的死锁问题
java·后端
yuzhiboyouye19 分钟前
Optional和Stream经典丰富的用法demo,以及相关的详细介绍
java
SuperHeroWu722 分钟前
ArkGenie 的技术原理:从一段 SSE 到一个可交互 Surface
开发语言·php·交互
小蒜学长32 分钟前
基于过程性评价的ai助学平台(代码+数据库+LW)
java·spring boot·后端·微信小程序·过程性评价
洁洁!37 分钟前
手搓 C++ 线程池详细讲解以及如何面试应答
java·c++·面试