Creating a named range using the sheet name in an array sheet Loop - VBA

SamNew2Coding

New Member
Joined
Jun 15, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a named range for multiple sheets from a specific cell to the last row. What i currently have:

VBA Code:
Sub test123()
 Dim wsName As Variant, ws As Worksheet, LastRow As Long

 For Each wsName In Array("AA-CR", "AA-CP", "AAA-CR", "AAA-CP", "AAT-CP", "AAT-CR")
 Set ws = Worksheets(wsName)
 
 
 LastRow = ws.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 wsStringName = wsName
 ws.Range("A3:D" & LastRow).Name = wsName

 
 Next
 
End Sub

I have taken this mostly from other posts. I feel like i am close but it bugs when i run this line:
VBA Code:
 ws.Range("B3:D" & LastRow).Name = wsName

I need some way to return the wsName value.

Bonus: Most sheets it will be "A3:D" and LastRow but for two of them it will be "B3:D" & LastRow, Is there an easy way to have an extra dimension where i can tell it to use this variable for two of them or do i have to just have another loop for the two different sheets?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi & welcome to MrExcel.
How about
VBA Code:
ws.Range("A3:D" & LastRow).Name = Replace(wsName, "-", "_")
It would probably just be easier to run another loop for the other two sheets.
 
Upvote 0
Hi and welcome to MrExcel.

In named range you cannot use the hypen, you must use the underscore:

VBA Code:
Sub test123()
 Dim wsName As Variant, arr1 As Variant, arr2 As Variant
 Dim ws As Worksheet, LastRow As Long, i As Long
 
 arr1 = Array("AA-CR", "AA-CP", "AAA-CR", "AAA-CP", "AAT-CP", "AAT-CR")
 arr2 = Array("A", "B", "A", "B", "A", "A") 'Here indicates the starting column
 For i = 0 To UBound(arr1)
  wsName = arr1(i)
  Set ws = Worksheets(wsName)
  LastRow = ws.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  ws.Range(arr2(i) & "3:D" & LastRow).Name = Replace(wsName, "-", "_")
 Next
End Sub
 
Upvote 0
If only i knew how that the "-" was what was messing it up, i wouldve changed the sheetname and saved a fair bit of time! That worked wonders. Much Appreciated!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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