循环 Excel 范围填充组合框 VB.Net
Loop through Excel Ranges Filling Combobox VB.Net
我有一个程序需要遍历一个非常大的 excel 范围,以将两个范围组合成一个组合框值。我有以下代码可以这样做,但它所做的只是将第一个值迭代五次。如果我删除第一个 FOR 循环,那么它只会 returns 第一个值并且永远不会完成。关于我可以做些什么来完善此代码并使其正常工作有什么建议吗?
Dim i As Integer
If TenantBox.SelectedItem = "CNS" Then
WFMBook.Workbooks.Open("C:\Schedule.xlsx")
For i = 0 To 5 Step +1
For Each CNSCell In WFMBook.Range("A3:A1441").Cells
f = CNSCell.Value.ToString
Next
For Each tst In WFMBook.Range("B3:B1441").Cells
l = tst.Value.ToString
Next
ComboBox1.Items.Add(f + " " + l)
If (i = 5) Then
Exit For
End If
Console.WriteLine(i)
Next
End If
试试这个,如果 VBA 是基于 1 的,只需将 x 更改为 1。
If TenantBox.SelectedItem = "CNS" Then
WFMBook.Workbooks.Open("C:\XHSchedule.xlsx")
Dim colCount = WFMBook.Range("A3:A8").Cells.Count
For x = 1 To colCount Step +1
For Each CNSCell In WFMBook.Range("A3:A8").Cells
f = WFMBook.Range("A3:A8").Cells(x).Value.ToString
l = WFMBook.Range("B3:B8").Cells(x).Value.ToString
Next
ComboBox1.Items.Add(f + " " + l)
Next
End If