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
相关推荐
无语......6 分钟前
安装uv并管理 Python / 包
开发语言·python·uv
道剑剑非道8 分钟前
【C++ 仿 MFC 反射系统】
开发语言·c++·mfc
电商API&Tina9 分钟前
1688 拍立淘接口(item_search_img)测试与接入实战心得
java·大数据·前端·物联网·oracle·json
行走的搬运工15 分钟前
Spring Security_05
java·spring·mybatis
我登哥MVP18 分钟前
【Spring6笔记】 - 11 - JDBCTemplate
java·数据库·spring boot·mysql·spring
也许明天y28 分钟前
Spring AI 核心原理解析:基于 1.1.4 版本拆解底层架构
java·后端·spring
小红的布丁39 分钟前
BIO、NIO、AIO 与 IO 多路复用:select、poll、epoll 详解
java·数据库·nio
Elastic 中国社区官方博客39 分钟前
在 Elastic 中使用 OpenTelemetry 内容包可视化 OpenTelemetry 数据
大数据·开发语言·数据库·elasticsearch·搜索引擎
lifallen40 分钟前
Flink Checkpoint 流程、Barrier 流动与 RocksDB 排障
java·大数据·flink
C+++Python42 分钟前
如何学习Python的应用领域知识?
开发语言·python·学习