VBA Looping Thru Sheets, and Adding and Naming Ranges in Sheets

Peter_W

New Member
Joined
Apr 2, 2018
Messages
31
Hello,

I am trying to write a few lines of code that will:

- loop through all worksheets in my workbook,
- and if the worksheet name begins with "GL",
- then add and name a small range in that worksheet. And, range name will = worksheet name

I have code that partially works, but looking at the named ranges in Excel Name Manager, all the newly created ranges look like this:

GL_05684 #Ref ! =#Ref !$D$1:$D$2

So with that outcome, I did not get a correctly created range in each worksheet.

I am aware of other code to name ranges, that goes like the following, but haven't been able to totally understand how to use it with looping and variables:

Worksheets("Sheet1").Names.Add Name::="TestRange", _
RefersToR1C1:="=Sheet1!R1C1:R6C6" (So just offering this code fyi...again, I did not use this version)

Here's the code I have tried, which I would like to tweak to work correctly:

Sub NameRanges()
Dim Ws as Worksheet

For Each Ws In Worksheets
Ws.Activate

If Left(Ws.Name, 2) = "GL" Then

Range("D1:D2").Name = Ws.Name

End If

Next

End Sub

So hopefully, would like to revise those 9 lines of code. Any help, much appreciated, thanks.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi & welcome to MrExcel.
That code works for me, what are your sheet names?
 
Upvote 0
Hi & welcome to MrExcel.
That code works for me, what are your sheet names?


Fluff,

Right, the code does loop, but when I look at newly created ranges in Name Manager they all look like:


Name Value Refers To
GL_02176 #REF =#REF !$D$1:$D$D2

How do I get rid of the #REF errors?

Thanks for quick reply,
Peter_W
 
Upvote 0
What are your sheet names?
 
Upvote 0
What are your sheet names?

Fluff,

My sheet names are: GL_0673, GL_08946, and GL_02176.

The funny thing is, the code works if I run it as a stand-alone sub. When I try to add the code to another larger sub, or call it from a smaller, separate sub, it does not work.

This has baffled me for days. There is some quirky thing I'm probably not doing right.

Thanks in advance,
Peter_W
 
Upvote 0
The code you've supplied works correctly, so it's probably something to do with the other subs.
 
Upvote 0
This code works (or doesn't work) very inconsistently. After working a few times, I can use it again and get no ranges at all, during another looping iteration.

Totally baffles me, Pete
 
Upvote 0
Ok Fluff, thank you for your prompt reviews. I will look at the other code I have. Peter_W
 
Upvote 0
Fluff, I got this code to work. I had another issue that prevented it from working. Which I found and fixed. All set now. Thanks for your support!
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,818
Messages
6,121,725
Members
449,049
Latest member
MiguekHeka

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