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
相关推荐
杨凯凡7 分钟前
【012】图与最短路径:了解即可
java·数据结构
比特森林探险记18 分钟前
【无标题】
java·前端
椰猫子32 分钟前
Javaweb(Filter、Listener、AJAX、JSON)
java·开发语言
盛世宏博北京1 小时前
以太网温湿度传感器运维技巧,提升设备稳定性与使用寿命
开发语言·php·以太网温湿度传感器
朝新_1 小时前
【Spring AI 】核心知识体系梳理:从入门到实战
java·人工智能·spring
一 乐1 小时前
旅游|基于springboot + vue旅游信息推荐系统(源码+数据库+文档)
java·vue.js·spring boot·论文·旅游·毕设·旅游信息推荐系统
代码改善世界1 小时前
【MATLAB初阶】矩阵操作(一)
开发语言·matlab·矩阵
覆东流1 小时前
第1天:Python环境搭建 & 第一个程序
开发语言·后端·python
我命由我123452 小时前
Android 开发中,关于 Gradle 的 distributionUrl 的一些问题
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
橙露2 小时前
SpringBoot 全局异常处理:优雅封装统一返回格式
java·spring boot·后端