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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
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
 

andyjames

Board Regular
Joined
May 15, 2007
Messages
133
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?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
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.
 

markadon

Board Regular
Joined
Feb 12, 2007
Messages
193
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,602
Messages
5,832,665
Members
430,153
Latest member
Javid_P

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
Top