我需要从 sheet1
中的一系列单元格中复制内容,比如 A1:A10
,然后将所有数据粘贴到 sheet2< 中的单个单元格中
在单元格 A1
中
这似乎是一个简单的用例,但到目前为止我找不到任何解决方案。以下是我尝试过的
' Attempt 1
Sheets("shee1").Select
Range("A1:A10").Select
Selection.Copy
MsgBox Selection.Text ' Error: Type mismatch. If I add Selection.Text to watch I see an array kind of structure with each index having each cell data
Dim val = Selection.Text
Sheets("sheet2").Select
Range("A1") = val
' Attempt 2
Sheets("sheet1").Range("A1:A10").Copy(Sheets("sheet2").Range("A1") 'Doesn't copy in single cell A1, instead copies to multiple rows like in source
最佳答案
也许是这个?
Option Explicit
Sub Sample()
Dim MyAr As Variant
MyAr = Sheets("Sheet1").Range("A1:A10").Value2
Sheets("sheet2").Range("A1").Value = Join(Application.Transpose(MyAr), vbLf)
End Sub
这会将范围读入一个数组,然后在将输出写入相关范围之前加入它们。
关于Excel VBA - 从一系列单元格中复制并粘贴到一个单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67573160/