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
相关推荐
csbysj202010 小时前
JSP 文件上传详解
开发语言
郝学胜-神的一滴10 小时前
FastAPI:Python 高性能 Web 框架的优雅之选
开发语言·前端·数据结构·python·算法·fastapi
柒.梧.10 小时前
Java位运算详解:原理、用法及实战场景(面试重点)
开发语言·数据库·python
游乐码10 小时前
c#万物之父装箱拆箱
开发语言·c#
CDwenhuohuo10 小时前
var面试题
开发语言·javascript·ecmascript
PD我是你的真爱粉11 小时前
深入理解 Event Loop:JavaScript 的“心脏起搏器”
开发语言·javascript·ecmascript
牢七11 小时前
反序列化重点模块 private Object readOrdinaryObject(boolean unshared)废案与反思
java·服务器·前端
GIS程序猿11 小时前
批量出图工具,如何使用C#实现动态文本
开发语言·arcgis·c#·arcgis插件·gis二次开发
量子物理学11 小时前
三、C#高级进阶语法——特性(Attribute)
java·算法·c#
刘恒12345678911 小时前
Windows 电脑文件夹手动分类指南
java·windows·python·电脑·php