L0lax89
08-17-2017, 07:46 AM
Hello everyone,
This problem I have is really bugging me.
I'm a beginner to VBA and want to write a code which does things to the cells in the first column from a user selection. This is the code if the selection is contiguous:
Sub simpleloop1()
Dim v_currang As Range
Dim v_selrang As Range
Set v_selrang = Selection.Columns(1)
For Each v_currang In v_selrang
'code goes here, next line as example
v_currang.Select
Next v_currang
End Sub
This doesn't work. When reaching the v_currang.Select part, it selects the entire Selection.Columns(1) range and exits the loop.
The next example works, it does loop through the entire range:
Sub simpleloop2()
Dim v_currang As Range
Dim v_selrang As Range
Selection.Columns(1).Select
Set v_selrang = Selection
For Each v_currang In v_selrang
'code goes here, next line as example
v_currang.Select
Next v_currang
End Sub
Why does the second work but the first doesn't?
Thx
This problem I have is really bugging me.
I'm a beginner to VBA and want to write a code which does things to the cells in the first column from a user selection. This is the code if the selection is contiguous:
Sub simpleloop1()
Dim v_currang As Range
Dim v_selrang As Range
Set v_selrang = Selection.Columns(1)
For Each v_currang In v_selrang
'code goes here, next line as example
v_currang.Select
Next v_currang
End Sub
This doesn't work. When reaching the v_currang.Select part, it selects the entire Selection.Columns(1) range and exits the loop.
The next example works, it does loop through the entire range:
Sub simpleloop2()
Dim v_currang As Range
Dim v_selrang As Range
Selection.Columns(1).Select
Set v_selrang = Selection
For Each v_currang In v_selrang
'code goes here, next line as example
v_currang.Select
Next v_currang
End Sub
Why does the second work but the first doesn't?
Thx