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
相关推荐
菜冻鱼14 分钟前
Python-sklearn-特征工程
开发语言·人工智能·python·机器学习·numpy·matplotlib·sklearn
SL_staff31 分钟前
JVS数字底座实践:如何复用企业文档能力快速构建知识类应用
java·数据库·程序员
sugar__salt34 分钟前
MyBatis ③动态 SQL 完全指南 —— 从条件拼接到批量操作
java·sql·mybatis
吴声子夜歌34 分钟前
正则指引——PHP
开发语言·正则表达式·php
吃饱了得干活1 小时前
Java并发安全:看这一篇就懂了!
java·后端·面试
前端双越老师1 小时前
前端学习 Java 其实很容易:TS 和 Java 语法的 N 个相同点
java·全栈
Claire_881 小时前
企业文件管理系统选型技术框架与主流产品对比分析
开发语言·php
(Charon)1 小时前
【C++】:使用 mutex + deque 实现一个简单的 LockedQueue
开发语言·c++
星轨初途1 小时前
LeetCode 热题 100——day10 和为 K 的子数组
开发语言·c++·算法·leetcode
sycmancia1 小时前
Qt——自定义控件温度计
开发语言·qt