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
相关推荐
毕设源码-邱学长几秒前
【开题答辩全过程】以 南工计算机等级网站为例,包含答辩的问题和答案
java
NE_STOP1 分钟前
spring boot3--自动配置与手动配置
java
froginwe111 分钟前
JSON 语法详解
开发语言
小北方城市网3 分钟前
Spring Cloud Gateway 生产级微内核架构设计与可插拔过滤器开发
java·大数据·linux·运维·spring boot·redis·分布式
XYCMS8 分钟前
PHP 外贸网站标题怎么用英文分割生成带杠号“-”的短网址
开发语言·php
csdn_aspnet9 分钟前
Java常用算法深度解析:从集合框架到并发编程
java·算法
云游云记9 分钟前
php设计模式总结
开发语言·设计模式·php
sheji341610 分钟前
【开题答辩全过程】以 基于j2ee的问卷调查系统为例,包含答辩的问题和答案
java·java-ee
计算机学姐10 分钟前
基于SpringBoot的自习室座位预定系统【预约选座+日期时间段+协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·spring·信息可视化·tomcat
Thomas_YXQ14 分钟前
Unity3D中提升AssetBundle加载速度的详细指南
java·spring boot·spring·unity·性能优化·游戏引擎·游戏开发