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
相关推荐
Mcworld85723 分钟前
整数分解JAVA
java·开发语言
请你喝好果汁64127 分钟前
python_竞态条件
开发语言·python
正在走向自律28 分钟前
Python 数据分析与可视化:开启数据洞察之旅(5/10)
开发语言·人工智能·python·数据挖掘·数据分析
小南家的青蛙1 小时前
LeetCode面试题 01.09 字符串轮转
java·leetcode
dudly1 小时前
Python 字典键 “三变一” 之谜
开发语言·python
秋野酱1 小时前
基于javaweb的SpringBoot爱游旅行平台设计和实现(源码+文档+部署讲解)
java·spring boot·后端
饕餮争锋1 小时前
org.slf4j.MDC介绍-笔记
java·开发语言·笔记
shane-u1 小时前
Maven私服搭建与登录全攻略
java·maven
半部论语1 小时前
jdk多版本切换,通过 maven 指定编译jdk版本不生效,解决思路
java·开发语言·maven·intellij-idea
阿沁QWQ1 小时前
单例模式的两种设计
开发语言·c++·单例模式