excel 如何比较单元格值和组合框条目?

6vl6ewon  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(144)

我创建了一个带有文本框和组合框的UserForm。文本框中输入的数据应该插入工作表中的某个单元格中。哪个单元格的确定基于ComboBox4中的值。
代码给我:
运行时错误"13":类型不匹配

If Cells(C + 1, 1) Like ComboBox4 Then
Private Sub UserForm_Initialize()
    ComboBox3.List = [ADMIN!e2:E1000].Value
    ComboBox4.List = [PRODUCTION!O6:O1000].Value
End Sub

Private Sub ACCEPTBUTTON_Click()

Application.ScreenUpdating = False

Worksheets("PRODUCTION").Activate

Dim C As Long
For C = 1000 To 1 Step -1
If Cells(C + 1, 1) Like ComboBox4 Then

Cells(C + 1, 1).EntireRow.Select
Selection.EntireRow.Hidden = False
Application.CutCopyMode = False

End If
Next C

Range("AC" & (ActiveCell.Row)).Value = TextBox1.Value
Range("AD" & (ActiveCell.Row)).Value = TextBox2.Value
Range("AE" & (ActiveCell.Row)).Value = TextBox3.Value
Range("AF" & (ActiveCell.Row)).Value = TextBox4.Value
Range("AG" & (ActiveCell.Row)).Value = TextBox5.Value
Range("AH" & (ActiveCell.Row)).Value = TextBox6.Value
Range("AI" & (ActiveCell.Row)).Value = TextBox7.Value
Range("AJ" & (ActiveCell.Row)).Value = TextBox8.Value

ActiveCell.EntireRow.RowHeight = 16

Unload Me
Application.ScreenUpdating = True

End Sub
mrfwxfqh

mrfwxfqh1#

下面是一些注解过的代码,应该对你有用。我确实觉得很奇怪,你从列O填充ComboBox4中的值,然后搜索列A以寻找匹配项,这是故意的吗?(在提供的代码中,它从填充组合框的相同列表中搜索匹配项,这将保证找到匹配项)。
此外,它使用了一个Range.find循环来提高速度和效率,而不是使用一个1000长的循环来查找匹配项。

'Declare userform variables that any of this userform's Subs can reference
Private wb As Workbook
Private wsAdm As Worksheet
Private wsPrd As Worksheet
Private rAdmList As Range
Private rPrdList As Range

Private Sub UserForm_Initialize()
    
    'Populate userform variables
    Set wb = ThisWorkbook
    Set wsAdm = wb.Worksheets("ADMIN")
    Set wsPrd = wb.Worksheets("PRODUCTION")
    Set rAdmList = wsAdm.Range("E2", wsAdm.Cells(wsAdm.Rows.Count, "E").End(xlUp))  'Dynamically size list
    Set rPrdList = wsPrd.Range("O6", wsPrd.Cells(wsPrd.Rows.Count, "O").End(xlUp))  'Dynamically size list
    
    Me.ComboBox3.List = rAdmList.Value
    Me.ComboBox4.List = rPrdList.Value
    
End Sub

Private Sub ACCEPTBUTTON_Click()
    
    'Check if anything is selected from ComboBox4
    If Me.ComboBox4.ListIndex = -1 Then
        Me.ComboBox4.SetFocus
        MsgBox "Must select a Production item"
        Exit Sub
    End If
    
    'An item from the production list in combobox4 has been confirmed to be selected
    'Search the corresonding ComboBox4 list range to find the corresponding row
    '(In your original code, you are searching column A instead of the column that populated the combobox which is column O, is there a reason for that?)
    Dim rFound As Range, sFirst As String
    Set rFound = rPrdList.Find(Me.ComboBox4.Text, rPrdList(rPrdList.Cells.Count), xlValues, xlWhole)
    If Not rFound Is Nothing Then
        sFirst = rFound.Address 'Record first address of found item
        Do
            'Matching row found, unhide and populate cells with textbox values
            'Note that there is currently no check or validation that the textboxes are populated
            rFound.EntireRow.Hidden = False
            wsPrd.Cells(rFound.Row, "AC").Value = Me.TextBox1.Text
            wsPrd.Cells(rFound.Row, "AD").Value = Me.TextBox2.Text
            wsPrd.Cells(rFound.Row, "AE").Value = Me.TextBox3.Text
            wsPrd.Cells(rFound.Row, "AF").Value = Me.TextBox4.Text
            wsPrd.Cells(rFound.Row, "AG").Value = Me.TextBox5.Text
            wsPrd.Cells(rFound.Row, "AH").Value = Me.TextBox6.Text
            wsPrd.Cells(rFound.Row, "AI").Value = Me.TextBox7.Text
            wsPrd.Cells(rFound.Row, "AJ").Value = Me.TextBox8.Text
            
            'Search for next cell that matches
            Set rFound = rPrdList.FindNext(rFound)
        Loop While rFound.Address <> sFirst 'Loop until back at first address
    Else
        'If the item wasn't found, it's because the user manually typed in something in the combobox, or other error occurred
        Me.ComboBox4.SetFocus
        MsgBox "Invalid value entered for Production item"
        Exit Sub
    End If
    
    Unload Me
    
End Sub

相关问题