Добавление элементов ComboBox из таблицы Excel
Код:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim App As New Excel.Application
Dim Book As Excel.Workbook
Dim Sh1 As Excel.Worksheet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim s As String
Dim x As Long
Book = App.Workbooks.Open(Application.StartupPath & "\Book.xlsx")
Sh1 = Book.Worksheets("Лист1")
s = Sh1.UsedRange.Rows.Count
With Sh1
Do Until x = s
x = x + 1
' тут нужно создать проверку есть ли такой элемент в списке или нет
' если нет то:
ComboBox1.Items.Add(.Range("A" & x).Value2)
Loop
End With
Book.Close()
End Sub
End Class
Public Class Form1
Dim App As New Excel.Application
Dim Book As Excel.Workbook
Dim Sh1 As Excel.Worksheet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim s As String
Dim x As Long
Book = App.Workbooks.Open(Application.StartupPath & "\Book.xlsx")
Sh1 = Book.Worksheets("Лист1")
s = Sh1.UsedRange.Rows.Count
With Sh1
Do Until x = s
x = x + 1
' тут нужно создать проверку есть ли такой элемент в списке или нет
' если нет то:
ComboBox1.Items.Add(.Range("A" & x).Value2)
Loop
End With
Book.Close()
End Sub
End Class
Код:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim App As New Excel.Application
Dim Book As Excel.Workbook
Dim Sh1 As Excel.Worksheet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim V As String = ""
ComboBox1.Sorted = True
Book = App.Workbooks.Open(Application.StartupPath & "\Book.xlsx")
Sh1 = Book.Worksheets("Лист1")
For i = 1 To Sh1.UsedRange.Rows.Count
V = Sh1.Range("A" & i).Value.ToString
If ComboBox1.Items.Contains(V) = False Then ComboBox1.Items.Add(V)
Next
Book.Close()
App.Quit()
End Sub
End Class
Public Class Form1
Dim App As New Excel.Application
Dim Book As Excel.Workbook
Dim Sh1 As Excel.Worksheet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim V As String = ""
ComboBox1.Sorted = True
Book = App.Workbooks.Open(Application.StartupPath & "\Book.xlsx")
Sh1 = Book.Worksheets("Лист1")
For i = 1 To Sh1.UsedRange.Rows.Count
V = Sh1.Range("A" & i).Value.ToString
If ComboBox1.Items.Contains(V) = False Then ComboBox1.Items.Add(V)
Next
Book.Close()
App.Quit()
End Sub
End Class
Не легче отсортировать и добавлять из уже отсортированного списка? И проверку на совпадение будет делать гораздо проще.
можно поискать в гугле или яндексе