karabiner8
Board Regular
- Joined
- Jan 11, 2005
- Messages
- 50
I've adapted a bit of code I found on the web to search for text in two adjacent columns where only numbers should be. It works fine when I test it. But now I want to check two more columns that are not contiguous with the first two columns I checked and so on for a total of three groups of two adjacent columns.
I tried a For loop but got errors so I thought I'd just do a brute force method and copy the code down three times (once for each group of two adjacent columns) and just change the range that is being looked at but the Err.Clear statement doesn't seem to be doing its job.
I've included the code below to check just the first two groups of adjacent cells. The code works for all scenarios accept one: if none of the target columns contain any text at all it bombs out with the error Run-time error '1004': No cells were found. after reporting that the first group of columns has no text in it. The line highlighted when I hit debug is:
The full code is:
Any help (including alternate methods to achieve the same thing) would be most appreciated.
I tried a For loop but got errors so I thought I'd just do a brute force method and copy the code down three times (once for each group of two adjacent columns) and just change the range that is being looked at but the Err.Clear statement doesn't seem to be doing its job.
I've included the code below to check just the first two groups of adjacent cells. The code works for all scenarios accept one: if none of the target columns contain any text at all it bombs out with the error Run-time error '1004': No cells were found. after reporting that the first group of columns has no text in it. The line highlighted when I hit debug is:
Rich (BB code):
Set oNOCells = Sheets("SG_Tracker").Range("BC8:BD" & lLastRow).Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
The full code is:
Rich (BB code):
Sub FindCellsWithText()
'adapted from http://vbadud.blogspot.com/2008_06_01_archive.html
Dim lLastRow As Long
Dim oNOCells
'Find last row
lLastRow = Range("W" & Rows.Count).End(xlUp).Row
'+++++++++++++++check first range
On Error GoTo Err_Hdlr1
Set oNOCells = Sheets("SG_Tracker").Range("AW8:AX" & lLastRow).Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each ocell In oNOCells
MsgBox "There is text in cell " & ocell.Address & vbCrLf & _
"make a note of it and correct it.", vbOKOnly, "Error!!!"
Next ocell
Err_Hdlr1:
If Err <> 0 Then
If Err.Description = "No cells were found." Then
MsgBox "No cells with text found in range AW8:AX" & lLastRow, vbOKInformation, "Note"
End If
Err.Clear
End If
'+++++++++++++++check second range
On Error GoTo Err_Hdlr2
Set oNOCells = Sheets("SG_Tracker").Range("BC8:BD" & lLastRow).Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each ocell In oNOCells
MsgBox "There is text in cell " & ocell.Address & vbCrLf & _
"make a note of it and correct it.", vbOKOnly, "Error!!!"
Next ocell
Err_Hdlr2:
If Err <> 0 Then
If Err.Description = "No cells were found." Then
MsgBox "No cells with text found in range BC8:BD" & lLastRow, vbOKInformation, "Note"
End If
Err.Clear
End If
End Sub
Any help (including alternate methods to achieve the same thing) would be most appreciated.