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
相关推荐
wWYy.4 分钟前
STL:list
开发语言·c++
TON_G-T14 分钟前
day.js和 Moment.js
开发语言·javascript·ecmascript
发际线还在16 分钟前
互联网大厂Java三轮面试全流程实战问答与解析
java·数据库·分布式·面试·并发·系统设计·大厂
飞Link18 分钟前
具身智能核心架构之 Python 行为树 (py_trees) 深度剖析与实战
开发语言·人工智能·python·架构
_周游29 分钟前
Kaptcha—Google验证码工具
java·intellij-idea·jquery
我真会写代码1 小时前
深入理解JVM GC:触发机制、OOM关联及核心垃圾回收算法
java·jvm·架构
本喵是FW1 小时前
C语言手记1
java·c语言·算法
码云数智-园园1 小时前
2026 年前端开发趋势:AI 赋能、组件化与跨端一体化的深度融合
开发语言
weixin_423533991 小时前
windows11安装飞桨paddlepaddle,python3.13
开发语言
2501_924952691 小时前
嵌入式C++电源管理
开发语言·c++·算法