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
相关推荐
阿猿收手吧!9 分钟前
【C++】volatile与线程安全:核心区别解析
java·c++·安全
Hui Baby23 分钟前
Java SPI 与 Spring SPI
java·python·spring
我能坚持多久25 分钟前
D22—C语言预处理详解:从宏定义到条件编译
c语言·开发语言
小猪咪piggy27 分钟前
【Python】(3) 函数
开发语言·python
青岑CTF36 分钟前
攻防世界-Php_rce-胎教版wp
开发语言·安全·web安全·网络安全·php
摇滚侠37 分钟前
Maven 教程,Maven 安装及使用,5 小时上手 Maven 又快又稳
java·maven
倔强菜鸟39 分钟前
2026.2.2--Jenkins的基本使用
java·运维·jenkins
hai742542 分钟前
在 Eclipse 的 JSP 项目中引入 MySQL 驱动
java·mysql·eclipse
初次见面我叫泰隆1 小时前
Qt——4、Qt窗口
开发语言·qt·客户端开发
瑞雪兆丰年兮1 小时前
[从0开始学Java|第十一天]学生管理系统
java·开发语言