×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Visual Basic BeforeSave Sub

Visual Basic BeforeSave Sub

Visual Basic BeforeSave Sub

(OP)
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

RE: Visual Basic BeforeSave Sub

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: www.essexsteam.co.uk

RE: Visual Basic BeforeSave Sub

(OP)
johnwm,
Exactly! Thanks alot!
thumbsup2

RE: Visual Basic BeforeSave Sub

Thanks - You're welcome!

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

Steam Engine enthusiasts: www.essexsteam.co.uk

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources