Excel 在列中查找空单元格和空白单元格的区别
我需要在一列中找到第一个空单元格或只包含空格的单元格。我提出了以下建议Excel 在列中查找空单元格和空白单元格的区别,excel,vba,Excel,Vba,我需要在一列中找到第一个空单元格或只包含空格的单元格。我提出了以下建议 Dim FindString As String Dim Rng As Range Dim Done As Boolean FindString = "" With Sheets("Yahoo").Range("A:A") Set Rng = .Find(What:=FindString, _ After:=.Cells([Stock_Start_Row], 1), _
Dim FindString As String
Dim Rng As Range
Dim Done As Boolean
FindString = ""
With Sheets("Yahoo").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells([Stock_Start_Row], 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
j = Rng.Row
Done = False
Do Until Done
FindString = .Cells(j, 1)
FindString = Replace(FindString, " ", "")
If FindString = "" Then
j = j - 1
Else
Done = True
End If
Loop
MsgBox "Found" & " " & Rng.Row & " " & j
Else
MsgBox "Nothing found"
End If
End With
这将发现并清除第一个空单元格之前的所有空白单元格,但不会在前面的单元格中发现空白单元格
有没有办法搜索包含一个或多个空格的单元格
如果是这样,我可以添加第二次搜索
我需要在列中找到第一个空或空的单元格
只包含空格
这将通过表上的A栏(“雅虎”)。它应该适合您:
Sub FindBlankOrEmptyCells()
Dim wbk As Workbook
Set wbk = ThisWorkbook
Dim ws As Worksheet
Set ws = wbk.Sheets(1)
Dim cell As Range
Dim BlankCounter As Integer
Dim i As Integer
Dim OldCellValue As Variant ' just for the heck of it
For Each cell In Sheets("Yahoo").Range("A:A")
OldCellValue = cell.Value
cell.NumberFormat = "@"
cell.Value = "'" & cell.Value
BlankCounter = 0
If cell.Value = "" Then
MsgBox "Found an empty cell in Column A, Row: " & " " & cell.Row
Exit Sub
End If
For i = 1 To Len(cell)
If cell.Characters(i, 1).Text = " " Then
BlankCounter = BlankCounter + 1
End If
If BlankCounter = Len(cell) Then
MsgBox "Found a cell full of blanks in Column A, Row: " & " " & cell.Row
cell.Clear
cell.Value = OldCellValue
cell.Value = cell.Value
Exit Sub
' If you want to delete the contents of the cell or continue looping you can delete this Exit Sub and put in:
' cell.ClearContents
' then it will loop through all the cells and delete blanks and message you each time
End If
Next i
cell.Clear
cell.Value = OldCellValue
cell.Value = cell.Value
Next cell
End Sub
这将找到第一个为空或仅包含空格(空格)的单元格。一旦找到满足该条件的单元格,它将停止。如果你想继续循环,你可以启用我注释掉的代码。让我知道它是如何工作的
编辑:
如果您想使用.find函数来获得一些可能的效率,但最终需要遍历单元格中的所有字符并确定它是否包含所有空格。试试这个(我在第30行停止了它,这样它就不会一直弹出空白消息,但您可以删除这些消息并扩展到循环,直到第999999行):
祝你好运。当你说
包含空格时
是指单元格中的文本包含空格
?或者您只是在列中查找空白单元格?为什么不在列中自动筛选长度为零的单元格?@scott:我想查找第一个对用户来说“看起来”为空的单元格,即为空或仅包含一个或多个空格。@brettdj:条件“=”是否查找空单元格?仅包含一个或多个空格的单元格?已编辑-忘记在循环开始时重置空格计数器。现在工作会更好。好的,为了提高效率,我试着用find替换类似的代码。想知道是否还有其他方法可以找到一个包含一个或多个空格的单元格,或者更好的方法是找到一个空的或包含一个或多个空格的单元格。Robbie:(深吸一口气,叹口气)哎呀,你知道这些信息。。。昨天真的会对我更有用开玩笑吧,不过这是一个很好的引语。除了反复明确地写出来之外:FindString=Replace(FindString,“,”)等等。FindString=Replace(FindString、space、space、space)我想我必须让其他人提供一个使用.find查看我的编辑的方法-我想你可能可以将我的sub放在你的sub中。因此,使用.find查找带有空格/空白/空的单元格,然后循环遍历单元格中的每个字符,以确定它们是否都是空格(或空)。最终,您将不得不循环遍历单元格中的每个字符,以确定它是否都是空格-我认为没有任何方法可以解决这个问题。在我看来,str=replace(str,“”)将消除字符串中的所有空格。
Sub FindBlankOrEmptyCellsWithFindFunction()
Dim FindString As String
Dim Rng As Range
Set Rng = Sheets("Yahoo").Range("A1")
Dim Done As Boolean
Dim wbk As Workbook
Set wbk = ThisWorkbook
Dim ws As Worksheet
Set ws = wbk.Sheets(1)
Dim cell As Range
Dim BlankCounter As Integer
Dim i As Integer
Dim ii As Integer
Dim LoopStopperRange As Range
Dim OldCellValue As Variant
ii = 0
Do
ii = ii + 1
FindString = " "
With Sheets("Yahoo").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(Rng.Row, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then ' If Rng Is Something Then
If ii = 1 Then
Set LoopStopperRange = Rng
End If
If LoopStopperRange = Rng And ii > 1 Then
Exit Do
End If
For Each cell In Rng
OldCellValue = cell.Value
cell.NumberFormat = "@"
cell.Value = "'" & cell.Value
BlankCounter = 0
If cell.Value = "" Then
MsgBox "Found an empty cell in Column A, Row: " & " " & cell.Row
'Exit Sub
End If
For i = 1 To Len(cell)
If cell.Characters(i, 1).Text = " " Then
BlankCounter = BlankCounter + 1
End If
If BlankCounter = Len(cell) Then
MsgBox "Found a cell full of blanks in Column A, Row: " & " " & cell.Row
cell.Clear
cell.Value = OldCellValue
cell.Value = cell.Value
'Exit Sub
' If you want to delete the contents of the cell or continue looping you can delete this Exit Sub and put in:
' cell.ClearContents
' then it will loop through all the cells and delete blanks
End If
Next i
cell.Clear
cell.Value = OldCellValue
cell.Value = cell.Value
Next cell
Else
End If
End With
Loop Until Rng Is Nothing
Set Rng = Sheets("Yahoo").Range("A1")
Do
ii = ii + 1
FindString = ""
With Sheets("Yahoo").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(Rng.Row, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then ' If Rng Is Something Then
For Each cell In Rng
OldCellValue = cell.Value
cell.NumberFormat = "@"
cell.Value = "'" & cell.Value
BlankCounter = 0
If cell.Value = "" Then
MsgBox "This loop will go until Row 30 so you don't have to pause/break out. Found an empty cell in Column A, Row: " & " " & cell.Row
'Exit Sub
End If
Next cell
Else
End If
End With
Loop Until Rng.Row = 30
'Loop Until Rng.Row = 99999
End Sub