Virginia632
New Member
- Joined
- Sep 12, 2023
- Messages
- 5
- Office Version
- 2019
- Platform
- Windows
I was getting an "Out of Memory" error after having my userform open too long so I unload the form every time the user clicks btnEnterResetNext. Now my problem is that I have to use a global variable to reopen the form in the right place. Is there a way of passing a variable that maintains its value when passed to the Initialize procedure? I tried using the .tag property but it keeps resetting to "" when Initialize is called.
The form populates a combobox with the start dates of all the pay periods
When the user clicks btnEnterResetNext the form enters the data onto the worksheet, resets the userform, and then should unload the form and reopen it and set the ListIndex to the one after the one previously selected. Right now I increment PayPeriodIndex (a global variable) which works but I want to get away from global variables.
The form populates a combobox with the start dates of all the pay periods
When the user clicks btnEnterResetNext the form enters the data onto the worksheet, resets the userform, and then should unload the form and reopen it and set the ListIndex to the one after the one previously selected. Right now I increment PayPeriodIndex (a global variable) which works but I want to get away from global variables.
VBA Code:
Private Sub btnEnterResetNext_Click()
'ensure a pay period has been selected first
If cmbDayInPay.ListCount Then
'enter the data
EnterData
'Reset the form
ResetFormValues
'If the last day isn't selected, select the next day
If (cmbDayInPay.ListIndex) < (cmbDayInPay.ListCount - 1) Then
cmbDayInPay.ListIndex = cmbDayInPay.ListIndex + 1
'Else tell the user they've finished with that pay period, and moving on to the next one
ElseIf cmbPayPeriod.ListIndex < cmbPayPeriod.ListCount - 1 Then
MsgBox "You have finished with this paystub. Selecting the next one."
PayPeriodIndex = cmbPayPeriod.ListIndex + 1
Unload Me
frmPayStubEntry.Show
'If the last Pay Period is selected send a message to the user
Else
MsgBox "That was the last paystub for the year"
Unload Me
End If
Else
MsgBox "Select a Pay Period first."
End If
End Sub
Private Sub UserForm_Initialize()
Dim strDate As String
'Populate the Pay Periods with dates
Dim i As Integer
For i = 1 To Sheets.Count - 2
'get the string to add to the combo box
strDate = Format(Sheets(i).Range("Pay_Period").Value, "dd-mmm-yyyy")
'add it to the combo box
cmbPayPeriod.AddItem strDate
Next i
cmbPayPeriod.ListIndex = PayPeriodIndex
End Sub