Link to home
Start Free TrialLog in
Avatar of jfrank85
jfrank85

asked on

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?

Thanks.
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

You can use something like this...

Dim ws as worksheet
set ws=Sheets("Run")
lrow=ws.cells(cells.rows.count,"b").end(xlup).row
set rng=ws.range("A1:B" & lrow)

Open in new window


Saurabh...
Avatar of Rgonzo1971
Rgonzo1971

Hi,
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

Regards
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?
Do you want to include the "RUN" row or the row before that?
aikimark

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

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.
Avatar of jfrank85

ASKER

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

=MATCH($B$4,Run!$A:$A,0)

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.

Thanks
Rob H
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks Norie! works great