bfreescott
Board Regular
- Joined
- Aug 6, 2015
- Messages
- 115
This snippet of my code is behaving oddly (at least to me) because when the column of range c is deleted, c then becomes c plus one, instead of next c.
I hope that is clear enough, but just in case it's not, let me give an example:
When looping through a row of ranges on row 11, if c is in column D and I delete column D, c becomes column F, when I want it to be E.
I don't understand the behavior, but I'm wondering if I should just avoid it altogether and add all the ranges where intVariable = 2 into an array and wait until after the For Next loop to finish before deleting the columns in the array. Recommendations?
(Caveat: I'm not a fan of arrays, so if you think that's the best practice here, can you provide an example?)
I hope that is clear enough, but just in case it's not, let me give an example:
When looping through a row of ranges on row 11, if c is in column D and I delete column D, c becomes column F, when I want it to be E.
I don't understand the behavior, but I'm wondering if I should just avoid it altogether and add all the ranges where intVariable = 2 into an array and wait until after the For Next loop to finish before deleting the columns in the array. Recommendations?
(Caveat: I'm not a fan of arrays, so if you think that's the best practice here, can you provide an example?)
Code:
For Each c In .Range("E11:M11")
If c.Offset(11, 0).Value = 0 Then
If intVariable = 2 Then
c.EntireColumn.Delete
ElseIf intVariable = 1 Then
c.EntireColumn.Hidden = True
End If
End If
Next c