vinylspin02
New Member
- Joined
- Jul 13, 2011
- Messages
- 1
I'm currently working on an excel file for work and have my current code for a button below. I have it doing a couple of things: SaveAs once a button is pushed with a confirmation MSG box, eliminating the pop-up box that ask about recalculting the cells, and I just have it saving under a certain name in a certain location. I also have it hiding the sheet once the button is pushed.
I'd like to be able to only have it save if certain cells are filled out (A2, B6, C10) for example. I'd also want to displace a error message if they aren't and not allow the SaveAs to happen. If I can do this, I can eliminate the confirmation msg box all together. I'm having a good bit of trouble with this as I'm brand new to this and have just pieced together the code below. Thanks for any and all help.
Sub Saveasfilename2()
Dim folder, cvalue, cvalue1, cvalue2, NewFname As String
folder = "C:\Users\James Kimbrell\Documents\"
cvalue = Sheets(3).Range("G4").Value
cvalue1 = Sheets(3).Range("D58").Value
cvalue2 = Sheets(3).Range("G58").Value
Answer = MsgBox("Do you want to continue ?", _
vbOKCancel, "Form Submission Confirmation")
If Answer = vbCancel Then Exit Sub ' the macro ends if the user selects the CANCEL-button
If Answer = vbOK Then NewFname = cvalue & "_" & "Rxn_" & cvalue1 & "-" & cvalue2 & "_" & Format(Date, "mmddyy") & ".xls"
Application.DisplayAlerts = False
Dim colSheetsToHide As Collection
Dim wks As Worksheet
' collection of sheets to hide when workbook is saved
Set colSheetsToHide = New Collection
With colSheetsToHide
.Add Sheets("Poly 2")
End With
' hide worksheets in collection
For Each wks In colSheetsToHide
wks.Visible = False
Next wks
ActiveWorkbook.SaveAs Filename:=NewFname, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Application.DisplayAlerts = True
End Sub
I'd like to be able to only have it save if certain cells are filled out (A2, B6, C10) for example. I'd also want to displace a error message if they aren't and not allow the SaveAs to happen. If I can do this, I can eliminate the confirmation msg box all together. I'm having a good bit of trouble with this as I'm brand new to this and have just pieced together the code below. Thanks for any and all help.
Sub Saveasfilename2()
Dim folder, cvalue, cvalue1, cvalue2, NewFname As String
folder = "C:\Users\James Kimbrell\Documents\"
cvalue = Sheets(3).Range("G4").Value
cvalue1 = Sheets(3).Range("D58").Value
cvalue2 = Sheets(3).Range("G58").Value
Answer = MsgBox("Do you want to continue ?", _
vbOKCancel, "Form Submission Confirmation")
If Answer = vbCancel Then Exit Sub ' the macro ends if the user selects the CANCEL-button
If Answer = vbOK Then NewFname = cvalue & "_" & "Rxn_" & cvalue1 & "-" & cvalue2 & "_" & Format(Date, "mmddyy") & ".xls"
Application.DisplayAlerts = False
Dim colSheetsToHide As Collection
Dim wks As Worksheet
' collection of sheets to hide when workbook is saved
Set colSheetsToHide = New Collection
With colSheetsToHide
.Add Sheets("Poly 2")
End With
' hide worksheets in collection
For Each wks In colSheetsToHide
wks.Visible = False
Next wks
ActiveWorkbook.SaveAs Filename:=NewFname, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
Application.DisplayAlerts = True
End Sub