找到第一个空单元格时退出循环并继续下一行
Exit loop and continue to next line when first empty cell is found
我需要能够遍历 Excel 工作表并在 第一个 null 或空值 我希望循环退出并继续下一行。
'Add the rows'
For Each cell In worksheetRow
If cell.Value Is Nothing Then
For index = _dataTable.Rows.Count - 1 To 0
Dim deleteRow = _dataTable.Rows(index)
If _dataTable.Rows.Contains("NULL") Then
deleteRow.Delete()
End If
Next
GoTo executeInsert
Else
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
End If
Next cell
Next
executeInsert: Try
Company.Applications.ProductionEngine.BusinessAccess.BankTargetsConfigurationBusinessAccess.ExecuteLoadFileData(_dataTable, _year)
问题是 For Each 循环遍历每个单元格。因此,在它找到的第一个空单元格上,它会继续到下一个空单元格,依此类推。
这是插入 table 的结果。
New Table
直到第 13 行,在第一次出现 null 时,我想退出循环并转到我的下一行代码,它负责保存 到目前为止循环收集的内容 直到第 12 行最右边的单元格值为“2018”。
为了完整起见,这是连接到数据库并保存数据的下一个代码块。这是我找到第一个 null 出现后需要去的地方。
Try
Company.Applications.ProductionEngine.BusinessAccess.BankTargetsConfigurationBusinessAccess.ExecuteLoadFileData(_dataTable, _year)
Catch ex As Exception
'The data insert failed'
InformationBox1.ShowErrorMessage("An internal error occured. Please refresh the page and try again.")
End Try
InformationBox1.ShowSuccessMessage("Data Uploaded")
我试过了,很管用。问题是,如果稍后将某些内容添加到工作表并且行变为 148,这将无济于事。
For rowNumber As Integer = 1 To 147
Dim worksheetRow = _worksheet.Cells(rowNumber, 2, rowNumber, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
Next
Next
我也试过这个但是行没有增加。读取工作表中的每一行并添加到第一行,每条记录覆盖前一条,所以这里总是一行。
Dim rowCounter As Integer = 0
While rowCounter < _worksheet.Dimension.End.Row
Dim worksheetRow = _worksheet.Cells(1, 2, _worksheet.Dimension.End.Row, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
If cell.Value IsNot Nothing Then
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
End If
rowCounter += 1
Next
End While
最后,我尝试了 Do Loop。我得到与上面的 While 循环相同的结果。
Do
Dim worksheetRow = _worksheet.Cells(1, 2, _worksheet.Dimension.End.Row, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
Next
Loop Until _worksheet.Cells.Value = ""
如有任何疑问,请告诉我。
我不确定,但也许你可以这样做:
For Each cell In rng
' do some stuff
If myCondition Then GoTo exitFor
Next cell
exitFor:
很难理解您到底想要实现什么,所以我不得不假设您的变量(例如工作表行)已正确定义...
Also, you have an error in your code. .Start
is not a property of a
range object (cell.Start.Column - 2
) in your code. This will result
in an error I can't correct given you haven't provided your input data
nor expected result.
I presumed you're trying to offset the column by 2 to the right, but change it accordingly to what you're trying to achieve
Same goes for .ToString
which is not a Range object method in VBA. Unless you have them somewhere defined in your own class properties and have not included it in your code here. Just like with offset, sounds like you're trying to utilize the CStr()
function here, but once again, with no expected output I can only be guessing here.
无论哪种方式,这些都是小的潜在错误,您应该能够照顾好自己。通常,您的循环应如下所示:
For each cell in worksheetRow
If IsEmpty(Cell) Then
Exit For ' this will exit the entire for loop after reachign an empty cell
End If
row.Offset(0, -2) = CStr(Trim(cell.Value))
Next cell
如果您想跳过空单元格,而不是退出整个 for 循环,请在您的单元格中使用辅助 dowhile 循环作为继续 - 在这个堆栈问题中找到示例:Continue For loop
伙计们。只需要删除特定行,如果它不包含任何内容。
这是最终代码。
For rowNumber As Integer = 1 To _worksheet.Dimension.End.Row
Dim worksheetRow = _worksheet.Cells(rowNumber, 2, rowNumber, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
If cell.Value Is Nothing Then
row.Delete()
GoTo executeInsert
Else
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
End If
Next cell
Next
//executeInsert is the connection/insert data method
感谢大家的帮助。
我需要能够遍历 Excel 工作表并在 第一个 null 或空值 我希望循环退出并继续下一行。
'Add the rows'
For Each cell In worksheetRow
If cell.Value Is Nothing Then
For index = _dataTable.Rows.Count - 1 To 0
Dim deleteRow = _dataTable.Rows(index)
If _dataTable.Rows.Contains("NULL") Then
deleteRow.Delete()
End If
Next
GoTo executeInsert
Else
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
End If
Next cell
Next
executeInsert: Try
Company.Applications.ProductionEngine.BusinessAccess.BankTargetsConfigurationBusinessAccess.ExecuteLoadFileData(_dataTable, _year)
问题是 For Each 循环遍历每个单元格。因此,在它找到的第一个空单元格上,它会继续到下一个空单元格,依此类推。
这是插入 table 的结果。
New Table
直到第 13 行,在第一次出现 null 时,我想退出循环并转到我的下一行代码,它负责保存 到目前为止循环收集的内容 直到第 12 行最右边的单元格值为“2018”。
为了完整起见,这是连接到数据库并保存数据的下一个代码块。这是我找到第一个 null 出现后需要去的地方。
Try
Company.Applications.ProductionEngine.BusinessAccess.BankTargetsConfigurationBusinessAccess.ExecuteLoadFileData(_dataTable, _year)
Catch ex As Exception
'The data insert failed'
InformationBox1.ShowErrorMessage("An internal error occured. Please refresh the page and try again.")
End Try
InformationBox1.ShowSuccessMessage("Data Uploaded")
我试过了,很管用。问题是,如果稍后将某些内容添加到工作表并且行变为 148,这将无济于事。
For rowNumber As Integer = 1 To 147
Dim worksheetRow = _worksheet.Cells(rowNumber, 2, rowNumber, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
Next
Next
我也试过这个但是行没有增加。读取工作表中的每一行并添加到第一行,每条记录覆盖前一条,所以这里总是一行。
Dim rowCounter As Integer = 0
While rowCounter < _worksheet.Dimension.End.Row
Dim worksheetRow = _worksheet.Cells(1, 2, _worksheet.Dimension.End.Row, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
If cell.Value IsNot Nothing Then
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
End If
rowCounter += 1
Next
End While
最后,我尝试了 Do Loop。我得到与上面的 While 循环相同的结果。
Do
Dim worksheetRow = _worksheet.Cells(1, 2, _worksheet.Dimension.End.Row, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
Next
Loop Until _worksheet.Cells.Value = ""
如有任何疑问,请告诉我。
我不确定,但也许你可以这样做:
For Each cell In rng
' do some stuff
If myCondition Then GoTo exitFor
Next cell
exitFor:
很难理解您到底想要实现什么,所以我不得不假设您的变量(例如工作表行)已正确定义...
Also, you have an error in your code.
.Start
is not a property of a range object (cell.Start.Column - 2
) in your code. This will result in an error I can't correct given you haven't provided your input data nor expected result.I presumed you're trying to offset the column by 2 to the right, but change it accordingly to what you're trying to achieve
Same goes for
.ToString
which is not a Range object method in VBA. Unless you have them somewhere defined in your own class properties and have not included it in your code here. Just like with offset, sounds like you're trying to utilize theCStr()
function here, but once again, with no expected output I can only be guessing here.
无论哪种方式,这些都是小的潜在错误,您应该能够照顾好自己。通常,您的循环应如下所示:
For each cell in worksheetRow
If IsEmpty(Cell) Then
Exit For ' this will exit the entire for loop after reachign an empty cell
End If
row.Offset(0, -2) = CStr(Trim(cell.Value))
Next cell
如果您想跳过空单元格,而不是退出整个 for 循环,请在您的单元格中使用辅助 dowhile 循环作为继续 - 在这个堆栈问题中找到示例:Continue For loop
伙计们。只需要删除特定行,如果它不包含任何内容。
这是最终代码。
For rowNumber As Integer = 1 To _worksheet.Dimension.End.Row
Dim worksheetRow = _worksheet.Cells(rowNumber, 2, rowNumber, 4)
Dim row As DataRow = _dataTable.Rows.Add()
For Each cell In worksheetRow
If cell.Value Is Nothing Then
row.Delete()
GoTo executeInsert
Else
row(cell.Start.Column - 2) = cell.Value.ToString.Trim()
End If
Next cell
Next
//executeInsert is the connection/insert data method
感谢大家的帮助。