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
相关推荐
用户3521802454752 小时前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
东坡白菜5 小时前
破局全栈:一个前端开发的Java入门实战记录(1)
java·全栈
唐青枫5 小时前
Java Tomcat 实战指南:从 Servlet 容器到 Spring Boot 部署
java
wsaaaqqq6 小时前
roudan:自由选择实体、灵活操作数据、快速写入数据库的 Java 框架
java
plainGeekDev9 小时前
null 判断 → Kotlin 可空类型
android·java·kotlin
糖拌西瓜皮9 小时前
Java开发者视角:深入理解Node.js异步编程模型
java·后端·node.js
plainGeekDev9 小时前
getter/setter → Kotlin 属性
android·java·kotlin
一线大码10 小时前
Smart-Doc 的简单使用
java·后端·restful
MacroZheng11 小时前
Claude Code官方桌面端正式发布,夯爆了!
java·人工智能·后端