• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Quick VBA question - defining cell range

Experts,

I have quick question in VBA programming. How do we select cell ranges when both the row range and column range are variables whose values are obtained from the program before it.

For example, if we want to auto fit ranges from B2 to E7, the code will be
range("B2:E7").autofit.
However, the indices could change for the next worksheet we apply the code to. It could change to, say, C3:D8.  The variable keep track of those row and column indices are ci, cj for begin and end column, ri, rj for begin and end row. Now that, if we do know ci, cj, ri, rj, what will be the code look like?
Will it be  
range(indirect("ciri,cjrj")) ? it does not seem to work.

Thanks
RDB
0
ResourcefulDB
Asked:
ResourcefulDB
  • 3
  • 2
1 Solution
 
Rory ArchibaldCommented:
You can use:
Range(Cells(ri, ci), cells(rj, cj))

Regards,
Rory
0
 
ResourcefulDBAuthor Commented:
Got it.

With this syntax, if we want to change the selected range's both font and background to white, is the following the right syntax?

Range(Cells(ri, ci), cells(rj, cj)).selection
With Selection.Font
        .ColorIndex = 2
    End With
    With Selection.Interior
        .ColorIndex = 2
    End With

the vba does not seem to like the line
Range(Cells(ri, ci), cells(rj, cj)).selection

what could be an alternative?

Thanks,
RDB
0
 
Rory ArchibaldCommented:
No need to select (you would need Select rather than Selection) - just use:
With Range(Cells(ri, ci), cells(rj, cj))
    .Font.ColorIndex = 2
    .Interior.ColorIndex = 2
End With

Regards,
Rory
0
 
ResourcefulDBAuthor Commented:
Rory,

Another quick question follow up. Should the variables in cells be numericals? for example, for cell c4, should it be cells(4,3) since the row for c4 is 4 and column for c4 is 3?

The question arise when the following works
DestWs.Range("A1:G10").Select
    Selection.ClearContents

but not this one

DestWs.Range(Cells(1, 1), Cells(10, 7)).Select
    Selection.ClearContents

Thanks
RDB
0
 
Rory ArchibaldCommented:
Well again you shouldn't Select the cells - it's unnecessary and slows the code down, but your main issue is that you also need to qualify the Cells property with the worksheet it belongs to, so it's:
DestWs.Range(DestWs.Cells(1, 1), DestWs.Cells(10, 7)).Select
    Selection.ClearContents

or better:
With DestWs
    .Range(.Cells(1, 1), .Cells(10, 7)).ClearContents
End With

Regards,
Rory
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now