Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Range References with VBA

Status
Not open for further replies.

reichertc

Mechanical
Mar 21, 2001
115
OK. I have two worksheets in a workbook, "OSBL" and "OSBLData". The OSBLData sheet is full of information that I retreived from an Access database. Excel automatically refers to the data that Access has provided me by the range name "ExternalData1". I want to lookup information on the OSBL sheet from "ExternalData1". The problem is that my lookupvalue consists of a combination of the data in the first two columns that Access has returned. So, I inserted a column to the left of the data and entered the formula =B2&C2 in cell A2 and copied it down. This gives me my list of unique values that I can then use with the VLOOKUP function. Now my range "ExternalData1" only refers to columns B,C,D... while the values I am looking up are in column A. So I redefine the range "ExternalData1" to include column A.

The reason I did all this was that the "ExternalData1" range will automatically resize every time I refresh the data that is being returned from Access. This is extremely useful for the hundred of formulas I have on the sheet "OSBL".

Does anyone know of a way that I can use VBA to automatically redefine the range one column to the left when I click the refresh data button on the external data toolbar? If this is too much of a pain, how about just a VBA procedure that will do it that I could assign to a button? It's no problem for me to redefine the range, but the other users of the spreadsheet will likely destroy all my work if I ask them to do anything more than click a button...

Thanks in advance,

Chris
 
Replies continue below

Recommended for you

Large amount of data is usually easier to handle in a database.

Why dont you just keep the data in access and use this program?

Best Regards

Morten
 
I've got some major math I am trying to do to this, plus I am working off a database that our IT department set up and locked so that we have a difficult time modifying certain aspects.

I did manage to find a good way to deal with this. I set up a query in Access that combined the information in column B and C. I then changed my "Get External Data" information in Excel to look up this information instead. This returned the data I needed in the format I needed.

The original problem still stands. I would be interested in knowing how to reference any of the ranges that Excel automatically creates and then make a slight modification. I think this could be quite useful.
 
Morten,
If you have continious data (no empty cells) at least in the first row and in the first column then you can use dynamic ranges. After inserting the column and filling it with data run macro:

Sub DefineRangeName()

ActiveWorkbook.Names.Add Name:="datarange", RefersToR1C1:= _
"=OFFSET(Sheet1!R1C1,0,0,COUNTA(Sheet1!C1),COUNTA(Sheet1!R1))"

End Sub

This way datarange will always start from cell A1 (until the next column-insert).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor