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?
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

NorieVBA ExpertCommented:
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?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Do you want to include the "RUN" row or the row before that?
NorieVBA ExpertCommented:

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
NorieVBA ExpertCommented:
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 ConnectWise

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.