How do I use the range property using variables

andyjames

Board Regular
Joined
May 15, 2007
Messages
133
At the moment I have the following code

Range("F7:H13").Select
With Selection.Interior
.ColorIndex = 44
.Pattern = xlSolid
End With


How do I replace this so that the F, 7, H and 13 are all variables that can change in the program?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Well that really depends on your variables.

Could be something like this.
Code:
With Range("F" & FirstRow & ":H" & LastRow)
     .ColorIndex = 44
     .Pattern = xlSolid
End With
 
Upvote 0
Thanks.
Unfortunately this also involves knowing the columns (namely, F and H).
May be it's not the RANGE property I should be using.
But I wanted my programme to be able to change (increment) the columns also.
Any ideas?
 
Upvote 0
Well it's hard to say without seeing the rest of the code, the variables and more information on what you want to achieve.:)

Some ideas though - Cells, Resize, Offset.
 
Upvote 0
The following code, placed in a module by itself, will do the trick. You pass in the column numbers and row numbers. You can easily adapt this to take the columns as letters ("F"-"H") rather than numbers (6'8).

THANKS TO RICHARDSCHOLLAR

Option Explicit

Sub test()
ColorCells 6, 7, 8, 13
End Sub

Function ColLet(piNum As Integer) As String
ColLet = Split(Cells(1, piNum).Address(, False), "$")(0)
End Function

Sub ColorCells(piStartCol As Integer, piStartRow As Long, _
piStopCol As Integer, piStopRow As Long)
Dim sStartLet As String
Dim sStopLet As String
sStartLet = ColLet(piStartCol)
sStopLet = ColLet(piStopCol)
Range(sStartLet & piStartRow & ":" & sStopLet & piStopRow).Select
Selection.Interior.ColorIndex = 44
Selection.Interior.Pattern = xlSolid
End Sub


--Doug
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,395
Members
448,891
Latest member
tpierce

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