[英]Excel VBA: Find empty cell and delete the row
I have vertical data with gaps in columns B:G 我在B:G列中有空白的垂直数据
I wish that my code does the following: 我希望我的代码执行以下操作:
I have some basic VBA knowledge and this is the code I have found on the subject so far, however it's a mess in my head how to approach this. 我有一些基本的VBA知识,这是到目前为止我在该主题上找到的代码,但是如何实现这一点在我头上一团糟。
The main issue I am having is how would the code know when to stop deleting and move to the next column. 我遇到的主要问题是代码如何知道何时停止删除并移至下一列。
This code below finds the next empty cell in column B and selects it. 下面的代码在B列中找到下一个空单元格并将其选中。
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 6 'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
Exit For 'This is missing...
End If
Next
End Sub
Public Sub SelectFirstBlankCell()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
For i = 0 To Cells(1, Columns.Count).End(xlToLeft).Column 'count and loop cols
sourceCol = 1 + i 'increment with i, move 1 to start column
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
For currentRow = rowCount To 1 Step -1 ' this will start at end and work its way up
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Or Trim(currentRowValue) = "" Then 'Trim accounts for " " may not be needed
Cells(currentRow, sourceCol).EntireRow.Delete
End If
Next
Next
End Sub
Try above. 尝试以上。 sourceCol = 1 + i can be changed to sourceCol = x + i where x is your start column sourceCol = 1 +我可以更改为sourceCol = x + i其中x是您的起始列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.