Run a SaveAs formula then delete SaveAs formula

dharv3y78

New Member
Joined
Oct 26, 2019
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi Mr Excel Experts,

Im looking at a way that will enable me to run the following VBA Sub which works and does the SaveAs and opens in the new document but what i want is for the next step is to either Delete or disable the Sub in the new document and leave the orignal document alone. Is this something that can be done????

VBA Code:
Private Sub Workbook_Open()
Dim FileSaveAsName As Variant
Dim intchoice As Integer
Static saveProcess As Boolean
Application.EnableEvents = False
ActiveWindow.ScrollRow = 1
        Set FileSaveName = Application.fileDialog(msoFileDialogSaveAs)

        FileSaveName.InitialFileName = ThisWorkbook.Name
        FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
        intchoice = FileSaveName.Show
        If intchoice <> 0 Then
            FileSaveName.Execute
        End If

Application.EnableEvents = True
End Sub

TIA for any assistance

Thanks
Donald
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You cannot disable specific Sub procedures - it is a matter of disabling ALL VBA code or not in the file as you open the file.
You cannot control that by VBA. Think about it, if that were possible, people could "force" you to run malicious VBA code.

I think it would be better instead of doing a SaveAs, just copy your data over to a new workbook with VBA code, and then save that.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:


There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:


There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
Hi RoryA,

Sorry for not following the rules and coming back to say that this post was cross-posted. My applogies.

Donald
 
Upvote 0
I think it would be better instead of doing a SaveAs, just copy your data over to a new workbook with VBA code, and then save that.
Hi MrExcel Experts

I have a follow to Joe4 suggestion where i am running a SaveAs VBA formula in Macro1 and i keep getting errors in runnging i'm not sure why?? I have code that already runs from ThisWorkbook which works exactly how i want it. And while the code are nearly identical they are different in what they do.

In ThisWorkbook i have the VBA code run just Userform2 then bring up the SaveAs diglog box if A2 equals the ComputerName (This Code Works)

VBA Code:
Private Sub Workbook_Open()
'Application.Visible = True
    Dim FileSaveName As Variant
    Dim intchoice As Integer
If Sheet1.Range("A2") = Environ$("Computername") Then
    Userform2.Show
    Unload Userform2
    If UCase(Me.Name) = UCase("Book1.xlsm") Then
        Application.EnableEvents = False
            Set FileSaveName = Application.FileDialog(msoFileDialogSaveAs)
            FileSaveName.InitialFileName = Me.Name
            FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
            intchoice = FileSaveName.Show
                If intchoice <> 0 Then
                    FileSaveName.Execute
                End If
            Application.EnableEvents = True
    End If
End If
End Sub

In Macro1 i have the VBA code run bothUserForm1 and Userform2 if A1 and A2 equals blank then i want to bring up the SaveAs diglog box after this but this code is bringing up a Compile error in hidden module1 is there somthing that that i am missing and i can fix this to do what i need it to do. (This Code has a Compile Error)

VBA Code:
Option Explicit
Private Sub Auto_Open()
    Dim FileSaveName As Variant
    Dim intchoice As Integer
With Sheet1
    .Visible = xlSheetVeryHidden
    If .Range("A2") = "" Then
        .Range("A1") = ""
        .Range("A2") = Environ$("ComputerName")
        Application.Visible = False
        Userform1.Show
        Userform2.Show
    If UCase(Me.Name) = UCase("Book1.xlsm") Then
        Application.EnableEvents = False
            Set FileSaveName = Application.FileDialog(msoFileDialogSaveAs)
            FileSaveName.InitialFileName = Me.Name
            FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
            intchoice = FileSaveName.Show
        If intchoice <> 0 Then
            FileSaveName.Execute
        End If
        Application.EnableEvents = True
    End If
End If
    ElseIf .Range("A2") <> Environ$("Computername") Then
        MsgBox "THIS FILE IS NOT LICENCED FOR THIS PC", vbInformation, ""
        If Application.Workbooks.Count > 1 Then
            Application.DisplayAlerts = False
            ThisWorkbook.Close
            Application.DisplayAlerts = True
        Else
            ThisWorkbook.Save
            Application.Quit
        End If
    End If
End With
End Sub

Thanks
Donald
 
Upvote 0
You cannot have an ELSEIF after you have closed ALL IFs when END IFs.
So I think you need to remove the "End If" before the "ElseIf".
This would have been easier to see if your code was lined up properly.

This does not return any Complile Errors:
VBA Code:
Private Sub Auto_Open()

Dim FileSaveName As Variant
Dim intchoice As Integer

With Sheet1
    .Visible = xlSheetVeryHidden
    If .Range("A2") = "" Then
        .Range("A1") = ""
        .Range("A2") = Environ$("ComputerName")
        Application.Visible = False
        Userform1.Show
        Userform2.Show
        If UCase(Me.Name) = UCase("Book1.xlsm") Then
            Application.EnableEvents = False
            Set FileSaveName = Application.FileDialog(msoFileDialogSaveAs)
            FileSaveName.InitialFileName = Me.Name
            FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
            intchoice = FileSaveName.Show
            If intchoice <> 0 Then
                FileSaveName.Execute
            End If
            Application.EnableEvents = True
        End If
    ElseIf .Range("A2") <> Environ$("Computername") Then
        MsgBox "THIS FILE IS NOT LICENCED FOR THIS PC", vbInformation, ""
        If Application.Workbooks.Count > 1 Then
            Application.DisplayAlerts = False
            ThisWorkbook.Close
            Application.DisplayAlerts = True
        Else
            ThisWorkbook.Save
            Application.Quit
        End If
    End If
End With

End Sub
 
Upvote 1
This does not return any Complile Errors:
Hi Joe4

I'm now getting a Complie Error: Invaild use of Me Keyword in the Bold Section and all i did was change the Sheet from sheet1 to sheet3 and changed the UCase("Book1.xlsm") to my file name i can't see how you didn't get any Compile Errors and i'm getting this one:

Rich (BB code):
Private Sub Auto_Open()

Dim FileSaveName As Variant
Dim intchoice As Integer

With Sheet1
    .Visible = xlSheetVeryHidden
    If .Range("A2") = "" Then
        .Range("A1") = ""
        .Range("A2") = Environ$("ComputerName")
        Application.Visible = False
        Userform1.Show
        Userform2.Show
        If UCase(Me.Name) = UCase("Book1.xlsm") Then
            Application.EnableEvents = False
            Set FileSaveName = Application.FileDialog(msoFileDialogSaveAs)
            FileSaveName.InitialFileName = Me.Name
            FileSaveName.FilterIndex = 2   'select to save with a ".xlsm" extension
            intchoice = FileSaveName.Show
            If intchoice <> 0 Then
                FileSaveName.Execute
            End If
            Application.EnableEvents = True
        End If
    ElseIf .Range("A2") <> Environ$("Computername") Then
        MsgBox "THIS FILE IS NOT LICENCED FOR THIS PC", vbInformation, ""
        If Application.Workbooks.Count > 1 Then
            Application.DisplayAlerts = False
            ThisWorkbook.Close
            Application.DisplayAlerts = True
        Else
            ThisWorkbook.Save
            Application.Quit
        End If
    End If
End With

End Sub

Thanks
Donald
 
Upvote 0
I just tried compiling the code, not running it, as I do not have your data files, forms, and other VBA code in order to run it.

What exactly are you trying to check with "Me.Name"?
"Me" is not commonly used with VBA code, except maybe in Form code.

If you are trying to confirm the file name, try either this:
VBA Code:
ActiveWorkbook.Name
to get the name of the active workbook, or this:
VBA Code:
ThisWorkbook.Name
to get the name of the workbook that this code is running from.
 
Upvote 0
Solution
What exactly are you trying to check with "Me.Name"?
I was trying that If UCase(Workbook Name) = UCase("Book1.xlsm") then show the SaveAs diglog Box

I have got it work by changing the Me.Name to ActiveWorkbook.Name

Thanks for your assistance

Donald
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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