Using "cells" with a "range"

I searched for this for a while, and I did not succeed.

I am trying to use the range with cells command in VBA, and the last line is a variable. Moreover, I do not get consecutive columns.

I need to select the range S2:S9 and U2:U9 (as already mentioned, the last line can be a variable). I know this command works:

 Range(Cells(2, 19), Cells(NumberofRows, 19)).select 

But I need to select 2 different columns that are not consecutive. I try something like this, but do not have time:

 Range(Cells(2, 19), Cells(NumLinhas, 19);(Cells(2, 21), Cells(NumLinhas, 21)).Select 

Does anyone know how to do this?

+6
source share
3 answers

Another option is to use the Union() Method in VBA.

 Union(Range(Cells(2, 19), Cells(NumLinhas, 19)), _ Range(Cells(2, 21), Cells(NumLinhas, 21))).Select 

If you have more ranges that you want to add to the join, you can continue to add ranges to the join, as shown below. This is especially useful when you include a loop in adding ranges to a join.

 Dim rngUnion As Range Set rngUnion = Union(Range("D1:D2"), Range("H1:H2")) Set rngUnion = Union(rngUnion, Range("B1:B2")) Set rngUnion = Union(rngUnion, Range("F1:F2")) rngUnion.Select 
+8
source

You can simply use this:

 Range("S2:S" & intLastRow & ",U2:U" & intLastRow).Select 
+7
source

If you want to stick to your logic with Range and Cells , try the following:

 Range(Range(Cells(2, 19), Cells(NumLinhas, 19)).Address & "," & _ Range(Cells(2, 21), Cells(NumLinhas, 21)).Address).Select 

A little long, but you will do your logical work.
You can also check the following links:

+1
source

All Articles