【发布时间】:2013-09-10 15:24:02
【问题描述】:
我正在尝试编写一个应用程序,它需要一个报告(Excel 工作表),操作一行,然后转到下一行,然后是下一行,等等,然后在前两个单元格中退出 Do Until 循环下一行是空的(表示没有更多行要处理),如下所示:
Imports Excel = Microsoft.Office.Interop.Excel
Dim MSExcel As New Excel.Application
MSExcel.Visible = True
Dim WorkbookA As Excel.Workbook
Dim WorksheetA As Excel.Worksheet
Dim i As Integer = 2 'Skipping header row
Dim Split() As String
Dim SomeStrings() As String = {"StringA", "StringB"} 'etc... an array of strings
WorkbookA = MSExcel.Workbooks.Open(TextBox1.Text)
WorksheetA = WorkbookA.Sheets.Item(1)
Do Until WorksheetA.Cells(i, 1).Value = "" And WorksheetA.Cells(i, 2).Value = ""
'~~If Column A cell does not contain 'valueA' or Column E cell does not contain 'valueB', delete the row
Do Until InStr(WorksheetDisplay.Cells(i, 1).Value, "ValueA") <> 0 And InStr(WorksheetDisplay.Cells(i, 5).Value, "ValueB") <> 0 _
And InStr("somenumbershere", Strings.Left((WorksheetDisplay.Cells(i, 3).Value), 1)) <> 0 'Only keeps entries that begin with a certain number
WorksheetDisplay.Rows(i).Delete() 'Otherwise we delete the row
Loop
For Each Str As String In SomeStrings
If Str = WorksheetDisplay.Cells(i, 3).Value Then
Split = Strings.Split(WorksheetDisplay.Cells(i, 3).Value, " ")
WorksheetDisplay.Cells(i, 3).Value = Split(0) & " some text here"
End If
Next
i = i + 1
Loop
但是程序永远不会停止运行。
知道为什么吗?
【问题讨论】:
-
要么这两个单元格永远不会真正为空,要么
[various statements here]修改了i的值。 -
感谢您的回复。我添加了实际代码而不是 [这里的各种语句]。
-
请注意,不需要 i = i-1。当删除一行时,工作簿的行向上移动,因此之前的第 i+1 行变为第 i 行。
标签: vb.net visual-studio-2010 excel