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
相关推荐
小灵不想卷27 分钟前
LangChain4 初体验
java·langchain·langchain4j
忍者必须死28 分钟前
ConcurrentHashMap源码解析
java
顾北1228 分钟前
SpringCloud 系列 04:Gateway 断言 / 过滤器 / 限流 一站式落地指南
java·开发语言·数据库
闻哥29 分钟前
23种设计模式深度解析:从原理到实战落地
java·jvm·spring boot·设计模式·面试
wuqingshun3141591 小时前
java创建对象的方式
java·开发语言
求知摆渡1 小时前
Spring AI 多模型对话 Demo 实战:OpenAI/Ollama 一套接口、Redis 会话记忆、SSE 流式输出、AOP 日志打点
java·spring
二十雨辰1 小时前
[英语]-介词和动词
开发语言
米羊1211 小时前
Struts 2 漏洞(下)
java·后端·struts
程序员敲代码吗1 小时前
提升Python编程效率的五大特性
开发语言·python
Kurbaneli1 小时前
C语言过时了吗?2025年仍不可替代
c语言·开发语言