?
Solved

Quick VBA question - defining cell range

Posted on 2007-08-07
5
Medium Priority
?
482 Views
Last Modified: 2008-02-01
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
Comment
Question by:ResourcefulDB
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 19646315
You can use:
Range(Cells(ri, ci), cells(rj, cj))

Regards,
Rory
0
 

Author Comment

by:ResourcefulDB
ID: 19649131
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 19649832
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
 

Author Comment

by:ResourcefulDB
ID: 19656298
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
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 19656469
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question