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
相关推荐
cmpxr_20 分钟前
【C】局部变量和全局变量及同名情况
c语言·开发语言
小碗羊肉1 小时前
【从零开始学Java | 第三十一篇下】Stream流
java·开发语言
❀͜͡傀儡师1 小时前
Spring AI Alibaba vs. AgentScope:两个阿里AI框架,如何选择?
java·人工智能·spring
aq55356002 小时前
Laravel10.x重磅升级,新特性一览
android·java·开发语言
一 乐2 小时前
酒店预订|基于springboot + vue酒店预订系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·酒店预订系统
报错小能手2 小时前
ios开发方向——swift错误处理:do/try/catch、Result、throws
开发语言·学习·ios·swift
zfan5202 小时前
python对Excel数据处理(1)
python·excel·pandas
Moe4882 小时前
Spring AI Advisors:从链式增强到递归顾问
java·后端
敖正炀2 小时前
ReentrantReadWriteLock、ReentrantLock、synchronized 对比
java
网域小星球2 小时前
C 语言从 0 入门(十七)|结构体指针 + 动态内存 + 文件综合实战
c语言·开发语言·文件操作·结构体指针·动态内存·综合项目