Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual Basic BeforeSave Sub 1

Status
Not open for further replies.

Clyde38

Electrical
Oct 31, 2003
533
I have included an automatic save subroutine using the Private Sub Workbook_BeforSave.... funciton in the VBA editor. I am using it to save to a particular path and file name. This works fine until I add a new worksheet to the workbook. All other additions or changes to the existing workbook will save automatically when I save the the workbook. What's up? See below..
I do not do many VBA programs. This is a variation of an Excel tip that I read.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Dim rngPullName As Range
Dim strSavePath As String
Application.DisplayAlerts = False
Set rngPullName = ThisWorkbook.Sheets(5).Range("A4")
strSavePath = "C:\Spreadsheets\myfilename\"

ActiveWorkbook.SaveAs Filename:=strSavePath & rngPullName & ".xls"
If ThisWorkbook.Saved = True Then
Cancel = True
End If
Application.DisplayAlerts = True

End Sub
 
Replies continue below

Recommended for you

Suspect that this may be your culprit:
Code:
Set rngPullName = ThisWorkbook.Sheets(5).Range("A4")
If your new sheet shows on the tabs before position 5 then rngPullName may well be empty. Use the sheet name instead
Code:
Set rngPullName = ThisWorkbook.Sheets("sheet5").Range("A4")
substituting the actual sheetname of course

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

Steam Engine enthusiasts:
 
Thanks - You're welcome!

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

Steam Engine enthusiasts:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor