This is long and I am sorry but I am desperate. Thanks in advance
I have a control userform with various buttons to open different userforms
In this case - I click the button to open the "Add New Account" userform - AddNewAccount userform opens via this code
AddNewAccount
IF I select "Add New..." in the combobox, I have an exit event to close AddNewAccount and open "NewAccountOrDetailType" via this code
NewAccountOrDetailType (note - the checkbox referred to in the previous code is not visible. It determines if a new Account is being added or a new Detail is being added)
Here is the code for both buttons on the userform. The issue I am having is - If cancel is clicked on NewAccountOrDetail and then again on AddNewAccount, NewAccountorDetail is being showed again. Close it and AddNewAccountUserform freezes. Have to hard crash excel. If the create new account button is clicked, it should unload me and show Addnewaccount again but it shows NewAccountDetailType again, close it again and AddNewAccount is shown but frozen - once again, have to hard crash Excel. I am absolutely confused.
I have a control userform with various buttons to open different userforms
In this case - I click the button to open the "Add New Account" userform - AddNewAccount userform opens via this code
Code:
Private Sub btnNewAccount_Click()
Unload ControlPanel
AddNewAccount.Show
End Sub
IF I select "Add New..." in the combobox, I have an exit event to close AddNewAccount and open "NewAccountOrDetailType" via this code
Code:
Private Sub cmbSelectAccountType_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim strAccountType As String
strAccountType = cmbSelectAccountType.Value
If Not strAccountType = "Add New..." Then
GoTo EndSubRoutine
Else
Unload Me
End If
Load NewAccountOrDetailType
NewAccountOrDetailType.chkNewAccountType.Value = True
NewAccountOrDetailType.chkNewDetailType.Value = False
NewAccountOrDetailType.lblTitle.Caption = "Enter Name for New Account"
NewAccountOrDetailType.Caption = "New Account"
NewAccountOrDetailType.btnSave.Caption = "Create New Account"
NewAccountOrDetailType.Show
EndSubRoutine:
End Sub
NewAccountOrDetailType (note - the checkbox referred to in the previous code is not visible. It determines if a new Account is being added or a new Detail is being added)
Here is the code for both buttons on the userform. The issue I am having is - If cancel is clicked on NewAccountOrDetail and then again on AddNewAccount, NewAccountorDetail is being showed again. Close it and AddNewAccountUserform freezes. Have to hard crash excel. If the create new account button is clicked, it should unload me and show Addnewaccount again but it shows NewAccountDetailType again, close it again and AddNewAccount is shown but frozen - once again, have to hard crash Excel. I am absolutely confused.
Code:
Private Sub btnCancel_Click()
Unload NewAccountOrDetailType
AddNewAccount.Show
End Sub
Code:
Private Sub btnSave_Click()
Dim lngConfigRows As Long, lngConfigNewRow As Long
Dim strAccountOrDetail As String, strName As String, strColumnLetter As String, strConfigRange As String, strComboboxName As String, msg As String
Dim wsConfig As Worksheet
Dim rngNameFound As Range
Dim cmb As MSForms.ComboBox
Set wsConfig = Sheets("Configuration")
strName = txtAccountName.Value
If strName = "" Then
MsgBox ("Name required for new " & strAccountOrDetail & ".")
GoTo EndSubRoutine
End If
If chkNewDetailType.Value = True Then
strAccountOrDetail = "Detail"
Else
strAccountOrDetail = "Account"
End If
strComboboxName = "cmbSelect" & strAccountOrDetail & "Type"
If strAccountOrDetail = "Account" Then
strColumnLetter = "D"
ElseIf strAccountOrDetail = "Detail" Then
strColumnLetter = "E"
End If
lngConfigRows = wsConfig.Range(strColumnLetter & "1048576").End(xlUp).Row
strConfigRange = strColumnLetter & "2:" & strColumnLetter & lngConfigRows
Set rngNameFound = wsConfig.Range(strConfigRange).Find(what:=strName, LookIn:=xlFormulas, lookat:=xlWhole)
If Not (rngNameFound Is Nothing) Then
msg = MsgBox("This " & strAccountOrDetail & " already exists.", vbCritical, strAccountOrDetail & " already exists")
GoTo EndSubRoutine
End If
lngConfigNewRow = lngConfigRows + 1
wsConfig.Range(strColumnLetter & lngConfigNewRow).Value = strName
strConfigRange = strColumnLetter & "2:" & strColumnLetter & lngConfigNewRow
ActiveWorkbook.Worksheets("Configuration").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Configuration").Sort.SortFields.Add Key:=Range(strColumnLetter & "1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Configuration").Sort
.SetRange Range(strConfigRange)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Unload Me
AddNewAccount.Show
EndSubRoutine:
End Sub
Last edited: