Excel VBA: I'm trying to add a range to a pre-existing range using Application.Union. Every time the Union makes the attempt, I get runtime error 1004. The kicker is that the ranges are on the same sheet, so I'm at a loss.
The magic is happening in the IF block within the nested FOR loop. I've included a bit more code for context. Any help would be greatly appreciated!
Code sample:
'---------------------------------------
'Get range of battle1 IDs from Sessions
'---------------------------------------
Dim ws As Worksheet: Set ws = objSessionsWB.ActiveSheet
Set rngBattleIDs = ws.Range("E3", ws.Range("E3").End(xlDown))
'---------------------------------------------------------
'Get range of battles from Battles that match battle1 IDs
'---------------------------------------------------------
Set ws = objBattlesWB.ActiveSheet
Dim rngBattleStats As Range
Dim bRow As Range
For Each battle In ws.Range("A3", ws.Range("A3").End(xlDown))
For Each battleID In rngBattleIDs
If battleID.Value = battle.Value Then
'Get row containing battle1 info
Set bRow = ws.Range(battle.End(xlToLeft), battle.End(xlToRight))
'Use Union to add additional rows to range
If rngBattleStats Is Nothing Then
Set rngBattleStats = bRow
Else
Set rngBattleStats = Union(rngBattleStats, bRow)
End If
End If
Next
Next
Looks like the problem was outside the scope of my question. The script is being run in workbook1, but the ranges I'm working with are in workbook2. The solution was to run Union from workbook2: