VBA:找不到方法或数据成员。 Combobox的FindString

时间:2014-12-17 09:16:47

标签: excel vba variables combobox

当我尝试打开UserForm时出现此错误。我想要的是在组合框中添加列中存在的所有不同对象(不重复)。

我一直在那里寻找一些解决方案,直到现在我都可以说是的,我有一个名为“offer1”的组合框

当它给我错误时,会突出显示循环中的.FindString()方法

Private Sub UserForm_Initialize()

Dim rCell As Range
Dim i As String
Dim ws As Worksheet
Dim text As String

text = rCell.text
ws = Offers

offer1.Clear

With offer1
    For Each rCell In Sheets("Offers").Range("A2", Sheets("Offers").Cells(Rows.Count, "A").End(xlUp))
        If TEST.offer1.FindString(text) = -1 Then
            .AddItem CStr(text)
        End If
    Next rCell
End With

End Sub

(如果你看到一些愚蠢的错误,变量的名称为“Ofertas”或类似的东西可能是我将一些变量名称翻译成英语,我跳过了几个)

非常感谢

1 个答案:

答案 0 :(得分:0)

用以下代码替换您的代码:

Option Explicit

Private Sub UserForm_Initialize()

Dim rCell As Range
Dim ws As Worksheet
Dim LastRow As Long
Dim strFirstCell As String

Set ws = Sheets("Offers")
With Me.offer1
    .Clear
    LastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    strFirstCell = ws.Cells(2, "A").Address
    For Each rCell In ws.Range(strFirstCell, ws.Cells(Rows.Count, "A").End(xlUp))
        If Evaluate("=SUMPRODUCT((" & strFirstCell & ":" & rCell.Address(0, 0) & "=" & rCell.Address & ")+0)") = 1 And rCell.Value <> "" Then
            .AddItem rCell.Value
        End If
    Next rCell
End With

End Sub

这将使您的组合框中包含A列中的所有唯一项目,从第2行开始,同时也会跳过任何空白。