A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
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: