Excel 显示用户窗体的命令按钮

Excel 显示用户窗体的命令按钮,excel,vba,Excel,Vba,我试图让ActiveX命令按钮显示名为“DataEntryUserForm”的用户表单 上面是命令按钮的当前代码。单击按钮时,我收到以下错误: 运行时错误“424” 所需对象 我试着四处寻找类似的问题,但什么也找不到 添加用户表单代码: Private Sub DataEntryUserForm_Initialize() 'Empty PatientIdentifierTextBox PatientIdentifierTextBox.Value = "" 'Empty AgeTextBox

我试图让ActiveX命令按钮显示名为“DataEntryUserForm”的用户表单

上面是命令按钮的当前代码。单击按钮时,我收到以下错误:

运行时错误“424”
所需对象

我试着四处寻找类似的问题,但什么也找不到

添加用户表单代码:

Private Sub DataEntryUserForm_Initialize()

'Empty PatientIdentifierTextBox
PatientIdentifierTextBox.Value = ""

'Empty AgeTextBox
AgeTextBox.Value = ""

'Empty SexComboBox
SexComboBox.Clear

'Fill SexComboBox
With SexComboBox
.AddItem "Male"
.AddItem "Female"
End With

'Unselect AlergiesOptionButtons
AllergiesYesOptionButton1.Value = False
AllergiesNoOptionButton2.Value = False

'Set no car as default
CarOptionButton2.Value = True

'Empty PrimaryDiagnosisTextBox
PrimaryDiagnosisTextBox.Value = ""

'Empty ReasonforTransfusionTextBox
ReasonforTransfusionTextBox.Value = ""

'Empty NumberofTransfusionsTextBox
NumberofTransfusionsTextBox.Value = ""

'Empty TypeofTRXNComboBox
TypeofTRXNComboBox.Clear

'Fill TypeofTRXNComboBox
With TypeofTRXNComboBox
.AddItem "TACO"
.AddItem "TRALI"
.AddItem "TAD"
.AddItem "Allergic Reaction"
.AddItem "Hypotensive TRXN"
.AddItem "FNHTR"
.AddItem "AHTR"
.AddItem "DHTR"
.AddItem "DSTR"
.AddItem "TAGVHD"
.AddItem "TTI"
.AddItem "Other"
End With

'Empty TreatmentofTRXNTextBox
TreatmentofTRXNTextBox.Value = ""

'Empty NumberofTransfusionsBeforeTextBox
NumberofTransfusionsBeforeTextBox.Value = ""

'Empty SignsSymptomsTextBox
SignsSymptomsTextBox.Value = ""

'Empty ImputabilityComboBox1
ImputabilityComboBox1.Clear

'Fill ImputabilityComboBox1
With ImputabilityComboBox1
.AddItem "Definite"
.AddItem "Probable"
.AddItem "Possible"
.AddItem "Doubtful"
.AddItem "Ruled Out"
.AddItem "Not Determined"
End With

'Empty SeverityComboBox2
SeverityComboBox2.Clear

'Fill SeverityComboBox2
With SeverityComboBox2
.AddItem "Non-Severe"
.AddItem "Severe"
.AddItem "Life-threatening"
.AddItem "Death"
End With

'Unselect TreatmentOptionButtons
TreatmentDrugOptionButton1.Value = False
TreatmentProductsOptionButton2.Value = False
TreatmentDrugandProductOptionButton3.Value = False

'Empty TypeofDrugTextBox
TypeofDrugTextBox.Value = ""

'Clear ProductModCheckBox
VolumeReductionCheckBox1.Value = False
SalineWashingCheckBox2.Value = False

'Empty TRXNBeforeChangeTextBox
TRXNBeforeChangeTextBox.Value = ""

'Empty TRXNAfterChangeTextBox
TRXNAfterChangeTextBox.Value = ""

'Set Focus on NameTextBox
NameTextBox.SetFocus

End Sub
Private Sub OKButton_Click()

Dim emptyRow As Long

'Make Sheet1 active
Sheet1.Activate

