Test for Upper Case and Lower Case, VBA.

ikjohnson

Board Regular
Joined
Sep 19, 2003
Messages
133
Hi,

I'd like to test the text of column headings in an Excel spreadsheet, where the headings may vary slightly from one data source to another.
For example, one worksheet may contain the heading Dealer in C4, where another worksheet may use Dealer Name. In the code below, the Left function seems to handle this situation.

I'm wondering how to handle situations where the heading in A4 could be either TERRITORY or Territory? (Both cases would be acceptable.)

Is there also a way to test dates? eg. D4 on occasions may contain 1/31/09 or 31/1/09, but formatted as Jan-09, and on other occasions just the text Jan. Again, both cases would be acceptable.

Code:
Sub TestHeadings()

    Dim strAnswer As String

    ' Test Headings here
    If Range("A4").Value <> "Terr" _
       Or Left(Range("B4").Value, 3) <> "Acc" _
       Or Left(Range("C4").Value, 6) <> "Dealer" _
       Or Range("D4").Value <> "Jan" Then

        strAnswer = MsgBox("The worksheet " & ActiveSheet.Name & " may have headings that do not match previous data." _
                           & vbCrLf & vbCrLf & _
                           "The data import will stop at this point." _
                           & vbCrLf & _
                           "Please recheck this worksheet.", _
                           vbOKOnly & vbInformation, "This data may have incorrect headings...")

        If strAnswer = vbOK Then
            Exit Sub
        End If
    End If

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You can use:
Code:
If UCase$(Range("A4").Value) <> "TERR" _
and so on.
 
Upvote 0
Try:

Code:
If UCase(Range("A4").Value) <> "TERR" _
   Or UCase(Left(Range("B4").Value, 3)) <> "ACC" _
   Or UCase(Left(Range("C4").Value, 6)) <> "DEALER" _
   Or UCase(Range("D4").Value) <> "JAN" Then
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

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