PDA

View Full Version : [SOLVED] Check Name and Save Workbook



LordDragon
10-08-2015, 09:38 AM
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?

GTO
10-08-2015, 09:32 PM
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

LordDragon
10-09-2015, 06:39 PM
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.

Paul_Hossler
10-09-2015, 06:59 PM
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

LordDragon
10-10-2015, 11:16 AM
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.