使用循环查找值的组合框
Combobox to find value using loop
我有组合框 9 的用户窗体,当你 select 组合框 9 值时,它将在每个框中显示所有值,你可以将文本框 19 值更新为 sheet 反对 selected combobox9 中的值但是问题是是否有相同的名称,例如在 combobox9 中有两次相同的名称,它只会在原始文件中更新其第一个名称,而不会更新第二个名称,或者即使 sheet 中有 3 个具有相同名称的条目。
名称在 C 列中,文本值在 H 列上更新了它的名称,但是我需要循环 H 列,如果它已经根据它的名称更新,那么需要更新相同的名称,它是新的原始文件。
下面是我拥有的 vba 代码,但目前无法使用
If Me.ComboBox9.Value <> "" Then
If VBA.CVar(Application.Match(VBA.CVar(Me.ComboBox9.Value), sh.Range("C:C"), 0)) = True Then
MsgBox "Record Not found for this PO-Number", vbCritical
Exit Sub
Else
i = Application.Match(VBA.CStr(Me.ComboBox9.Value), sh.Range("C:C"), 0)
End If
lCol = Me.ComboBox9.Value
Set findvalue2 = sh.Range("C:C").Find(What:=lCol, LookIn:=xlValues)
If Not findvalue2 Is Nothing Then
adr = findvalue2.Address
Do
If findvalue2.Offset(0, -1).Value = Me.TextBox19 Then
sh.Unprotect "1234"
findvalue2.Offset(0, 6).Value = Me.TextBox19.Value = ""
Exit Do
End If
Set findvalue2 = sh.Range("C:C").FindNext(findvalue2)
Loop While findvalue2.Address <> adr
Set findvalue2 = Nothing
End If
我不完全确定我明白你想做什么,但假设你只是想从 col C 到 col H 中的相同值,当在组合框中选择该值时,beyond 应该这样做。
我替换了你的匹配函数,因为这个函数的缺点是它会在第一次出现时停止。此外,使用 vba 时数组的速度要快得多。在非常大的数据集上,您甚至可以通过使用“字典”进一步改进代码。
Sub combobox()
Dim test As String, combobox1 As combobox, names
Sheets(1).combobox1.List = Sheets(1).Range("c2:c4").Value
names = Sheets(1).Range("C1:C5").Value2
Dim j As Long, i As Long
If Sheets(1).combobox1.Value <> "" Then
Debug.Print Sheets(1).combobox1.Value
For j = 1 To UBound(names)
If (names(j, 1) = Sheets(1).combobox1.Value) Then
Sheets(1).Cells(j, 8).Value2 = names(j, 1)
End If
Next j
End If
End Sub
祝你好运,
我有组合框 9 的用户窗体,当你 select 组合框 9 值时,它将在每个框中显示所有值,你可以将文本框 19 值更新为 sheet 反对 selected combobox9 中的值但是问题是是否有相同的名称,例如在 combobox9 中有两次相同的名称,它只会在原始文件中更新其第一个名称,而不会更新第二个名称,或者即使 sheet 中有 3 个具有相同名称的条目。
名称在 C 列中,文本值在 H 列上更新了它的名称,但是我需要循环 H 列,如果它已经根据它的名称更新,那么需要更新相同的名称,它是新的原始文件。
下面是我拥有的 vba 代码,但目前无法使用
If Me.ComboBox9.Value <> "" Then
If VBA.CVar(Application.Match(VBA.CVar(Me.ComboBox9.Value), sh.Range("C:C"), 0)) = True Then
MsgBox "Record Not found for this PO-Number", vbCritical
Exit Sub
Else
i = Application.Match(VBA.CStr(Me.ComboBox9.Value), sh.Range("C:C"), 0)
End If
lCol = Me.ComboBox9.Value
Set findvalue2 = sh.Range("C:C").Find(What:=lCol, LookIn:=xlValues)
If Not findvalue2 Is Nothing Then
adr = findvalue2.Address
Do
If findvalue2.Offset(0, -1).Value = Me.TextBox19 Then
sh.Unprotect "1234"
findvalue2.Offset(0, 6).Value = Me.TextBox19.Value = ""
Exit Do
End If
Set findvalue2 = sh.Range("C:C").FindNext(findvalue2)
Loop While findvalue2.Address <> adr
Set findvalue2 = Nothing
End If
我不完全确定我明白你想做什么,但假设你只是想从 col C 到 col H 中的相同值,当在组合框中选择该值时,beyond 应该这样做。
我替换了你的匹配函数,因为这个函数的缺点是它会在第一次出现时停止。此外,使用 vba 时数组的速度要快得多。在非常大的数据集上,您甚至可以通过使用“字典”进一步改进代码。
Sub combobox()
Dim test As String, combobox1 As combobox, names
Sheets(1).combobox1.List = Sheets(1).Range("c2:c4").Value
names = Sheets(1).Range("C1:C5").Value2
Dim j As Long, i As Long
If Sheets(1).combobox1.Value <> "" Then
Debug.Print Sheets(1).combobox1.Value
For j = 1 To UBound(names)
If (names(j, 1) = Sheets(1).combobox1.Value) Then
Sheets(1).Cells(j, 8).Value2 = names(j, 1)
End If
Next j
End If
End Sub
祝你好运,