'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
Cells(emptyRow, 1).Value = PatientIdentifierTextBox.Value
Cells(emptyRow, 2).Value = AgeTextBox.Value
Cells(emptyRow, 3).Value = SexComboBox.Value
Cells(emptyRow, 5).Value = PrimaryDiagnosisTextBox.Value
Cells(emptyRow, 6).Value = ReasonforTransfusionTextBox.Value
Cells(emptyRow, 7).Value = NumberofTransfusionsTextBox.Value
Cells(emptyRow, 8).Value = TypeofTRXNComboBox.Value
Cells(emptyRow, 9).Value = TreatmentofTRXNTextBox.Value
Cells(emptyRow, 10).Value = NumberofTransfusionsBeforeTextBox.Value
Cells(emptyRow, 11).Value = SignsSymptomsTextBox.Value
Cells(emptyRow, 12).Value = ImputabilityComboBox1.Value
Cells(emptyRow, 13).Value = SeverityComboBox2.Value
Cells(emptyRow, 15).Value = TypeofDrugTextBox.Value
Cells(emptyRow, 17).Value = TRXNBeforeChangeTextBox.Value
Cells(emptyRow, 18).Value = TRXNAfterChangeTextBox.Value


If AllergiesYesOptionButton1.Value = True Then Cells(emptyRow, 4).Value = 
AllergiesYesOptionButton1.Caption

If AllergiesNoOptionButton2.Value = True Then Cells(emptyRow, 4).Value = 
Cells(emptyRow, 4).Value & " " & AllergiesNoOptionButton2.Caption

End If

If TreatmentDrugOptionButton1.Value = True Then Cells(emptyRow, 14).Value = 
TreatmentDrugOptionButton1.Caption

If TreatmentProductsOptionButton2.Value = True Then Cells(emptyRow, 
14).Value = Cells(emptyRow, 14).Value & " " & 
TreatmentProductsOptionButton2.Caption

If TreatmentDrugandProductOptionButton3.Value = True Then Cells(emptyRow, 14).Value = Cells(emptyRow, 14).Value & " " & 
TreatmentDrugandProductOptionButton3.Caption

End If

If VolumeReductionCheckBox1.Value = True Then Cells(emptyRow, 16).Value = VolumeReductionCheckBox1.Caption

If SalineWashingCheckBox2.Value = True Then Cells(emptyRow, 16).Value = Cells(emptyRow, 16).Value & " " & SalineWashingCheckBox2.Caption

End If

End Sub
Private Sub ClearButton_Click()

Call UserForm_Initialize

End Sub
Private Sub CancelButton_Click()

Unload Me

End Sub
试着改变你的习惯
Private Sub-UserForm\u Initialize()
code to

私有子DataEntryUserForm\u初始化()

(双击用户表单以查看/编辑其代码。)


(我不确定是否添加了用户表单。如果没有,请尝试添加用户表单-在Visual Basic编辑器中单击“插入用户表单”按钮-或转到“插入”>“用户表单”)

添加了用户表单编码。结果如何?如果仍然不起作用,请尝试打开Visual basic窗口并逐个按F8,然后告诉我哪一行出现错误/高亮显示。
Private Sub DataEntryUserForm_Initialize()

'Empty PatientIdentifierTextBox
PatientIdentifierTextBox.Value = ""

'Empty AgeTextBox
AgeTextBox.Value = ""

'Empty SexComboBox
SexComboBox.Clear

'Fill SexComboBox
With SexComboBox
.AddItem "Male"
.AddItem "Female"
End With

'Unselect AlergiesOptionButtons
AllergiesYesOptionButton1.Value = False
AllergiesNoOptionButton2.Value = False

'Set no car as default
CarOptionButton2.Value = True

'Empty PrimaryDiagnosisTextBox
PrimaryDiagnosisTextBox.Value = ""

'Empty ReasonforTransfusionTextBox
ReasonforTransfusionTextBox.Value = ""

'Empty NumberofTransfusionsTextBox
NumberofTransfusionsTextBox.Value = ""

'Empty TypeofTRXNComboBox
TypeofTRXNComboBox.Clear

'Fill TypeofTRXNComboBox
With TypeofTRXNComboBox
.AddItem "TACO"
.AddItem "TRALI"
.AddItem "TAD"
.AddItem "Allergic Reaction"
.AddItem "Hypotensive TRXN"
.AddItem "FNHTR"
.AddItem "AHTR"
.AddItem "DHTR"
.AddItem "DSTR"
.AddItem "TAGVHD"
.AddItem "TTI"
.AddItem "Other"
End With

