If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Setting a range in VBA that changes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-31-08, 20:23
Bob.Carter.17 Bob.Carter.17 is offline
Registered User
 
Join Date: Dec 2005
Posts: 111
Setting a range in VBA that changes

Gang,
I have an Excel export out of Access that will always be 6 columns wide, but the number of rows will change with each export. I want to write the macro to set borders for that area, but am having a difficult time trying to figure out the code. I can have a cell do the math to tell me how many rows there are that need to have borders, but how can I add a statement that says I need a range starting at A2, that is 6 columns wide and covers rows = value of B2?

I have looked thru help, but it escaped me.

Thanks,
Bob
Reply With Quote
  #2 (permalink)  
Old 11-01-08, 13:40
weejas weejas is offline
Registered User
 
Join Date: Sep 2006
Location: Surrey, UK
Posts: 448
Don't use a cell to calculate the range.
Add a loop to your macro so that it goes down each cell in the first column until it hits an empty cell. Then subtract 1 from ActiveCell.Row, and use that value to calculate your range. If you use intRows as your variable, your command to set the range will be something like:
Range("A2:F" & intRows).CODE
Reply With Quote
  #3 (permalink)  
Old 11-09-08, 01:41
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
The worksheet object has the UsedRange property that tells you how much of the worksheet is in use. ActiveSheet.UsedRange.Rows.Count would give you the number of rows used in the active worksheet, for example.

In general, the UsedRange might include rows that only look empty. That shouldn't be an issue with data straight from Access.
Reply With Quote
  #4 (permalink)  
Old 11-10-08, 09:37
pierrevbaexcel pierrevbaexcel is offline
Registered User
 
Join Date: Dec 2003
Location: Ottawa, Canada
Posts: 72
varNbRows=Range("A1").CurrentRegion.Rows.Count

will give you the number of rows then

Range("A2",Range("A2").Offset(varNbRows-2,5)).Select

will select the set of data

CurrentRegion is one of the most important property when you work with imported data from any database or accounting, finance, sales, production application
__________________
A piece of data is like a brick
If you don't build anything with it
It is just a brick
www.excel-examples.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On