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
相关推荐
阿弱15 分钟前
graph-core 的边与命令模式设计
java·后端·agent
互联网中的一颗神经元24 分钟前
01. Go 内存管理全景架构
java·jvm·golang
萧瑟余晖25 分钟前
Java深入解析篇三十四之分布式事务
java·开发语言·分布式
CAE虚拟与现实25 分钟前
源码注解 /class 注解 / 运行时注解三者生命周期
java·开发语言
有脚就行28 分钟前
第24篇-Go-gRPC推理服务-高性能跨语言通信
开发语言·人工智能·后端·golang
码匠许师傅28 分钟前
【C++ 面试真题】聊聊 C++ 的关联容器
开发语言·c++·面试
如意机反光镜裸30 分钟前
如何合并局域网共享文件夹中的多个 Excel 工作簿?(免 WPS 会员解决方案)
服务器·excel·wps
互联网中的一颗神经元32 分钟前
02. 核心概念与术语表
java·jvm·spring
丰锋ff34 分钟前
7.2Qt中Json的操作
开发语言·qt·json
January120737 分钟前
Cassandra CQL 基本命令详解
java