用VB如何把一个excel里一个列的数据无重复的写入combo里?
用VB如何把一个excel里一个列的数据无重复的放到combo里?
(可先假定该Excel文件路径固定[比如c:\book1.xls],要打开的列也固定[比如sheet1中的第一列的所有数据])
我自己写了一小段,实在不晓得如何写,请大家帮我看看行不,多谢!
Private Sub Command1_Click()
Dim aa As Excel.Application
Dim bb As Excel.Workbook
Dim cc As Excel.Worksheet
Set aa = c:\book1.xls-------这个肯定不对,但实在不晓得怎么写:)
Set bb = aa.Workbooks
Set cc = bb.Worksheets
rec1 = cc.Application.Cells(1, 1).Value
If rec1 <> " " Then
i = 1
Combo1.Clear
Do While Not rec1 = " "
Combo1.AddItem cc.Application.Cells(i, 1).Value
rec1 = cc.Application.Cells(i, 1).Value
i = i + 1
Loop
End If
End Sub
[解决办法]
Private Sub Command1_Click()
Dim aa As Excel.Application
Dim bb As Excel.Workbook
Dim cs As String
Dim rec1 As String
Dim i As Integer
Set aa = New Excel.Application ' c:\book1.xls-------这个肯定不对,但实在不晓得怎么写:)
On Error GoTo errQuit
Set bb = aa.Workbooks.Open( "c:\Excel1.xls ")
On Error GoTo errClose
i = 1
Combo1.Clear
cs = bb.Worksheets(1).Cells(1, 1)
Do While Not cs = " "
If InStr(1, rec1, "; " & cs & "; ") < 1 Then
Combo1.AddItem cs
rec1 = rec1 & "; " & cs & "; "
End If
i = i + 1
cs = bb.Worksheets(1).Cells(i, 1).Value
Loop
errClose:
If Err.Number <> 0 Then MsgBox Err.Description
On Error Resume Next
bb.Close False
Set bb = Nothing
errQuit:
If Err.Number <> 0 Then MsgBox Err.Description
On Error Resume Next
aa.Quit
Set aa = Nothing
End Sub
[解决办法]
cangwu_lee(橙子) 的代码无懈可击