遍历范围-联合

时间:2019-12-04 23:09:16

标签: vba foreach range union

尝试遍历我的范围时遇到错误。““ _Global”失败的方法“ Union”。请有人帮助...更新代码后,我得到“无法获取Application类的Union属性”

    Dim chuteRG As Range, stowRG As Range, pickRG As Range, shipdockRG As Range, allRG As Range
    Dim cell As Range

    Set chuteRG = shChuteBowler.Range("F4:S18")
    Set stowRG = shStowBowler.Range("F4:S14")
    Set pickRG = shPickBowler.Range("F4:S12")
    Set shipdockRG = shShipDockBowler.Range("F4:S17")
    Set allRG = Union(chuteRG, stowRG, pickRG, shipdockRG).Cells

    For Each cell In allRG
        If cell = "0" Then
            cell = "X"
        End If
    Next cell


UPDATED - Still not working

    Dim chuteRG As Range, stowRG As Range, pickRG As Range, shipdockRG As Range, allRG As Range
    Dim cell As Range
    Dim ApXL As Object

    Set ApXL = CreateObject("Excel.Application")
    Set chuteRG = shChuteBowler.Range("F4:S18")
    Set stowRG = shStowBowler.Range("F4:S14")
    Set pickRG = shPickBowler.Range("F4:S12")
    Set shipdockRG = shShipDockBowler.Range("F4:S17")

    Set allRG = ApXL.Union(chuteRG, stowRG, pickRG, shipdockRG).Cells

    For Each cell In allRG
        If cell = "0" Then
            cell = "X"
        End If
    Next cell

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题。您需要在致电ApXL.之前添加Union

Dim chuteRG As Range, stowRG As Range, pickRG As Range, shipdockRG As Range, allRG As Range
Dim cell As Range

Set chuteRG = shChuteBowler.Range("F4:S18")
Set stowRG = shStowBowler.Range("F4:S14")
Set pickRG = shPickBowler.Range("F4:S12")
Set shipdockRG = shShipDockBowler.Range("F4:S17")

Set allRG = ApXL.Union(chuteRG, stowRG, pickRG, shipdockRG).Cells 'AMENDED

For Each cell In allRG
    If cell = "0" Then
        cell = "X"
    End If
Next cell