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
相关推荐
她说彩礼65万几秒前
C# Lambda 表达式
开发语言·c#
未若君雅裁1 分钟前
LeetCode 18 - 四数之和 详解笔记
java·数据结构·笔记·算法·leetcode
Bug快跑-12 分钟前
Java、C# 和 C++ 并发编程的深度比较与应用场景
java·开发语言·前端
2501_941111468 分钟前
高性能计算集群部署
开发语言·c++·算法
Moe48830 分钟前
ConcurrentHashMap 重要方法实现原理和源码解析(二)
java·后端
普通网友36 分钟前
模板编译期机器学习
开发语言·c++·算法
普通网友38 分钟前
C++与机器学习框架
开发语言·c++·算法
普通网友38 分钟前
C++安全编程指南
开发语言·c++·算法
学困昇1 小时前
C++11中的右值引用和移动语义
开发语言·c++
有梦想的攻城狮1 小时前
初识Rust语言
java·开发语言·rust