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
相关推荐
无言(* ̄(エ) ̄)几秒前
C语言--运算符/函数/结构体/指针
c语言·开发语言·数据结构·数据库·算法·mongodb
进阶的猿猴1 分钟前
easyExcel实现下拉单选框和(变相的下拉多选框)
java
Ccuno2 分钟前
Java 核心类库与数据结构
java·深度学习
辣机小司3 分钟前
【踩坑记录:EasyExcel 生产级实战:策略模式重构与防御性导入导出校验指南(实用工具类分享)】
java·spring boot·后端·重构·excel·策略模式·easyexcel
better_liang3 分钟前
每日Java面试场景题知识点之-RabbitMQ消息重复消费问题
java·分布式·消息队列·rabbitmq·幂等性
醒过来摸鱼3 分钟前
Spring Cloud Gateway
java·spring·spring cloud
2501_944441754 分钟前
Flutter&OpenHarmony商城App消息通知组件开发
java·javascript·flutter
we1less5 分钟前
[audio] AudioTrack (四) getOutputForAttr 分析
android·java
计算机毕设指导67 分钟前
基于微信小程序的博物馆文创系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
沐知全栈开发8 分钟前
PHP EOF (Heredoc)
开发语言