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 分钟前
Graphify安装与结合claude code使用指南
java·python·ai编程·图搜索
seven97_top6 分钟前
Tomcat的架构设计和启动过程详解
java·tomcat
qq_2546177712 分钟前
attribute((constructor)) 在C/C++中的应用
开发语言·c++
wtsolutions12 分钟前
Excel-to-JSON Local App - Secure Offline Excel to JSON Conversion
json·excel
xyq202414 分钟前
HTML5 Input 类型详解
开发语言
云深麋鹿16 分钟前
C++ | 多态
开发语言·c++
我是无敌小恐龙20 分钟前
Java SE 零基础入门 Day05 类与对象核心详解(封装+构造方法+内存+变量)
java·开发语言·人工智能·python·机器学习·计算机视觉·数据挖掘
逻辑驱动的ken25 分钟前
Java高频面试考点14
开发语言·数据库·算法·哈希算法
小灰灰搞电子27 分钟前
Python self关键字详解及其应用
开发语言·python
故事还在继续吗28 分钟前
C++17关键特性
开发语言·c++·算法