【问题标题】:Looping through Ranges - Union循环遍历范围 - 联合
【发布时间】:2019-12-04 23:09:16
【问题描述】:

我在尝试遍历我的范围时遇到错误。“方法 'Union' of '_Global' 失败”。请有人帮忙...在我得到更新代码后,“无法获取应用程序类的联合属性”

    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

【问题讨论】:

  • 这能回答你的问题吗? Method 'Union' of Object '_Global' failed
  • 尝试将其更改为Set allRG = Union(chuteRG, stowRG, pickRG, shipdockRG),因此省略.Cells 部分,因为这适用于我的工作表。
  • 仍然无法正常工作:/ .. 碰巧我需要一个具体的参考资料?

标签: vba foreach range union


【解决方案1】:

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

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

【讨论】:

  • 好的,尝试将其更改为Set allRG = Union(chuteRG, stowRG, pickRG, shipdockRG),因此省略.Cells 部分,因为这适用于我的工作表。
猜你喜欢
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 2015-10-17
  • 2018-08-23
  • 2013-05-03
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多