'Empty TreatmentofTRXNTextBox
TreatmentofTRXNTextBox.Value = ""

'Empty NumberofTransfusionsBeforeTextBox
NumberofTransfusionsBeforeTextBox.Value = ""

'Empty SignsSymptomsTextBox
SignsSymptomsTextBox.Value = ""

'Empty ImputabilityComboBox1
ImputabilityComboBox1.Clear

'Fill ImputabilityComboBox1
With ImputabilityComboBox1
.AddItem "Definite"
.AddItem "Probable"
.AddItem "Possible"
.AddItem "Doubtful"
.AddItem "Ruled Out"
.AddItem "Not Determined"
End With

'Empty SeverityComboBox2
SeverityComboBox2.Clear

'Fill SeverityComboBox2
With SeverityComboBox2
.AddItem "Non-Severe"
.AddItem "Severe"
.AddItem "Life-threatening"
.AddItem "Death"
End With

'Unselect TreatmentOptionButtons
TreatmentDrugOptionButton1.Value = False
TreatmentProductsOptionButton2.Value = False
TreatmentDrugandProductOptionButton3.Value = False

'Empty TypeofDrugTextBox
TypeofDrugTextBox.Value = ""

'Clear ProductModCheckBox
VolumeReductionCheckBox1.Value = False
SalineWashingCheckBox2.Value = False

'Empty TRXNBeforeChangeTextBox
TRXNBeforeChangeTextBox.Value = ""

'Empty TRXNAfterChangeTextBox
TRXNAfterChangeTextBox.Value = ""

'Set Focus on NameTextBox
NameTextBox.SetFocus

End Sub
Private Sub OKButton_Click()

Dim emptyRow As Long

'Make Sheet1 active
Sheet1.Activate

'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
Cells(emptyRow, 1).Value = PatientIdentifierTextBox.Value
Cells(emptyRow, 2).Value = AgeTextBox.Value
Cells(emptyRow, 3).Value = SexComboBox.Value
Cells(emptyRow, 5).Value = PrimaryDiagnosisTextBox.Value
Cells(emptyRow, 6).Value = ReasonforTransfusionTextBox.Value
Cells(emptyRow, 7).Value = NumberofTransfusionsTextBox.Value
Cells(emptyRow, 8).Value = TypeofTRXNComboBox.Value
Cells(emptyRow, 9).Value = TreatmentofTRXNTextBox.Value
Cells(emptyRow, 10).Value = NumberofTransfusionsBeforeTextBox.Value
Cells(emptyRow, 11).Value = SignsSymptomsTextBox.Value
Cells(emptyRow, 12).Value = ImputabilityComboBox1.Value
Cells(emptyRow, 13).Value = SeverityComboBox2.Value
Cells(emptyRow, 15).Value = TypeofDrugTextBox.Value
Cells(emptyRow, 17).Value = TRXNBeforeChangeTextBox.Value
Cells(emptyRow, 18).Value = TRXNAfterChangeTextBox.Value


If AllergiesYesOptionButton1.Value = True Then Cells(emptyRow, 4).Value = 
AllergiesYesOptionButton1.Caption

If AllergiesNoOptionButton2.Value = True Then Cells(emptyRow, 4).Value = 
Cells(emptyRow, 4).Value & " " & AllergiesNoOptionButton2.Caption

End If

If TreatmentDrugOptionButton1.Value = True Then Cells(emptyRow, 14).Value = 
TreatmentDrugOptionButton1.Caption

If TreatmentProductsOptionButton2.Value = True Then Cells(emptyRow, 
14).Value = Cells(emptyRow, 14).Value & " " & 
TreatmentProductsOptionButton2.Caption

If TreatmentDrugandProductOptionButton3.Value = True Then Cells(emptyRow, 14).Value = Cells(emptyRow, 14).Value & " " & 
TreatmentDrugandProductOptionButton3.Caption

End If

If VolumeReductionCheckBox1.Value = True Then Cells(emptyRow, 16).Value = VolumeReductionCheckBox1.Caption

If SalineWashingCheckBox2.Value = True Then Cells(emptyRow, 16).Value = Cells(emptyRow, 16).Value & " " & SalineWashingCheckBox2.Caption

End If

End Sub
Private Sub ClearButton_Click()

Call UserForm_Initialize

End Sub
Private Sub CancelButton_Click()

Unload Me

End Sub