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?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Are you using Option Excplicit at the start of the codes ??
If so, it means you haven't declared the variable coe name ??
What line is it erroring on ?
 
Upvote 0
This is my code 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
    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)
    End With
        'Define a new named range for tblYPNames by resizing the current range by _
        one row down
        wb2.Names.Add Name:="tblYPNames", _
        RefersTo:=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 get the error now of object variable or with block variable not set with this line highlighted
VBA Code:
With wb2.Sheets(1)
 
Upvote 0
I changed the the line to
VBA Code:
With Workbooks("List").Sheets(1)

and it works fine.

My code is 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
    With Workbooks("List").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:=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

Now when I run it, I get an error Method range of object global failed with this line highlighted
VBA Code:
        wb2.Names.Add Name:="tblYPNames", _
        RefersTo:=Range("tblYPNames").Resize(Range("tblYPNames").Rows.Count + 1)
 
Upvote 0
By the way, I am trying to move the list I had made on a separate sheet to a new workbook. I am not sure what parts of the code I will need to update.
 
Upvote 0
The error is gone but now I get another error

This is my sub
Excel Formula:
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
    With Workbooks("List").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).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

It halts in this code and on the line
VBA Code:
Workbooks.Add.SaveAs wb1.Path & "\Young People\" & newyp, FileFormat:=52

VBA Code:
Sub CreateWB(newyp As String, wb1 As Workbook)
Dim V
    Call CheckFolderExists
    Workbooks.Add.SaveAs wb1.Path & "\Young People\" & newyp, FileFormat:=52
    For Each V In Split("7 8 9 10 11 12 1 2 3 4 5 6")
        Sheets.Add(, Sheets(Sheets.Count)).Name = MonthName(V)
    Next
    Call SetupSheets
    Sheets("sheet1").Delete
ActiveWorkbook.Close savechanges:=True
Tracker.Cells(5, 4).Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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