VBA code not working

Euro5
Euro5 used Ask the Experts™
on
The FindHeaders Module 17 should find a phrase from Header Names sheet on Net Rate 1 and then also Net Rate 2.

It should return on the NOTES sheet
'Phrase' can be found on Net Rate 1 OR
'Phrase' can not be found on Net Rate 1

but it seems to return a positive "can be found" regardless of whether the phrase is actually there or not.
For instance, it says "Puerto Rico Southbound Priority Overnight (Based on Custom rates [Envelope] [Pak] [Package] )  found on Net Rates 1"  but it really not on the sheet.
Can anyone help? Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Can you upload a sample?
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Euro, whenever possible you should include a workbook in your question so that everyone has a fair chance to answer. In any case I found an error in the code I wrote for you. Replace it with this.

Sub FindHeaders()
Dim cel As Range
Dim wsSummary As Worksheet
Dim wsNR1 As Worksheet
Dim wsNR2 As Worksheet
Dim rngFound As Range
Dim lngNewRow As Long

Set wsSummary = Sheets("Summary")
Set wsNR1 = Sheets("Net Rates 1")
Set wsNR2 = Sheets("Net Rates 2")
 
lngNewRow = wsSummary.Range("A1048576").End(xlUp).Row + 2

For Each cel In Range("Headers")
    With wsNR1
        .Select
        Set rngFound = Selection.Find(What:=cel.Value, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If rngFound Is Nothing Then
            wsSummary.Cells(lngNewRow, 1) = cel.Value & " not found on " & wsNR1.Name
            lngNewRow = lngNewRow + 1
        Else
            wsSummary.Cells(lngNewRow, 1) = cel.Value & " found on " & wsNR1.Name
            lngNewRow = lngNewRow + 1
        End If
    End With
    With wsNR2
        .Select
        Set rngFound = Selection.Find(What:=cel.Value, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If rngFound Is Nothing Then
            wsSummary.Cells(lngNewRow, 1) = cel.Value & " not found on " & wsNR2.Name
            lngNewRow = lngNewRow + 1
        Else
            wsSummary.Cells(lngNewRow, 1) = cel.Value & " found on " & wsNR2.Name
            lngNewRow = lngNewRow + 1
        End If
    End With
Next
End Sub

Open in new window

Author

Commented:
sorry, thought that i did! Must have not completed the attachment process. Ok, will try.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
@Marty, still does not seem to be working.
For instance, on Net sheet 2 there is "Domestic 2 Day" but the result is:
"Domestic 2 Day not found on Net Rates 2"

not sure if I'm missing something...
notes.xlsm
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Are you sure? Please see my results after clearing everything from row 7 down on the Notes sheet.

Looks OK

Author

Commented:
I don't know what I'm missing. I used the same file that I uploaded.
I cleared the rows then ran the macro.
notes.JPG
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Try this. If you still have a notes.xlsm workbook on your pc, rename it. Then download the notes.xlsm workbook you uploaded in post ID: 40833142 and run the FindHeaders macro.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
In addition to my previous post would you be interested in the results looking like this?
Another wayIf so then use this code instead.
Sub FindHeaders()
Dim cel As Range
Dim wsSummary As Worksheet
Dim wsNR1 As Worksheet
Dim wsNR2 As Worksheet
Dim rngFound As Range
Dim lngNewRow As Long
Dim ws As Worksheet

Application.ScreenUpdating = False

Set wsSummary = Sheets("Notes")
Set wsNR1 = Sheets("Net Rates 1")
Set wsNR2 = Sheets("Net Rates 2")

wsSummary.Range("A3,A5").HorizontalAlignment = xlLeft
wsSummary.Range("A7:C7").Font.FontStyle = "Bold"

wsSummary.Cells(lngNewRow, 1) = "Header"
wsSummary.Cells(lngNewRow, 2) = wsNR1.Name
wsSummary.Cells(lngNewRow, 3) = wsNR2.Name

lngNewRow = wsSummary.Range("A1048576").End(xlUp).Row + 2

For Each cel In Range("HeaderNames")
    lngNewRow = lngNewRow + 1
    wsSummary.Cells(lngNewRow, 1) = cel.Value
    With wsNR1
        .Select
        Set rngFound = Selection.Find(What:=cel.Value, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If rngFound Is Nothing Then
            wsSummary.Cells(lngNewRow, 2) = "Not found"
        Else
            wsSummary.Cells(lngNewRow, 2) = "Found"
        End If
    End With
    With wsNR2
        .Select
        Set rngFound = Selection.Find(What:=cel.Value, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If rngFound Is Nothing Then
            wsSummary.Cells(lngNewRow, 3) = "Not found"
        Else
            wsSummary.Cells(lngNewRow, 3) = "Found"
        End If
    End With
Next

Sheets("Notes").Select
Range("A1:A300").WrapText = False
Columns.AutoFit

Application.ScreenUpdating = True
Sheets("Summary").Activate
End Sub

Open in new window

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Are you talking about line 19 in post D: 40834704? What do you mean by "stuck"?

Author

Commented:
I love this version - but I am getting an error at line 19 - any ideas?

Author

Commented:
wsSummary.Cells(lngNewRow, 1) = "Header"

header
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Sorry, my fault. I moved as line without testing it again. I corrected that and added line 18 so you can run it without having to delete the previous results.

Sub FindHeaders()
Dim cel As Range
Dim wsSummary As Worksheet
Dim wsNR1 As Worksheet
Dim wsNR2 As Worksheet
Dim rngFound As Range
Dim lngNewRow As Long
Dim ws As Worksheet

Application.ScreenUpdating = False

Set wsSummary = Sheets("Notes")
Set wsNR1 = Sheets("Net Rates 1")
Set wsNR2 = Sheets("Net Rates 2")

' This will delete everything from row 7 on down so you can run
' this macro without having to delete the results of the last time
wsSummary.Range("A7:A" & wsSummary.UsedRange.Rows.Count).EntireRow.Delete

wsSummary.Range("A3,A5").HorizontalAlignment = xlLeft
wsSummary.Range("A7:C7").Font.FontStyle = "Bold"

lngNewRow = wsSummary.Range("A1048576").End(xlUp).Row + 2

wsSummary.Cells(lngNewRow, 1) = "Header"
wsSummary.Cells(lngNewRow, 2) = wsNR1.Name
wsSummary.Cells(lngNewRow, 3) = wsNR2.Name

For Each cel In Range("HeaderNames")
    lngNewRow = lngNewRow + 1
    wsSummary.Cells(lngNewRow, 1) = cel.Value
    With wsNR1
        .Select
        Set rngFound = Selection.Find(What:=cel.Value, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If rngFound Is Nothing Then
            wsSummary.Cells(lngNewRow, 2) = "Not found"
        Else
            wsSummary.Cells(lngNewRow, 2) = "Found"
        End If
    End With
    With wsNR2
        .Select
        Set rngFound = Selection.Find(What:=cel.Value, After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If rngFound Is Nothing Then
            wsSummary.Cells(lngNewRow, 3) = "Not found"
        Else
            wsSummary.Cells(lngNewRow, 3) = "Found"
        End If
    End With
Next

Sheets("Notes").Select
Range("A1:A300").WrapText = False
Columns.AutoFit

Application.ScreenUpdating = True
Sheets("Summary").Activate
End Sub

Open in new window

Author

Commented:
No problem at all. Unfortunately, still having some issues though..

Now what I get - removes summary of rows and says no services are found....
Notes

Author

Commented:
Here is file I am using.
Rerate-v3.xlsm
Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Corrected.
Rerate-v3.xlsm

Author

Commented:
Excellent solution. Thanks for this.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You're welcome. Sorry it took so long.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial