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
相关推荐
Just right2 分钟前
python安装包问题
开发语言·python
峥嵘life2 分钟前
Android16 EDLA【CTS】CtsNetTestCases存在fail项
android·java·linux·学习·elasticsearch
dxz_tust5 分钟前
flow match简单直观理解
开发语言·python·深度学习·扩散模型·流匹配·flow match
写代码的【黑咖啡】5 分钟前
Python 中的时间序列特征自动提取工具:tsfresh
开发语言·python
Frank学习路上9 分钟前
【Qt】问题记录ld: framework ‘AGL‘ not found on MacOS 26
开发语言·qt·macos
陳103012 分钟前
C++:二叉搜索树
开发语言·数据结构·c++
wqwqweee13 分钟前
Flutter for OpenHarmony 看书管理记录App实战:个人中心实现
开发语言·javascript·python·flutter·harmonyos
t1987512817 分钟前
水下无人自主航行器(AUV)的MATLAB/Simulink仿真程序实现
开发语言·matlab
九皇叔叔36 分钟前
【01】SpringBoot3 MybatisPlus 工程创建
java·mybatis·springboot3·mybatis plus
tqs_1234540 分钟前
Spring Boot 和 Spring异同
java