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
相关推荐
xinlianluohan几秒前
excel筛选背景颜色的值&拼接单元格&判断单元格是否含有中文
excel
liwulin05065 分钟前
【JSON】使用com.fasterxml.jackson解析json字符串
java·数据库·json
sonrisa_16 分钟前
Python同一类不同方法中变量值的传递
开发语言·windows·python
what丶k24 分钟前
深度解析:以Kafka为例,消息队列消费幂等性的实现方案与生产实践
java·数据结构·kafka
星火开发设计28 分钟前
C++ 输入输出流:cin 与 cout 的基础用法
java·开发语言·c++·学习·算法·编程·知识
毕设源码-邱学长37 分钟前
【开题答辩全过程】以 基于Springboot的酒店住宿信息管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
曹牧1 小时前
Java:强类型转换
开发语言·python
wuguan_1 小时前
C#之线程
开发语言·c#
LXS_3571 小时前
STL - 函数对象
开发语言·c++·算法
木千1 小时前
Qt5.15.2安装MSVC2019编译器
开发语言·qt