excel - 将值粘贴到多列中的 VBA 代码
问题描述
我正在尝试编写一个 VBA 代码来复制和粘贴 CSV 文件中的多个列。目前我有以下代码:
Set thisWS = ActiveWorkbook.Sheets("Sheet1")
Set thatWB = Workbooks.Open("file_with_data.csv")
Set thatWS = thatWB.Sheets("table_with_data")
thatWS.Range("A2:X5000, Z2:Z5000, AC2:AC5000, AE2:AG5000").Copy
thisWS.Range("A4:X5002, Z4:Z5002, AC4:AC5002, AE4:AG5002").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
但是,当我运行它时,我收到以下错误消息:
Run-time error '1004'
That command cannot be used on multiple selections.
-> thisWS.Range("A4:X5002, Z4:Z5002, AC4:AC5002, AE4:AG5002").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
解决方案
根据SJR的建议:
Sub UseArray()
For Each a In Split("A2:X5000,Z2:Z5000,AC2:AC5000,AE2:AG5000", ",")
With Range(a)
.Copy
.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
End With
Next a
End Sub
(另一种方法是循环Areas
)
推荐阅读
- npm - NPM 注册中心需要多长时间才能在全球传播?
- swift - 在 Swift 中随机播放字符串中的字符
- arrays - 如何在 Swift 中删除与 myindexes 数组中的索引匹配的所有项目
- azure - 如何在 Azure 中创建 RSS 源?
- express - 如何检查对象中的键是否具有特定值?
- python - 我正在使用 Redis 替换 ROS,这很奇怪吗?
- json - Angular:带有空 200/201 响应的 HttpClient 错误(始终调用 JSON.parse(""))
- javascript - 在 Node.js (React) 中更改为不同的页面/组件时如何运行不同的 SQL 函数?
- awk - awk:根据两列的连接值添加新列
- r - 如何使用ggmap在谷歌地图上绘制一个小标题?