set range up until text vba help

for some reason my range isn't working right.

 'set new range
   lr = Cells(Rows.Count, "B").End(xlUp).Row
   Set rng = Range("A1").Resize(lr, Val(Mid(Worksheets(1).Range("B4"), 6)) - 1)

on worksheet B4, it lists: "Week9", this may change, and I want to set my range from A1 up until that text of the B4 on worksheet "RUN". How can I get this to work properly?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use something like this...

Dim ws as worksheet
set ws=Sheets("Run")
set rng=ws.range("A1:B" & lrow)

Open in new window

pls try

   Set rng = Range("A1").Resize(lr, Val(Mid(Worksheets(1).Range("B4"), 5, 2)) - 1)

you had 6 when Week9 has only 5 letters

NorieAnalyst Assistant Commented:
Do you mean you want to set the range from A1 to the cell in column A that has the text that is in B4?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Do you want to include the "RUN" row or the row before that?
NorieAnalyst Assistant Commented:

Isn't 'RUN' the name of the worksheet?

It wasn't clear whether the text was missing an equals sign
...until that text of the B4 on worksheet = "RUN"

...or if the row needed to be the last row containing the text value of cell B4
...or whether we sought the last non-empty row in column B.

It would be a more clearly defined problem if we had a representative sample workbook.
jfrank85Author Commented:
To be clear:

the worksheet "RUN" will list the Week name in B4. This week name is in the format of: "Week #".
It can be anywhere from "Week 1" to "Week 52".

Throughout the rest of the workbook are other tabs, which are created by my macro. I'm currently using this code to grab the range:

'set new range
   lr = Cells(Rows.Count, "B").End(xlUp).Row
   Set rng = Range("A1").Resize(lr, Val(Mid(Worksheets(1).Range("B4"), 6)) + 3)

However, I notice that if I have "Week 9" in B4 on the Run worksheet, my range actually captures up to "Week 10". So something is off here..

On the other tabs, to be clear, the weeks are in the title bar, so A1:BF1
Rob HensonFinance AnalystCommented:
Assuming you want to start at A1 and go down as far as the first instance of the text specified in B4, you can use the MATCH function:


The VBA equivalent may have to use the "Application.WorkbookFunction" statement but will generate a variable value which can then be used as the last row of the range.

Rob H
NorieAnalyst Assistant Commented:
Does this work?
   lr = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("A1", Cells(1, Application.Match(Sheets("RUN").Range("B4"), Rows(1), 0))).Resize(lr)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jfrank85Author Commented:
thanks Norie! works great
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.