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
相关推荐
计算机毕设定制辅导-无忧学长1 小时前
InfluxDB 权限管理与安全加固(一)
java·struts·安全
老华带你飞1 小时前
生产管理ERP系统|物联及生产管理ERP系统|基于SprinBoot+vue的制造装备物联及生产管理ERP系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·论文·制造·毕设·生产管理erp系统
一勺-_-1 小时前
全栈:如何判断自己应该下载哪个版本的Tomcat
java·tomcat
现在没有牛仔了1 小时前
举例说明什么是Redis缓存击穿,以及如何解决。
java·redis·后端
青云交1 小时前
Java 大视界 -- 基于 Java 的大数据分布式计算在气象灾害数值模拟与预警中的应用(388)
java·大数据·flink·分布式计算·预警系统·数值模拟·气象灾害
CHEN5_022 小时前
Java基础知识总结
java·开发语言
Kiri霧2 小时前
Kotlin反射
java·开发语言·kotlin
苹果醋32 小时前
Deep Dive React 4 How does React State actually work
java·运维·spring boot·mysql·nginx
嫩萝卜头儿2 小时前
深入理解 Java AWT Container:原理、实战与性能优化
java·python·性能优化
爱吃芒果的蘑菇3 小时前
使用pybind11封装C++API
开发语言·c++·python