我在此处找到了此链接:VBA Code to Create Sheets based on the values in column A
它告诉我如何根据第一张纸上其中一列中的名称从列中创建纸张列表而不重复。
我试图将此代码翻译为可用于ComboBox,但我的VBA技能绝对是低迷的。
到目前为止我所拥有的是
Private Sub form_load()
Dim lastRow As Integer
Dim cellName As Integer
Dim match As Boolean
lastRow = Sheets(1).Range("F2").End(xlDown).Row
For i = 2 To lastRow
match = False
cellName = .Sheets(1).Range("F" & i).Value
For _________________
ComboBox1.AddItem i
End Sub
您会注意到,在我链接的示例中,他们使用代码
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = sheetName Then
match = True
End If
Next
确定单元格是否已用作工作表名称。但是,我对VBA不太熟悉,不知道是否存在补充命令来检查组合框中的值。
答案 0 :(得分:1)
试试这个:
Private Sub UserForm_Initialize()
Dim lastRow As Integer
Dim cellName As Integer
Dim match As Boolean
Dim iItem As Integer, sItem As String
lastRow = Sheets(1).Range("F2").End(xlDown).Row
For i = 2 To lastRow
match = False
sItem = Sheets(1).Range("F" & i).Value2
For iItem = 0 To ComboBox1.ListCount - 1
If ComboBox1.List(iItem) = sItem Then
match = True
Exit For
End If
Next iItem
If Not match Then ComboBox1.AddItem sItem
Next i
End Sub