This is the first userform I've done and it works but I would appreciate someone in the know looking at it and suggesting how it could be better. I've looked at various reference books and online sites and feel like I need a userforms for dummies book. I'm using a combo box to allow the user to select a month. I have a Cancel button that unloads the form and a Continue button that for some reason requires two clicks to proceed. So I know something is not quite right. This is similar to some of the other post but not the same. My macro when complete will take an exported report and plug the data into the proper columns based on the user selecting the month in the userform. Any suggestions to improve my knowledge will be greatly appreciated. My code follows.
[highlight #FCE94F]Edit - Based on suggestions provided by Combo I have edited the code as shown in the highlighted areas.[/highlight]
[highlight #FCE94F]Edit - Based on suggestions provided by Combo I have edited the code as shown in the highlighted areas.[/highlight]
Code:
Public Sub cboMonths_Change()
' Combo box
End Sub
__________________________________________________________
Public Sub cmdCancel_Click()
Unload sMonth
End Sub
____________________________________________________________
Public Sub cmdContinue_Click()
strMonth = sMonth.cboMonths.Value
sMonth.Hide
End Sub
_________________________________________________________________
Private Sub Label2_Click()
[highlight #FCE94F]' I got rid of this, it wasn't needed[/highlight]
End Sub
____________________________________________________________________________
Public Sub UserForm_Initialize()
ufMonths = Array("January", "February", "March", "April", "May", "June", _
"July", "August", "September", "October", "November", "December")
sMonth.cboMonths.List = ufMonths
With sMonth
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
' .Show [highlight #FCE94F]I got rid of this line, it was redundant[/highlight]
End With
End Sub
_________________________________________________________________________
Global strMonth As String 'Procedure starts here
_________________________________________________________________________
Public Sub ImportGroupData()
Dim Finfo As String ' Used for file extension filters
Dim FilterIndex As Integer ' Used to indicate default file extension
Dim Title As String ' Used to hold the file dialog title text
Dim FPath As Variant ' Holds the file name and path that is selected
Dim FName As Variant ' Holds the name of the file that is selected
Dim dMonth As String ' Holds the name of the month the data applies to
' Allows the user to use the File Open dialog to select a file.
' Set up list of file extension filters for the file type drop down,
' I used wild card only. Others could be added.
Finfo = "All Files (*.*),*.*"
' Display All Files by default. If you have more extensions in the above list
' the FilterIndex determines what number in the list shows by default.
FilterIndex = 1
' Set the dialog box title caption
Title = "Select the file to import"
' Get the filename
FPath = Application.GetOpenFilename(Finfo, _
FilterIndex, Title)
' Handle return info from dialog box
If FPath = False Then
MsgBox "No file was selected."
End If
' Exit macro if no file is selected
If FPath = False Then Exit Sub
' Grabs the file name from the full path
FName = Dir(FPath)
' Open the selected file
Workbooks.Open FileName:=FName
' Make the workbook active
Windows(FName).Activate
' Open a userform to select a month for the data to apply to
sMonth.Show
dMonth = strMonth
' Check if userform is still in memory before unloading it. [highlight #FCE94F]I added this[/highlight][highlight #FCE94F][/highlight]
If Not sMonth Is Nothing Then
Unload sMonth
End If
' Code will continue here
End Sub