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
相关推荐
Hazenix6 小时前
Go 指南:一篇文章速通 Golang
开发语言·后端·golang
灯澜忆梦6 小时前
GO_复合类型---指针
开发语言·后端·golang
Ulyanov6 小时前
雷达导引头Python仿真框架:GPU加速、6-DOF模型与半实物仿真接口
开发语言·python·雷达信号处理·雷达导引头
wuqingshun3141597 小时前
MYSQL的乐观锁和悲观锁是什么?
java
唐青枫7 小时前
Java SLF4J 实战指南:从日志门面到 Logback、MDC 和链路追踪
java
名字还没想好☜8 小时前
Go slice 的 append 陷阱:共享底层数组导致的数据串改
开发语言·后端·golang·go·slice
bitbrowser8 小时前
Claude 账号频繁被封?转向国内可直接使用的 AI 工具
开发语言·php
jvmind_dev8 小时前
Java GC 实战指南(番外篇):被忽视的隐形杀手 —— Class Unloading 如何拖垮 GC
java·后端
An_s8 小时前
机器学习python之识别图中物品信息
java·linux·开发语言
小雪_Snow8 小时前
JavaScript 中的三种常用事件
开发语言·前端·javascript