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
相关推荐
覆东流3 分钟前
Java开发环境搭建
java·开发语言·后端
阿洛学长6 分钟前
VMware安装虚拟机教程(超详细)
java·linux·开发语言
rit84324996 分钟前
链路预测(Link Prediction)MATLAB 实现
开发语言·matlab
jiayong237 分钟前
01 检查 Python 版本与环境
开发语言·python
coder Ethan10 分钟前
Spring AI 入门:(3)快速搭建一个简单的问答助手
java·人工智能·spring
屋外雨大,惊蛰出没12 分钟前
starter的创建与引用
java·stater
小同志0014 分钟前
Spring Boot ⽇志概述(简单了解)
java·java-ee·日志
小马爱打代码18 分钟前
SpringBoot + 延迟消息 + 时间轮:订单超时、优惠券过期等场景的高效实现方案
java·spring boot·后端
就叫_这个吧22 分钟前
Java普通类、抽象类、接口的应用和区别
java·开发语言
梅孔立25 分钟前
解决Nginx缓存不写入响应体问题:浏览器强制不缓存配置教程
java·开发语言·nginx·spring