Оригинальный вопрос:
У меня есть пользовательская форма, которая открывается, вводится некоторая информация, и есть кнопка отмены и OK. Когда я нажимаю "Отмена", он выгружает пользовательскую форму и закрывает окно, затем снова открывается пустое окно?
Обновление:
Я думал, что просто что-то испортил, и копирование и вставка всего кода в новую рабочую книгу будет работать. Казалось, для некоторого начального тестирования, и теперь он делает то же самое снова.
Я предполагаю, что это как-то связано со всеми подпрограммами textbox_exit?
На главном рабочем листе есть кнопка форматирования, которая вызывает userform1. Некоторая информация введена, и варианты кнопки - Отмена и OK. Отмена должна просто выгрузить userform1 и скрыть его. ОК переходит к userform3. (userform2 был удален, и я никогда не переименовывал их)
Userform3 вводится дополнительная информация, и есть Отмена и снова OK. OK должен запустить остальную часть макроса, и отмена должна выгрузить и скрыть userform3.
Когда я нажимаю любую кнопку на пользовательской форме3, она выгружает форму (ожидается), затем форма закрывается и сразу же открывается пользовательская форма1 (неожиданная / нежелательная)
когда я нажимаю кнопку ОК на пользовательской форме1 Отмена пользовательской формы1 выгружается и исчезает, а затем сразу же снова открывается (выгружено / пусто)?
Исправлено, но есть вопросы.
Как было упомянуто в комментариях ниже, это было скопировано в новую рабочую книгу, и это, казалось, исправило себя. Затем по какой-то причине он снова начал делать то же самое. Я ушел с работы в пятницу и дважды подумал об этом. Сейчас в понедельник вроде работает? Кто-нибудь знает, что могло вызвать необходимость дважды нажать «Отмена», чтобы форма исчезла?
вот код на userform1
Option Explicit
Public Sub UserForm_Initialize()
'centers the userform on the active excel screen
Me.StartUpPosition = 0
Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
Me.top = Application.top + (0.5 * Application.Height) - (0.5 * Me.Height)
Sheets("hidden").Range("A2:C2").Clear
End Sub
Private Sub Cancel_UserForm1_Click()
Unload Me
End Sub
Public Sub OK_UserForm1_Click()
Dim chk As Control
Dim c, i, j, k, m As Long
'START DATE populated
If TextBox1 = "" Then
'SEQUENCE populated (get double error at same time rather than 2 individual errors)
If TextBox2 = "" Then
'PROPNUM COLUMN populated (get triple error at same time)
If TextBox8 = "" Then
'all 3 blank
TextBox1.BackColor = RGB(255, 153, 153)
TextBox2.BackColor = RGB(255, 153, 153)
TextBox8.BackColor = RGB(255, 153, 153)
TextBox1.SetFocus
MsgBox "Please enter start date, beginning sequence number, and propnum column"
Exit Sub
Else
'first 2 blank
TextBox1.BackColor = RGB(255, 153, 153)
TextBox2.BackColor = RGB(255, 153, 153)
TextBox1.SetFocus
MsgBox "Please enter start date and beginning sequence number"
Exit Sub
End If
Else
'####################################
'## START DATE is blank ##
'## SEQUENCE filled in correctly ##
'####################################
'PROPNUM COLUMN populated
If TextBox8 = "" Then
TextBox1.BackColor = RGB(255, 153, 153)
TextBox8.BackColor = RGB(255, 153, 153)
TextBox1.SetFocus
MsgBox "Please enter start date and propnum column"
Exit Sub
Else
TextBox1.BackColor = RGB(255, 153, 153)
TextBox1.SetFocus
MsgBox "Please enter start date"
Exit Sub
End If
End If
Else
'####################################
'## START DATE filled in correctly ##
'####################################
'SEQUENCE populated
If TextBox2 = "" Then
'PROPNUM COLUMN populated
If TextBox8 = "" Then
TextBox2.BackColor = RGB(255, 153, 153)
TextBox8.BackColor = RGB(255, 153, 153)
TextBox2.SetFocus
MsgBox "Please enter beginning sequence and propnum column"
Exit Sub
Else
TextBox2.BackColor = RGB(255, 153, 153)
TextBox2.SetFocus
MsgBox "Please enter beginning sequence number"
Exit Sub
End If
Else
'####################################
'## START DATE filled in correctly ##
'## SEQUENCE filled in correctly ##
'####################################
'PROPNUM COLUMN populated
If TextBox8 = "" Then
TextBox8.BackColor = RGB(255, 153, 153)
TextBox8.SetFocus
MsgBox "Please enter the propnum column"
Exit Sub
End If
End If
End If
'checks CheckBox's to see if they are checked (checked = 1, not checked = 0)
For Each chk In Me.Controls
If TypeOf chk Is MSForms.CheckBox Then
If chk = 0 Then
i = i + 1
End If
End If
Next
'if they were all 0 (not checked) this will be 13
If i = 13 Then
MsgBox "Please select at least one keyword category"
Exit Sub
End If
'saves the selected number of keyword categories and start date
Sheets("hidden").Visible = True
Sheets("hidden").Select
Sheets("hidden").Range("A2").Value = TextBox1.text
Sheets("hidden").Range("B2").Value = TextBox2.text
Sheets("hidden").Range("C2").Value = TextBox8.text
'saves the CheckBox keywords
For j = 1 To 13
'what this box checked (1 is checked)
m = 0
If Me.Controls("CheckBox" & j) = 1 Then
Sheets("hidden").Cells((3 + m), 1).Value = Me.Controls("CheckBox" & j).Caption
m = m + 1
End If
Next j
'i is the number of categories that were checked
For c = 1 To 13
'puts the variables into the spreadsheet
With Me
'once all checks are done clear out any previously saved KEYWORDS
If c = 1 Then
k = 4
Sheets("hidden").Visible = True
Sheets("hidden").Select
Range(Cells(k, 1), Cells(k + 9, 3)).Select
Range(Cells(k, 1), Cells(k + 9, 3)).Clear
Sheets("hidden").Cells(4, 3).Value = Sheets("hidden").Cells(2, 2).Value
Else
End If
'KEYWORDS and line continuations get saved
If Me.Controls("CheckBox" & c).Object.Value = True Then
'If c = 1 Or c = 2 Then
Sheets("hidden").Cells(k, 1).Value = .Controls("CheckBox" & c).Caption
Sheets("hidden").Cells(k, 2).Value = .Controls("TextBox" & (k - 1)).text
Sheets("hidden").Cells((k + 1), 3).Value = Sheets("hidden").Cells(k, 2).Value + Sheets("hidden").Cells(k, 3).Value + 1
k = k + 1
End If
End With
Next c
'remove last KSS cell -- it isnt needed
Cells(k, 3).Clear
Unload Me
Unload UserForm3
UserForm3.Show
Sheets("Imported Data").Select
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'checks START DATE if there is something in it, if its blank skip check
If TextBox1 <> "" Then
'START DATE a date
If Not IsDate(TextBox1) Then
TextBox1.BackColor = RGB(255, 153, 153)
TextBox1.SetFocus
MsgBox "Please enter start date in MM/YYYY format"
Exit Sub
Else
If Not IsNumeric(Right(TextBox1.Value, 4)) Then
TextBox1.BackColor = RGB(255, 153, 153)
TextBox1.SetFocus
MsgBox "Please enter start date in MM/YYYY format"
Exit Sub
Else
If Right(TextBox1.Value, 4) > 2050 Then
TextBox1.SetFocus
MsgBox "Are you sure about your date?"
End If
End If
End If
End If
'changes color back to white for correct
TextBox1.BackColor = RGB(255, 255, 255)
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'checks SEQUENCE if there is something in it, if its blank skip check
If TextBox2 <> "" Then
'BEGINNING SEQUENCE a number and not blank
If Not IsNumeric(TextBox2) Then
TextBox2.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a numeric beginning sequence number"
Exit Sub
End If
'BEGINNING SEQUENCE a positive number
If TextBox2 < 1 Then
TextBox2.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a positive beginning sequence"
Exit Sub
Else
'BEGINNING SEQUENCE trim decimals
If TextBox2 > 0 And TextBox2 < 10 Then
TextBox2.Value = Left(TextBox2, 1)
Else
TextBox2.Value = Left(TextBox2, 2)
End If
End If
End If
'changes color back to white for correct
TextBox2.BackColor = RGB(255, 255, 255)
End Sub
Private Sub TextBox8_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'checks PROPNUM COLUMN if it is entered in correct format, if its blank skip check
If TextBox8 <> "" Then
'START DATE a date
If IsNumeric(TextBox8) Then
TextBox8.BackColor = RGB(255, 153, 153)
MsgBox "Please enter propnum column in Y format"
Exit Sub
End If
End If
'changes color back to white for correct
TextBox8.BackColor = RGB(255, 255, 255)
End Sub
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'if the box is blank skip the check
If TextBox3 <> "" Then
'CONTINUATION LINE numeric
If Not IsNumeric(TextBox3) Then
TextBox3.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a number into the UNIQUE GAS continuation line box"
Exit Sub
Else
'CONTINUATION LINE positive
If TextBox3 < 0 Then
TextBox3.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a positive number into the UNIQUE GAS continuation line box"
Exit Sub
Else
'CONTINUATION LINE less than 6
If TextBox3 > 5 Then
TextBox3.BackColor = RGB(255, 153, 153)
MsgBox "Plese enter a number less than 6 into the UNIQUE GAS continuation line box"
Exit Sub
Else
'CONTINUATION LINE whole number
TextBox3.Value = Left(TextBox3, 1)
End If
End If
End If
End If
'CONTINUATION LINE box white for correct
TextBox3.BackColor = RGB(255, 255, 255)
End Sub
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'if the box is blank skip the check
If TextBox4 <> "" Then
'CONTINUATION LINE numeric
If Not IsNumeric(TextBox4) Then
TextBox4.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a number into the OIL continuation line box"
Exit Sub
Else
'CONTINUATION LINE positive
If TextBox4 < 0 Then
TextBox4.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a positive number into the OIL continuation line box"
Exit Sub
Else
'CONTINUATION LINE less than 6
If TextBox4 > 5 Then
TextBox4.BackColor = RGB(255, 153, 153)
MsgBox "Plese enter a number less than 6 into the OIL continuation line box"
Exit Sub
Else
'CONTINUATION LINE whole number
TextBox4.Value = Left(TextBox4, 1)
End If
End If
End If
End If
'CONTINUATION LINE box white for correct
TextBox4.BackColor = RGB(255, 255, 255)
End Sub
Private Sub TextBox5_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'if the box is blank skip the check
If TextBox5 <> "" Then
'CONTINUATION LINE numeric
If Not IsNumeric(TextBox5) Then
TextBox5.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a number into the GAS continuation line box"
Exit Sub
Else
'CONTINUATION LINE positive
If TextBox5 < 0 Then
TextBox5.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a positive number into the GAS continuation line box"
Exit Sub
Else
'CONTINUATION LINE less than 6
If TextBox5 > 5 Then
TextBox5.BackColor = RGB(255, 153, 153)
MsgBox "Plese enter a number less than 6 into the GAS continuation line box"
Exit Sub
Else
'CONTINUATION LINE whole number
TextBox5.Value = Left(TextBox5, 1)
End If
End If
End If
End If
'CONTINUATION LINE box white for correct
TextBox5.BackColor = RGB(255, 255, 255)
End Sub
Private Sub TextBox6_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'if the box is blank skip the check
If TextBox6 <> "" Then
'CONTINUATION LINE numeric
If Not IsNumeric(TextBox6) Then
TextBox6.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a number into the WATER continuation line box"
Exit Sub
Else
'CONTINUATION LINE positive
If TextBox6 < 0 Then
TextBox6.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a positive number into the WATER continuation line box"
Exit Sub
Else
'CONTINUATION LINE less than 6
If TextBox6 > 5 Then
TextBox6.BackColor = RGB(255, 153, 153)
MsgBox "Plese enter a number less than 6 into the WATER continuation line box"
Exit Sub
Else
'CONTINUATION LINE whole number
TextBox6.Value = Left(TextBox6, 1)
End If
End If
End If
End If
'CONTINUATION LINE box white for correct
TextBox6.BackColor = RGB(255, 255, 255)
End Sub
Private Sub TextBox7_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'if the box is blank skip the check
If TextBox7 <> "" Then
'CONTINUATION LINE numeric
If Not IsNumeric(TextBox7) Then
TextBox7.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a number into the NGL continuation line box"
Exit Sub
Else
'CONTINUATION LINE positive
If TextBox7 < 0 Then
TextBox7.BackColor = RGB(255, 153, 153)
MsgBox "Please enter a positive number into the NGL continuation line box"
Exit Sub
Else
'CONTINUATION LINE less than 6
If TextBox7 > 5 Then
TextBox7.BackColor = RGB(255, 153, 153)
MsgBox "Plese enter a number less than 6 into the NGL continuation line box"
Exit Sub
Else
'CONTINUATION LINE whole number
TextBox7.Value = Left(TextBox7, 1)
End If
End If
End If
End If
'CONTINUATION LINE box white for correct
TextBox7.BackColor = RGB(255, 255, 255)
End Sub