vba help - validate input file using instr

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

Need help to Validate Correct input file.
if cell "A1" Contains Highlighted string in Red Color.

That will be Genuine input file... Macro to proceed else exit.

how to check using instr or Select case here.

Book37
A
1ABC and Co.(Accrual) - 17-18
2PQR Business Consulting LLP
3XYZ Pvt.Ltd. (Formely Known As DEF Consultancy Services Pvt.Ltd.) W.E.F 11-2-20
Sheet1


Below is snapshot highlighted red color are instr.

1608213070704.png



Thanks
mg
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hello,
Check if this code helps you!

VBA Code:
Sub FoundColor()
    Dim I As Long, J As Long, lastRow As Long
    
    lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    
    For I = 1 To lastRow
        For J = 1 To Len(Cells(I, 1).Value)
            If Cells(I, 1).Characters(Start:=J, Length:=1).Font.color = vbRed And Cells(I, 1).Characters(Start:=J, Length:=1).Font.Bold = True Then
                MsgBox "Red and Bold Character found on range " & Cells(I, 1).Address
                J = Len(Cells(I, 1).Value) + 1
            End If
        Next J
    Next I
End Sub
 
Upvote 0
Hi AfonSomira,

Nice code, Thanks for your help, I want to validate cell A1 only. Below is my attempted code which is working
How to achieve same task using Select Case. just for learning purpose.


VBA Code:
Dim str_Company As String
str_Company = ws_input.Range("A1").Value
   
   
   If InStr(1, str_Company, "ABC and Co.", vbTextCompare) = 0 Or _
        InStr(1, str_Company, "PQR Business Consulting LLP", vbTextCompare) = 0 Or _
        InStr(1, str_Company, "XYZ Pvt.Ltd.", vbTextCompare) = 0 Then
    msgbox "Correct input file"
    Else
        MsgBox "Incorrect Input File", vbCritical
    Exit Sub
        
   End If

Thanks
mg
 
Upvote 0
The code for validate only cell A1:
VBA Code:
Sub FoundColor()

Dim J As Long

For J = 1 To Len(Cells(1, 1).Value)
    If Cells(1, 1).Characters(Start:=J, Length:=1).Font.color = vbRed And Cells(1, 1).Characters(Start:=J, Length:=1).Font.Bold = True Then
        MsgBox "Red and Bold Character found on range A1"
        J = Len(Cells(1, 1).Value) + 1
    End If
Next J

End Sub
 
Upvote 0
You want the select case on which code?
This:

VBA Code:
Dim str_Company As String
str_Company = ws_input.Range("A1").Value
  
  
   If InStr(1, str_Company, "ABC and Co.", vbTextCompare) = 0 Or _
        InStr(1, str_Company, "PQR Business Consulting LLP", vbTextCompare) = 0 Or _
        InStr(1, str_Company, "XYZ Pvt.Ltd.", vbTextCompare) = 0 Then
    msgbox "Correct input file"
    Else
        MsgBox "Incorrect Input File", vbCritical
    Exit Sub
       
   End If

Or this:

VBA Code:
Sub FoundColor()

Dim J As Long

For J = 1 To Len(Cells(1, 1).Value)
    If Cells(1, 1).Characters(Start:=J, Length:=1).Font.color = vbRed And Cells(1, 1).Characters(Start:=J, Length:=1).Font.Bold = True Then
        MsgBox "Red and Bold Character found on range A1"
        J = Len(Cells(1, 1).Value) + 1
    End If
Next J

End Sub
 
Upvote 0
Hi afonsomira,

Thanks once again for your help, really nice code,
Actually my requirement is to check cell a1 whether it contains substring. which are highlighted in bold.
not color, but i liked your code, learned something new in it.




Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

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