检查具有组合框值的行并将其用作指针来填充该活动行中的单元格

Check for row with combobox value and use that as a pointer to populate the cell in that active row

我构建了下面的代码以在工作表 "MFGLR" 的 A 列中找到 combobox2 值(如果有,如果没有,则什么也不做),然后在与 combobox2 值相同的行中粘贴AE 列中文本框的值。我不知道如何让它正常工作。

背景:我有一个包含 2 个组合框和 1 个文本框的用户窗体。我已经创建了 3 个场景,其中 2 个场景我需要我上面所说的发生。

Sub linepick()

Dim N As Long
Dim i As Long

'Check for a row with combobox2 value and use that as a pointer to populate the textbox in that active row

N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 5 To N

If Cells(i, "A").Value = Combobox2.value Then

'The  = Combobox2.value brings up an error

Cells(i, "A").Rows.Select

End If

Next i

Userform2.Textbox1.text = CStr(Worksheets("MFGLR").Range("AE" & ActiveCell.Row).Value)

End Sub

如果我没有解释清楚,请告诉我。谢谢

我想这就是你想要的。

  • 此代码不在表单后面,因此您需要添加对它的引用(正如您在后面所做的那样)。
  • 您也可以在找到子后退出。
  • 您正在将 AE 的值传输到文本框,所以我将其调转。
  • 确保您包含的 sheet 参考文献一致。

    Sub linepick()
    
    Dim N As Long
    Dim i As Long
    
    'Check for a row with combobox2 value and use that as a pointer to populate the textbox in that active row
    
    N = Worksheets("MFGLR").Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = 5 To N
        If Worksheets("MFGLR").Cells(i, "A").Value = Userform2.Combobox2.Value Then
            Worksheets("MFGLR").Cells(i, "AE").Value = Userform2.Textbox1.Text
            Exit Sub
        End If
    Next i
    
    End Sub