Results 1 to 3 of 3

Thread: [RESOLVED] Active Workbook name Reference error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    24

    Resolved [RESOLVED] Active Workbook name Reference error

    I have created a template named “Stock Quantity Calculator 2007 Template 10K a2.xltm” with many macro in it. When opening a new workbook with this template I am getting errors because some of the code is looking for the template name not the newly created workbook name.

    What would be the code to replace the original workbook name with the current\active workbook name? As you can see it occurs (2) times in this one macro.



    Sub O_All_Button()
    '
    ' O_All_Button Macro
    '

    '
    Application.Run _
    "'Stock Quantity Calculator 2007 Template 10K a2.xltm'!UnHide_all"

    Sheets("0 Usage Data By Sto-Loc").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Branch Numbers").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Format Data MRP ").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Upload MRP").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Upload MRP RDC").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Format Data RDC").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("0 Stock Cost").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Input Cost Data").Select
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("STO Cost").Select
    ActiveWindow.SelectedSheets.Visible = False

    Application.Run _
    "'Stock Quantity Calculator 2007 Template 10K a2.xltm' O_Usage_All_SLoc"

    Sheets("0 Usage Data ALL Sto-Loc").Select
    ActiveWindow.SelectedSheets.Visible = False
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Active Workbook name Reference error

    when working with multiple workbooks or as this case a workbook created from a template, you should avoid all references to select, activesheet or active anything else,
    use fully qualified paths to the required ranges or sheets

    most of the above code can be converted like
    vb Code:
    1. sheets("some sheet").visible = false
    if working with multiple workbooks the workbook should also be specified
    if the code applies to a workbook opened from a template, possibly use
    vb Code:
    1. thisworkbook.sheets("some sheet").visible = false

    recording of macros is great to generate sample code, but it needs some conversion for real applications
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    24

    Re: Active Workbook name Reference error

    Thanks for your reply.

    The fix to the name issue was to remove this code

    Application.Run _
    "'Stock Quantity Calculator 2007 Template 10K a2.xltm'!UnHide_all"

    And replace with

    Call UnHide_all

    Worked Great!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width