Excel 如何显示一行中的连续单元格

Excel 如何显示一行中的连续单元格,excel,vba,copy-paste,Excel,Vba,Copy Paste,我在A1:E1中得到了5个表格标题。我想在单元格A2:E2中显示连续单元格数据A2、B4、D5、E1、F3。请帮忙。我的代码显示在下面 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,

我在A1:E1中得到了5个表格标题。我想在单元格A2:E2中显示连续单元格数据A2、B4、D5、E1、F3。请帮忙。我的代码显示在下面

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
更简单的方法:

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

@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

你的问题到底是什么?你的代码不工作吗?它会出错吗?如果是,错误信息在哪里,是什么?如果没有错误,它会做什么?另外,由于您只需要这些值,您可以跳过使用剪贴板,并将范围的值设置为彼此相等:pasteRange.Cells1,ecocolumn.Value=cel.Value。