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
相关推荐
清水白石00821 小时前
突破并行瓶颈:Python 多进程开销全解析与 IPC 优化实战
开发语言·网络·python
追随者永远是胜利者21 小时前
(LeetCode-Hot100)53. 最大子数组和
java·算法·leetcode·职场和发展·go
百锦再1 天前
Java之Volatile 关键字全方位解析:从底层原理到最佳实践
java·开发语言·spring boot·struts·kafka·tomcat·maven
张万森爱喝可乐1 天前
Java高并发实战
java
daad7771 天前
rcu 内核线程
java·开发语言
xzjiang_3651 天前
检查是否安装了MinGW 编译器
开发语言·qt·visual studio code
百锦再1 天前
Java JUC并发编程全面解析:从原理到实战
java·开发语言·spring boot·struts·kafka·tomcat·maven
清水白石0081 天前
突破性能瓶颈:深度解析 Numba 如何让 Python 飙到 C 语言的速度
开发语言·python
Eternity∞1 天前
Linux系统下,C语言基础
linux·c语言·开发语言