Consulting

Results 1 to 5 of 5

Thread: Check Name and Save Workbook

  1. #1
    VBAX Contributor
    Joined
    Jun 2015
    Location
    Houston
    Posts
    111
    Location

    Question Check Name and Save Workbook

    Greetings,


    I am getting and error with this code that says: "Invalid Use of Me Keyword".

    Public Sub SaveProject()
    	
        GetBook = MyRange.Parent.Parent.Name
        
        If GetBook = "IBU Installation BOMs - 3.5" Then Call SaveProjectAs
        If Not GetBook = "IBU Installation BOMs - 3.5" Then Me.Save
    
    
    End Sub

    This sub is currently located in Module1. After getting the error, I moved it to the ThisWorkbook module, however, when I tried to run the code it wasn't visible by the button.


    What am I doing wrong?
    Thanks.

    Lord Dragon

    "Discovery consists not in seeking new lands, but in seeing with new eyes." ~ Marcel Proust

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    Presuming you are wanting to save ThisWorkbook (the workbook the code is housed in, regardless of what workbook may have the current focus), then simply use ThisWorkbook.Save

    The Me keyword only works in the Object Module of what you want 'Me' to refer to. For instance when used in the workbook's Object Module (named 'ThisWorkbook' by default), then Me refers to the workbook object. If used in a worksheet's object module, the Me refers to the specific worksheet. Same for Me used in a UserForm's object module, then Me refers to the form.

    This is the reason it falls over with "Invalid use..." in a Standard Module, as a Standard Module does not represent an object. Does that make sense?

    Mark

  3. #3
    VBAX Contributor
    Joined
    Jun 2015
    Location
    Houston
    Posts
    111
    Location
    Ok. That fixed the Me Error part. However, now the workbook is saving, but not running the function it is supposed to run if the name hasn't changed from the original.
    Thanks.

    Lord Dragon

    "Discovery consists not in seeking new lands, but in seeing with new eyes." ~ Marcel Proust

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    All it takes is an extra or missing space in IBU Installation BOMs - 3.5 and it won't match

    But the WB .Name includes the extension


    Public Sub SaveProject()
        Dim GetBook As String
        Dim iDot As Long
        
        GetBook = MyRange.Parent.Parent.Name
         
        iDot = InStrRev(GetBook, ".")
        
        GetBook = Left(GetBook, iDot - 1)
         
        If GetBook = "IBU Installation BOMs - 3.5" Then
            Call SaveProjectAs
        Else
            ThisWorkbook.Save
        End If
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Contributor
    Joined
    Jun 2015
    Location
    Houston
    Posts
    111
    Location
    Paul,

    That fixed it.

    For some reason the "GetBook = MyRange.Parent.Parent.Name" part didn't. But I changed that to "GetBook = ThisWorkbook.Name" and it worked fine.
    Thanks.

    Lord Dragon

    "Discovery consists not in seeking new lands, but in seeing with new eyes." ~ Marcel Proust

Posting Permissions

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