The code to disable saving worked perfectly when I place it under Workbook_Open! Maybe you can help me decipher the problem in the rest of my userform code. The main purpose of the form is to request user input and then save the workbook based on the values of those inputs. When I run a test to see if the input boxes are empty, a message box should warn the user if they are empty. Instead, the form does nothing.
My other issues are in the comments of the code. If I said it "should...", that basically means I couldn't get it to work.
MANY THANKS!!!
Private Sub CommandButton2_Click()
Dim N, A, M, Y, Msg, Style
N = TextBox1.Value
A = Trim(OpArea.Value)
M = Trim(Month.Value)
Y = Trim(Year.Value)
'Should display an message box if any of the input boxes are empty
If TextBox1.Value = "" Or OpArea.Value = "" Or Month.Value = "" Or Year.Value = "" Then
Msg = "Please fill in each box before saving"
Style = vbOKOnly
'Saves the Workbook with the input box entries and closes the form.
Else
ActiveWorkbook.SaveAs N & "_" & A & "_" & M & "_" & Y
UserForm1.Hide
End If
'Should disable the save option if the "x" is clicked to close the form or if the userform hides
Dim CmdBar1 As CommandBar
Dim CmdBar2 As CommandBar
Set CmdBar1 = Excel.CommandBars("Worksheet Menu Bar").Controls("File").CommandBar
For I = 1 To CmdBar1.Controls.Count
CtrlName = CmdBar1.Controls(I).Caption
If CtrlName = "&Save" Or Left(CtrlName, 4) = "Save" Then
CmdBar1.Controls(I).Enabled = False
End If
Next I
Set CmdBar2 = Excel.CommandBars("Standard")
CmdBar2.Controls("Save").Enabled = False
End Sub
'Tied to j-walk code for displaying a file browser
Private Sub CommandButton3_Click()
Dim Msg As String
Msg = "Please select a location for the backup."
MsgBox GetDirectory(Msg)
End Sub
'Should enable the save option when the form is opened, allowing CommandButton2 to save the form
Private Sub UserForm_Activate()
Dim CmdBar1 As CommandBar
Dim CmdBar2 As CommandBar
Set CmdBar1 = Excel.CommandBars("Worksheet Menu Bar").Controls("File").CommandBar
For I = 1 To CmdBar1.Controls.Count
CtrlName = CmdBar1.Controls(I).Caption
If CtrlName = "&Save" Or Left(CtrlName, 4) = "Save" Then
CmdBar1.Controls(I).Enabled = True
End If
Next I
Set CmdBar2 = Excel.CommandBars("Standard")
CmdBar2.Controls("Save").Enabled = True
'Establishes values for the combo boxes
With OpArea
.AddItem "AnadarkoOK"
.AddItem "AnadarkoTX"
.AddItem "ETXSouth"
.AddItem "ETXNorth"
.AddItem "Gloria"
.AddItem "NTXEast"
.AddItem "NTXWest"
.AddItem "MidLa"
.AddItem "Mississippi"
With Month
.AddItem "January"
.AddItem "February"
.AddItem "March"
.AddItem "April"
.AddItem "May"
.AddItem "June"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "October"
.AddItem "November"
.AddItem "December"
With Year
.AddItem "2005"
.AddItem "2006"
.AddItem "2007"
.AddItem "2008"
.AddItem "2009"
.AddItem "2010"
.AddItem "2011"
.AddItem "2012"
.AddItem "2013"
.AddItem "2014"
.AddItem "2015"
.AddItem "2016"
.AddItem "2017"
.AddItem "2018"
.AddItem "2019"
.AddItem "2020"
End With
End With
End With
End Sub
Bookmarks