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
相关推荐
Java后端的Ai之路9 小时前
【JDK】-JDK 17 新特性整理(比较全)
java·开发语言·后端·jdk17
郝学胜-神的一滴9 小时前
Effective Modern C++ 条款40:深入理解 Atomic 与 Volatile 的多线程语义
开发语言·c++·学习·算法·设计模式·架构
小小小米粒9 小时前
Spring Boot Starter ,不止是 “打包好配置的工具类包”
java·开发语言
一个天蝎座 白勺 程序猿9 小时前
国产数据库破局之路——KingbaseES与MongoDB替换实战:从场景到案例的深度解析
开发语言·数据库·mongodb·性能优化·kingbasees·金仓数据库
沛沛rh459 小时前
Rust 中的三个“写手“:print!、format!、write! 的详细区别
开发语言·后端·rust
城数派9 小时前
2005-2025年我国逐日露点温度栅格数据
数据分析·excel
tod1139 小时前
C++核心知识点全解析(四)
开发语言·c++·面试经验
用户8307196840829 小时前
告别冗余!Spring Boot Web 入参转换 6 种玩法,@InitBinder 可以退休了
java·spring boot
Desirediscipline10 小时前
#include<limits>#include <string>#include <sstream>#include <iomanip>
java·开发语言·前端·javascript·算法
番茄去哪了10 小时前
python基础入门(一)
开发语言·数据库·python