从多个单元格创建范围

时间:2013-11-28 08:56:12

标签: excel excel-vba vba

我想从多个单元格创建一个范围。像这样:

我想拥有以下4个单元格:

  • Cells(4, 7)
  • Cells(4, 8)
  • Cells(8, 7)
  • Cells(8, 8)

从文件中提取,然后保存到另一个文件作为第一行。

我写了这样的话:

With mybook.Worksheets(5)
    Set sourceRange = .Range(Cells(4, 7), Cells(4, 8), 
                             Cells(8, 7), Cells(8, 8))
End With 

我遇到的问题是,摘要表中只显示Cells (4,7)Cells (4,8)

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

非常基本的方法。

Sub Transfer4Cells()
    Dim sSht As Worksheet, tSht As Worksheet
    Dim sRng As Range, tRng As Range
    Dim Rng As Range
    Set sSht = ThisWorkbook.Sheets("Sheet1") 'Change as needed.
    Set tSht = ThisWorkbook.Sheets("Sheet2") 'Change as needed.
    Set sRng = sSht.Range("G4,H4,G8,H8") 'You can add more cells here.
    Pos = 1
    For Each Rng In sRng
        tSht.Cells(1, Pos).Value = Rng.Value
        Pos = Pos + 1
    Next Rng
End Sub

虽然不是完美的解决方案,但如果您只需要提取这4个单元格,或者甚至更多,那么该方法非常灵活。

希望这有帮助。