我在A1:E1
中有5个表头。我想在单元格(A2, B4, D5, E1, F3)
中显示连续的单元格数据A2:E2
。请帮助这样做。我的代码显示在下面
Sub test()
Dim copyRange As Range, cel As Range, pasteRange As Range,
erow As Long, ecolumn As Long
Set copyRange = ThisWorkbook.Sheets("Sheet1").Range("A2, B4, D5, E1, F3")
Set pasteRange = ThisWorkbook.Sheets("Sheet2").Range("A2")
For Each cel In copyRange
cel.Copy
erow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Offset(1, 1).Row
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(0, 1).Row
ecolumn = Sheet2.Cells(2, Columns.Count).End(xlToLeft).Offset(0, 1).Row
pasteRange.Cells(1, ecolumn).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub
答案 0 :(得分:1)
更简单的方法:
Sub test()
Dim cel As Range, pasteRange As Range
Set pasteRange = ThisWorkbook.Sheets("Sheet2").Range("A2")
For Each cel In ThisWorkbook.Sheets("Sheet1").Range("A2, B4, D5, E1, F3")
pasteRange.Value = cel.Value
Set pasteRange = pasteRange.Offset(0, 1)
Next
End Sub
答案 1 :(得分:0)
@TimWilliams的答案很简单,但我只是想向您展示如何使用数组来完成此任务:
Sub t2()
Dim copyRange As Range, cel As Range, pasteRange As Range
Set copyRange = ThisWorkbook.Sheets("Sheet1").Range("A2, B4, D5, E1, F3")
Set pasteRange = ThisWorkbook.Sheets("Sheet2").Range("A2")
Dim copyVals() As Variant
ReDim copyVals(0 To copyRange.Cells.Count)
Dim n As Long
n = 0
For Each cel In copyRange
copyVals(n) = cel.Value
n = n + 1
Next cel
Dim k As Long
For k = LBound(copyVals) To UBound(copyVals)
pasteRange.Offset(k, 0).Value = copyVals(k)
Next k
End Sub