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
相关推荐
Mr YiRan10 分钟前
C++语言学习之面向对象
java·c++·学习
dc_001221 分钟前
“mysqld --initialize --console ”执行不成功情况总结和解决措施
java
前端达人26 分钟前
被JavaScript忽视的Web Animations API:为什么说它是前端动画的真正未来?
开发语言·前端·javascript·ecmascript
米羊1211 小时前
风险评估文档记录
开发语言·网络·php
摘星编程1 小时前
解锁Agent智能体的未来:五大实战策略彻底革新人机协作模式
java·开发语言
百块富翁1 小时前
可管控、不重复TraceId解决方案
java·分布式·系统架构
金銀銅鐵1 小时前
浅解 Junit 4 第二篇: Runner 和 ParentRunner
java·junit·单元测试
Aerkui1 小时前
Go 泛型(Generics)详解
开发语言·后端·golang
_codemonster1 小时前
JavaWeb开发系列(七)表单开发
java