VBA code to Highlight cell in Red

SamarthSalunkhe

Board Regular
Joined
Jun 14, 2021
Messages
103
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am using below code to find unwanted character in cell, code is working fine but in addition I want code to highlight wrong value cell in red.

VBA Code:
Sub Validate_File()

'Variable Declaration
Dim iCnt As Integer
Dim IpData As Range, DataRange As Range
Dim lr As Long

'-----------------------------------------------------------------------------------------------------------------------------------
'Below code will find Unwanted Characters in Account Number Column
        
    lr = Sheet1.Range("F" & Rows.Count).End(xlUp).Row
    
    'Create Object for Selected Range
    Set DataRange = Sheets("Tally Format").Range("F2:F" & lr)
    
    'Runnning for loop to check all available cells
    For Each IpData In DataRange
    For iCnt = 1 To Len(IpData.Value)
        
        If Not Mid(IpData.Value, iCnt, 1) Like "[0-9A-Z]" Then
            MsgBox "Unwanted Character fould in Column", vbExclamation, "Wrong A/c No"
            Exit Sub
        End If
    Next iCnt
    Next IpData
End Sub

Thank you in Advance 😊
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Add the line in red below:
Rich (BB code):
Sub Validate_File()

'Variable Declaration
Dim iCnt As Integer
Dim IpData As Range, DataRange As Range
Dim lr As Long

'-----------------------------------------------------------------------------------------------------------------------------------
'Below code will find Unwanted Characters in Account Number Column
        
    lr = Sheet1.Range("F" & Rows.Count).End(xlUp).Row
    
    'Create Object for Selected Range
    Set DataRange = Sheets("Tally Format").Range("F2:F" & lr)
    
    'Runnning for loop to check all available cells
    For Each IpData In DataRange
    For iCnt = 1 To Len(IpData.Value)
        
        If Not Mid(IpData.Value, iCnt, 1) Like "[0-9A-Z]" Then
            IpData.Interior.Color = 255
            MsgBox "Unwanted Character fould in Column", vbExclamation, "Wrong A/c No"
            Exit Sub
        End If
    Next iCnt
    Next IpData
End Sub
 
Upvote 0
Add the line in red below:
Rich (BB code):
Sub Validate_File()

'Variable Declaration
Dim iCnt As Integer
Dim IpData As Range, DataRange As Range
Dim lr As Long

'-----------------------------------------------------------------------------------------------------------------------------------
'Below code will find Unwanted Characters in Account Number Column
       
    lr = Sheet1.Range("F" & Rows.Count).End(xlUp).Row
   
    'Create Object for Selected Range
    Set DataRange = Sheets("Tally Format").Range("F2:F" & lr)
   
    'Runnning for loop to check all available cells
    For Each IpData In DataRange
    For iCnt = 1 To Len(IpData.Value)
       
        If Not Mid(IpData.Value, iCnt, 1) Like "[0-9A-Z]" Then
            IpData.Interior.Color = 255
            MsgBox "Unwanted Character fould in Column", vbExclamation, "Wrong A/c No"
            Exit Sub
        End If
    Next iCnt
    Next IpData
End Sub

Hi @Joe4

Getting below error.

1681471887638.png
 
Upvote 0
When you hit "Debug", what line of code does it highlight?
 
Upvote 0
Are you on the sheet when the code is run?
Is there any sort of protection on the sheet?

Maybe try adding the sheet reference, i.e.
VBA Code:
Sheets("Tally Format").IpData.Interior.Color = 255
 
Upvote 0
Are you on the sheet when the code is run?
Is there any sort of protection on the sheet?

Maybe try adding the sheet reference, i.e.
VBA Code:
Sheets("Tally Format").IpData.Interior.Color = 255
Yes my file is protected, I have resolved it.

but I found that above code is only highlighting first error cell not all.
 
Upvote 0
but I found that above code is only highlighting first error cell not all.
It is because you have an "Exit Sub" line in your code. That kicks you out of the entire Sub procedure.

If you just want to exit that loop, and not the entire sub, change "Exit Sub" to "Exit For".
 
Upvote 0
It is because you have an "Exit Sub" line in your code. That kicks you out of the entire Sub procedure.

If you just want to exit that loop, and not the entire sub, change "Exit Sub" to "Exit For".
got it, working fine now.

but I am getting massage box after every error, is it possible to code should highlight all error first and then show massage box.
 
Upvote 0
Here is what I would recommend.
- Put a counter in the loop that counts the number of errors it find
- Remove the message box from the loop
- After the completion of both loops, put an IF...THEN that says IF the error counter > 0, then return a MsgBox
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top