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
相关推荐
阿拉金alakin几秒前
深入理解 Java 线程池:核心参数、工作流程与常用创建方式
java·开发语言·java-ee
小江的记录本7 分钟前
【网络安全】《网络安全三大加密算法结构化知识体系》
java·前端·后端·python·安全·spring·web安全
slandarer13 分钟前
MATLAB | R2026a 更新了哪些有趣的新东西?
开发语言·数据库·matlab
希望永不加班17 分钟前
SpringBoot 中 AOP 实现多数据源切换
java·数据库·spring boot·后端·spring
早起傻一天~G21 分钟前
vue2+element-UI上传图片封装
开发语言·javascript·ui
广师大-Wzx23 分钟前
JavaWeb:前端部分
java·前端·javascript·css·vue.js·前端框架·html
生万千欢喜心24 分钟前
Linux 安装金蝶天燕中间件 AAS-V9.0.zip
java·linux
耿雨飞26 分钟前
Python 后端开发技术博客专栏 | 第 03 篇 面向对象编程进阶 -- 从 SOLID 原则到 Python 特色 OOP
开发语言·python·面向对象·oop
charlie11451419127 分钟前
嵌入式现代C++工程实践——第14篇:第二次重构 —— 模板登场,编译时绑定端口和引脚
开发语言·c++·stm32·安全·重构
源码站~30 分钟前
基于python的校园代跑(跑腿)系统
开发语言·python