Share via

Excel I need to use the below macro to function on the workbook that I am working on.

SteveD 495 Reputation points
2026-05-27T02:41:55.6133333+00:00

Hello from Steve

Can the below line be changed to please any workbook that I am working on

Set ws = ThisWorkbook.Sheets("15 Mar Sun Tauherenikau")

Sub HighlightCellsWithNumbersAndText()

Dim ws As Worksheet

Dim rng As Range, cell As Range

Dim hasNumber As Boolean, hasLetter As Boolean

Dim i As Long



'Set the range to check

Set ws = ThisWorkbook.Sheets("15 Mar Sun Tauherenikau")

Set rng = ws.Range("C1:C250")



' Loop through each cell in the range

For Each cell In rng

    If VarType(cell.Value) = vbString Or VarType(cell.Value) = vbDouble Or VarType(cell.Value) = vbVariant Then

        hasNumber = False

        hasLetter = False


        

        ' Check each character in the cell

        For i = 1 To Len(cell.Text)

            If Mid(cell.Text, i, 1) Like "[0-9]" Then hasNumber = True

            If Mid(cell.Text, i, 1) Like "[A-Za-z]" Then hasLetter = True

            ' Exit early if both found

            If hasNumber And hasLetter Then Exit For

        Next i


        

        ' Highlight if both number and letter exist

        If hasNumber And hasLetter Then

            cell.Interior.Color = RGB(255, 255, 0) ' Yellow highlight

        End If

    End If

Next cell



MsgBox "Highlighting complete.", vbInformation
```End Sub
Microsoft 365 and Office | Excel | For home | Windows
0 comments No comments

Answer accepted by question author

AI answer

2026-05-27T02:42:14.5966667+00:00

Replace the Set ws line so the macro uses the active workbook and active sheet instead of a specific sheet name.

For the active sheet in the workbook being worked on:

Sub HighlightCellsWithNumbersAndText()

    Dim ws As Worksheet
    Dim rng As Range, cell As Range
    Dim hasNumber As Boolean, hasLetter As Boolean
    Dim i As Long

    ' Use the active sheet in the active workbook
    Set ws = ActiveWorkbook.ActiveSheet
    Set rng = ws.Range("C1:C250")

    ' Loop through each cell in the range
    For Each cell In rng
        If VarType(cell.Value) = vbString Or _
           VarType(cell.Value) = vbDouble Or _
           VarType(cell.Value) = vbVariant Then

            hasNumber = False
            hasLetter = False

            ' Check each character in the cell
            For i = 1 To Len(cell.Text)
                If Mid(cell.Text, i, 1) Like "[0-9]" Then hasNumber = True
                If Mid(cell.Text, i, 1) Like "[A-Za-z]" Then hasLetter = True
                ' Exit early if both found
                If hasNumber And hasLetter Then Exit For
            Next i

            ' Highlight if both number and letter exist
            If hasNumber And hasLetter Then
                cell.Interior.Color = RGB(255, 255, 0) ' Yellow highlight
            End If
        End If
    Next cell

    MsgBox "Highlighting complete.", vbInformation

End Sub

If the macro should always run on the sheet that is currently active, this change is sufficient. If it should always run on a specific sheet in the active workbook but with a variable name, use:

Set ws = ActiveWorkbook.Sheets("15 Mar Sun Tauherenikau")

instead of ThisWorkbook.


References:

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.