VBA set range to non-continuous with variables

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,428
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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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