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
相关推荐
zcmodeltech7 分钟前
反应装置模型控制系统设计与实现:多设备协同联动方案
java·网络·数据库·stm32·嵌入式硬件·能源·制造
研☆香16 分钟前
数组方法 splice讲解 拓展
开发语言·前端·javascript
码视野18 分钟前
基于 Spring Boot + Vue3 的【城市地下燃气管网微泄漏感知与相邻地下空间燃爆预警中台】设计与实现(含PRD/三端高保真源码/大屏)
java·前端·人工智能·spring boot·后端
tryCbest23 分钟前
Excel常用公式(WPS vs Office)
excel·wps
峥嵘life35 分钟前
Android16 系统 APEX 模块说明
android·大数据·开发语言
2601_9620715739 分钟前
数据库系统架构与DBMS功能探微:现代信息时代数据管理的关键
java·开发语言·数据库
————A40 分钟前
Agent 文件处理中间件
开发语言·javascript·ecmascript
梦想的旅途241 分钟前
如何高效调用企业微信通讯录API管理组织架构
java·开发语言·企业微信
whcyhhh1 小时前
头歌实践教学平台:大数据存储2023(十三3)
大数据·开发语言·python
2601_962065251 小时前
2.启动mysql容器
java·数据库·mysql