Dynamically find a range within a sheet...help with VBA please!

lmoseley7

Board Regular
Joined
Jul 9, 2013
Messages
151
I have a macro that opens another worksheet and pulls information via VLOOKUP formulas from a range that I name within that sheet. Recently, for some reason the information has been showing up in different columns on the report and since I can't control how that report is produced, I need to be able to find a certain text string that will indicate the top left cell of my range. Then I can search for another text string on that same row to get the width of my range. Previously when the reports were consistently being produced I had the macro count the number of rows with text in the starting column of the range to determine the height of my range. Since the starting column is moving around, it sometimes resides in the same column as the header information, so I can't predict how many rows of text to exclude from my COUNTA calculation.

What I need is a macro that will create a named range, the size of which we will determine. First it will search for the phrase "SS Acct #"; this will be the top left cell of the range. I believe if we limit the possible range of finding that phrase to cells B10:E20 that will provide more than enough room to handle the inconsistency of this report. From that cell we need to count how many rows below it have text in them. That number will be the number of rows in our range. We will also search for "Amount" in the same row as we found SS Acct #. That will represent the last column in our range and will be the column we use in our VLOOKUP formula as the amount to return.

I've been able to find the top left cell. I'm not sure how to count the rows beneath that cell that have been used. The amount column has consistently been two columns over from the 1st column, but I don't want to assume that will remain. I'm having the most trouble converting row numbers into ranges and now I think I'm so lost in the weeds there must be a easier solution, that hopefully one of you will share.

An example of the data would be as follows:
C D E
12: SS Acct # Amount
13: ABF2 93,402.97
14: ABF5 5,643.07

The next time this report comes C12 might be D12 and E12 would now be F12.

Thanks in advance for any help.
 
Update

I added the second macro code to the first just to see if I could make it work. The first thing I noticed was that to display the range in the message box I had to change my code to the following:
Code:
MsgBox "" & myRange.Address
That was nice to figure out, but not necessary for the code.
I would like to be able to reference this code by calling another macro, and at this point I don't care if it is in the same module or not, although it would be nice to know how to do it that way if possible.

I'm close, but I need help getting over the hump. How do I get this to work when myRange and the code that creates it is in another macro?
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
What exactly was wrong with

Code:
Range(LURAnchor, Cells((LastRow + AnchorRow - 1), LastCol)).Name = "Pull_Data"

which you commented out in your last code?
 
Upvote 0
There was nothing wrong with that code, the only problem was that the name would change depending on the report. We have two reports that are very similar in structure, one is for money going out (push) and one is for money coming in (pull). I wanted to use this same code for both so I wanted to move the naming of the range back to the original macro, which is why I was trying to pass the range variable from another macro. Thanks for looking!
What exactly was wrong with

Code:
Range(LURAnchor, Cells((LastRow + AnchorRow - 1), LastCol)).Name = "Pull_Data"

which you commented out in your last code?
 
Upvote 0
So basically the same as the line you commented out. :)
Happy you found a solution you can work with.
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,156
Members
452,385
Latest member
Dottj

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
Back
Top