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
相关推荐
蜡台20 小时前
JavaScript Object Function ERROR
开发语言·javascript·ecmascript·error
鬼先生_sir20 小时前
Dubbo:从入门到精通
java·dubbo·springcloud
Shadow(⊙o⊙)20 小时前
C语言学习中需要的额外函数
c语言·开发语言·学习
还是大剑师兰特20 小时前
pinia-plugin-persistedstate详解与Vue3使用示例
开发语言·javascript·ecmascript
艾莉丝努力练剑20 小时前
【Linux线程】Linux系统多线程(四):线程ID及进程地址空间布局,线程封装
java·linux·运维·服务器·c语言·c++·学习
方便面不加香菜20 小时前
C++ 类和对象(二)
开发语言·c++
@大迁世界20 小时前
20.“可复用组件”具体指的是什么?如何设计与产出这类组件?.
开发语言·前端·javascript·ecmascript
有味道的男人20 小时前
电商效率翻倍:用 Open Claw 对接 1688 接口,快速实现图片选品 + 货源监控
java·开发语言·数据库
froginwe1120 小时前
Chart.js 雷达图:深入解析与实际应用
开发语言