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
相关推荐
LUCIAZZZ30 分钟前
简单的SQL语句的快速复习
java·数据库·sql
来恩100336 分钟前
C# 类与对象详解
开发语言·c#
komo莫莫da1 小时前
寒假刷题Day19
java·开发语言
ElseWhereR1 小时前
C++ 写一个简单的加减法计算器
开发语言·c++·算法
S-X-S2 小时前
算法总结-数组/字符串
java·数据结构·算法
linwq82 小时前
设计模式学习(二)
java·学习·设计模式
※DX3906※2 小时前
cpp实战项目—string类的模拟实现
开发语言·c++
wjs20242 小时前
Nginx 安装配置指南
开发语言
桦说编程2 小时前
CompletableFuture 超时功能有大坑!使用不当直接生产事故!
java·性能优化·函数式编程·并发编程
@_@哆啦A梦2 小时前
Redis 基础命令
java·数据库·redis