VBA set range to non-continuous with variables

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,375
hello all,

not sure what to code I need to select the range
Code:
startrow =2
endrow = 15

startcol=10
end col=50
colstep = 10


for r = startrow to endrow
for c = startcol to endcol step colstep

set my Range=???
....

any ideas??

Ross
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How about
Code:
Sub chk()
Dim rng As Range

Set rng = Intersect(Rows("2:15"), Range("J:J,T:T,AD:AD,AN:AN,AX:AX"))
End Sub
 
Upvote 0
Or this would start with a single cell and then expand with each iteration of the loops.

Code:
Set myRange = Range(Cells(2, 10), Cells(r, c))
 
Last edited:
Upvote 0
Perhaps
Code:
startrow =2
endrow = 15

startcol=10
end col=50
colstep = 10

Set myRng = Cells(startRow, startCol)

For r = startRow to endRow
    For c = startCol to endCol Step 10
        set myRng = Application.Union(myRng, Cells(r, c))
    Next c
Next r
 
Upvote 0
Mikeerickson... you post you resposne while i was trying mine below. look thik your will work.


let me re-phrase:

the 5 variables will be tied to input boxes that the user will enter.

Code:
startrow =inputbox ("enter START row")
endrow = inputbox ("enter END row")

startcol=inputbox ("enter START column")
endcol=inputbox ("enter END column")
colstep = inputbox ("enter column STEP")


for r = startrow to endrow
for c = startcol to endcol step colstep

set my Range=???
....

later in the macro I will do some things on the range, like countif values, setting formats etc..

Ross
 
Last edited:
Upvote 0
Mikeerickson solution works for most of the thing I need to do with the range (Format, set fonts etc.)

but now I need a countif

ex:
x = WorksheetFunction.CountIf(myRange, "Joe")

the above will gives a error try to count how many times Joe is in the range.
 
Upvote 0
There is no discontinous range that will work properly with COUNTIF.

It would be best to count "Joes" while assembling the range.

Alternatly, if "Joe" only appears in the "good" cells, you could COUNTIF the whole sheet.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top