Code name not working

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have some code that opens a workbook and I then used to code name of the sheet in the opened workbook but it highlights the code name and says variable not defined.

This is the sub I try and run
VBA Code:
Sub AddYP()
Application.DisplayAlerts = False
Dim newyp As String, wb1 As Workbook
    'Assign the name in D5 of the Tracker sheet to the variable newyp
    newyp = Tracker.Cells(5, 4)
    'Check if List file is open and if not, open it
    If Not isFileOpen("List.xlsm") Then
        Set wb1 = Workbooks.Open(ThisWorkbook.Path & "\" & "Young People" & "\" & "List.xlsm")
    End If
    With ActiveWorkbook
        'Offset from the bottom of the list of YP by 1 to add the newyp to the list
        NamesList.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = newyp
            'Call CreateWB sub and send the variable newyp to it
            Call CreateWB(newyp)
        'Define a new named range for tblYPNames by resizing the current range by _
        one row down
        ThisWorkbook.Names.Add Name:="tblYPNames", _
        RefersTo:=NamesList.Range("tblYPNames").Resize(NamesList.Range("tblYPNames").Rows.Count + 1)
            Tracker.cboYP.ListFillRange = "tblYPNames"
    End With
    'Sort the YP names list in alphabetical order
    Call Sort
Application.DisplayAlerts = True
End Sub

Could someone help me please?
 
If the code referss to wb1, you will need
Rich (BB code):
wb2.Names.Add Name:="tblYPNames", _
        RefersTo:=wb1.Range("tblYPNames").Resize(wb1.Range("tblYPNames").Rows.Count + 1)
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I forgot to mention the error is
method range of object global failed
 
Upvote 0
If the code referss to wb1, you will need
Rich (BB code):
wb2.Names.Add Name:="tblYPNames", _
        RefersTo:=wb1.Range("tblYPNames").Resize(wb1.Range("tblYPNames").Rows.Count + 1)
The named range and the list of names is in wb2
 
Upvote 0
We are certainly doing this in dribs and drabs.

I am assuming the With statement is supposed to refer to the List workbook so change it back to wb2.
Assuming the range is meant to refer to wb2 then unless you put a "." in front of the Range the With is not doing anything.

VBA Code:
    With wb2.Sheets(1)
        'Offset from the bottom of the list of YP by 1 to add the newyp to the list
        .Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = newyp
            'Call CreateWB sub and send the variable newyp to it
            Call CreateWB(newyp, wb1)
    End With
 
Upvote 0
Oops a full stop in front of Rows.Count too. (since the default if the active workbook which wb2 is at that point, it shouldn't make a difference but it will be clearer)
 
Upvote 0
This is what I have now.

VBA Code:
Sub AddYP()
Application.DisplayAlerts = False
Dim newyp As String, wb1 As Workbook, wb2 As Workbook
    'Assign the name in D5 of the Tracker sheet to the variable newyp
    newyp = Tracker.Cells(5, 4)
    'Check if List file is open and if not, open it
    Set wb1 = ThisWorkbook
    If Not isFileOpen("List.xlsm") Then
        Set wb2 = Workbooks.Open(ThisWorkbook.Path & "\" & "Young People" & "\" & "List.xlsm")
    End If
    Set wb2 = Workbooks("List")
    With wb2.Sheets(1)
        'Offset from the bottom of the list of YP by 1 to add the newyp to the list
        .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0) = newyp
            'Call CreateWB sub and send the variable newyp to it
            Call CreateWB(newyp, wb1)
    End With
    
    
        'Define a new named range for tblYPNames by resizing the current range by _
        one row down
        wb2.Names.Add Name:="tblYPNames", _
        RefersTo:=wb2.Worksheets(1).wb2.Range("tblYPNames").Resize(Range("tblYPNames").Rows.Count + 1)
        wb1.Worksheets("Tracker").cboYP.ListFillRange = "tblYPNames"
    
    'Sort the YP names list in alphabetical order
    Call Sort
Application.DisplayAlerts = True
End Sub

I am not feeling that great today so my ability to create meaningful code is not that good.

With this code, I get the error Method range of object global failed.
The following code is highlighted
VBA Code:
        wb2.Names.Add Name:="tblYPNames", _
        RefersTo:=wb2.Worksheets(1).wb2.Range("tblYPNames").Resize(Range("tblYPNames").Rows.Count + 1)



I think the error occurred as there is no named range already defined in the list workbook.

I also want to add in code to create the list file if it does not exist.
 
Upvote 0
I changed and added this:-

VBA Code:
Dim ws2 as Worksheet
Set ws2 = wb2.Sheets(1)
    
        wb2.Names.Add Name:="tblYPNames", _
        RefersTo:=ws2.Range("tblYPNames").Resize(ws2.Range("tblYPNames").Rows.Count + 1)
        wb1.Worksheets("Tracker").cboYP.ListFillRange = ws2.Range("tblYPNames")
 
Upvote 0
If you have a workbook and the codename of a worksheet, this UDF will return that sheet
VBA Code:
Function WorksheetCodeNamed(aCodeName As String, Optional wb As Workbook) As Worksheet
    If wb Is Nothing Then Set wb = ThisWorkbook
    With wb
        Set WorksheetCodeNamed = .Sheets(.VBProject.VBComponents(aCodeName).Properties("Index"))
    End With
End Function
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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