我有一个包含多个用户表单按钮的Excel表单。当我使用这个userform button1时,它应该工作,直到我使用userform button2。在我使用button2之后,再次尝试使用button1来更改我的partnumber,我得到运行时错误'1004'。
我认为问题是在button1上我只将数据输入到一个单元格。因为我有ws.data.value = me.textbox_partnumber.value它真的不是真的,因为我的第一个条目会将ws.data.value改为我改变的任何部件号,比如说“PARTS”。因此,当我再次使用userform并将其更改为“TESTS”时,将发生错误,因为“PARTS”(ws.data.value)不等于“TESTS”(me.textbox_partnumber.value)。希望有道理吗?
这是我的公式:
Private Sub CommandButton1_Click()
Dim data As Range
Dim ws As Worksheet
Dim wt As Range
Set ws = Worksheets("SecondShift")
Set data = ws.Range("d1")
Set wt = ws.Range("l1")
If Trim(Me.apartnumber.Value) = "" Then
Me.apartnumber.SetFocus
MsgBox "Please Select the Rebel Part Number"
Exit Sub
End If
If Trim(Me.atextbox_workticket.Value) = "" Then
Me.atextbox_workticket.SetFocus
MsgBox "Please Enter The Work Ticket Number"
Exit Sub
End If
With ws
data.Value = Me.apartnumber.Value '<---THIS IS WHERE THE HIGHLIGHTED ERROR IS
wt.Value = Me.atextbox_workticket.Value
End With
'clear the data
Me.apartnumber.Value = ""
Me.atextbox_workticket.Value = ""
Me.Hide
End Sub
这是button2的代码
Private Sub acmd_EnterData_Click()
Dim iRow As Long
Dim Lastrow As Long
Dim ws As Worksheet
Set ws = Worksheets("SecondShift")
Lastrow = ws.Range("i101").End(xlUp).Row
For iRow = 16 To Lastrow
If ws.Cells(iRow, 9) = "" And ws.Cells(iRow, 10) = "" Then
ws.Cells(iRow, 9).Value = Me.atextbox_Lane1.Value
ws.Cells(iRow, 10).Value = Me.atextbox_Lane2.Value
ws.Cells(iRow, 11).Value = Me.atextbox_Lane3.Value
ws.Cells(iRow, 12).Value = Me.atextbox_Lane4.Value
ws.Cells(iRow, 13).Value = Me.atextbox_Lane5.Value
ws.Cells(iRow, 14).Value = Me.atextbox_Lane6.Value
ws.Cells(iRow, 15).Value = Me.atextbox_Lane7.Value
ws.Cells(iRow, 16).Value = Me.atextbox_Length.Value
ws.Cells(iRow, 17).Value = Me.atextbox_SheetCount.Value
ws.Cells(iRow, 18).Value = Me.acbchecktype.Value
ws.Cells(iRow, 19).Value = Me.acbchecktype1.Value
End If
Next iRow
If checkbox_Retest.Value = False And Me.atextbox_Lane1.Value = "" Then
'do nothing
Me.atextbox_Lane1.SetFocus
MsgBox "ENTER LANE 1 WIDTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.atextbox_Length.Value = "" Then
'do nothing
Me.atextbox_Length.SetFocus
MsgBox "ENTER YOUR LENGTH!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.atextbox_SheetCount.Value = "" Then
'do nothing
Me.atextbox_SheetCount.SetFocus
MsgBox "ENTER THE SHEETCOUNT!"
Exit Sub
End If
If checkbox_Retest.Value = False And Me.acbchecktype.Value = "" Then
'do nothing
Me.acbchecktype.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR PERF CHECK!!"
Exit Sub
Select Case checktype
Case Trim(Me.acbchecktype.Value) = "PASS"
checktype = "PASS"
Case Trim(Me.acbchecktype.Value) = "FAIL"
checktype = "FAIL"
End Select
End If
If checkbox_Retest.Value = False And Me.acbchecktype1.Value = "" Then
'do nothing
Me.acbchecktype1.SetFocus
MsgBox "ENTER 'PASS' OR 'FAIL' FOR SLITHER CHECK!!"
Exit Sub
Select Case checktype1
Case Trim(Me.acbchecktype1.Value) = "PASS"
checktype1 = "PASS"
Case Trim(Me.acbchecktype1.Value) = "FAIL"
checktype1 = "FAIL"
End Select
End If
With ws
.Cells(iRow, 9).Value = Me.atextbox_Lane1.Value
.Cells(iRow, 10).Value = Me.atextbox_Lane2.Value
.Cells(iRow, 11).Value = Me.atextbox_Lane3.Value
.Cells(iRow, 12).Value = Me.atextbox_Lane4.Value
.Cells(iRow, 13).Value = Me.atextbox_Lane5.Value
.Cells(iRow, 14).Value = Me.atextbox_Lane6.Value
.Cells(iRow, 15).Value = Me.atextbox_Lane7.Value
.Cells(iRow, 16).Value = Me.atextbox_Length.Value
.Cells(iRow, 17).Value = Me.atextbox_SheetCount.Value
.Cells(iRow, 18).Value = Me.acbchecktype.Value
.Cells(iRow, 19).Value = Me.acbchecktype1.Value
End With
Me.atextbox_Lane1.Value = ""
Me.atextbox_Lane2.Value = ""
Me.atextbox_Lane3.Value = ""
Me.atextbox_Lane4.Value = ""
Me.atextbox_Lane5.Value = ""
Me.atextbox_Lane6.Value = ""
Me.atextbox_Lane7.Value = ""
Me.atextbox_Length.Value = ""
Me.atextbox_SheetCount.Value = ""
Me.acbchecktype.Value = ""
Me.acbchecktype1.Value = ""
Me.checkbox_Retest.Value = False
Me.Hide
End Sub