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
相关推荐
我还记得那天2 分钟前
函数的递归调用
c语言·开发语言·visualstudio
zhangfeng11333 分钟前
ThinkPHP5 事件系统的标准最佳实践 事件系统的完整设计逻辑tags.php tags.php(事件地图)
android·开发语言·php
xyq20246 分钟前
HTML 标签简写及全称
开发语言
tongluowan0077 分钟前
数据结构 Bitmap(位图)示例 - 用户签到系统
开发语言·数据结构·bitmap·用户签到系统
就叫_这个吧7 分钟前
Java线程池应用的四种方式+线程池底层实现原理
java·开发语言
Dicky-_-zhang9 分钟前
Java并发编程实战:线程池与并发工具类
java·jvm
Rust研习社10 分钟前
Rust 官方拟定 LLM 政策,防止 LLM 污染开源社区?
开发语言·后端·ai·rust·开源
devilnumber10 分钟前
JDK6→JDK7→JDK8 重点技术更新(精简背诵版)
java
云烟成雨TD11 分钟前
Spring AI Alibaba 1.x 系列【61】Graph 持久化执行
java·人工智能·spring
muqsen11 分钟前
Java 分布式相关面试题总结
java·开发语言·分布式