VBA code to read headers

bmkelly

Board Regular
Joined
Mar 26, 2020
Messages
172
Office Version
  1. 365
Platform
  1. Windows
Hello I currently am trying to have my code Read/Find by Column Headers instead of Columns M, O, and U that you see in my code below. I just want to make sure that it reads and finds the headera and then goes through the code that way if anyone adds any columns it doesn't effect the code any.

VBA Code:
Sub BlankWarranty()

'Looks for Additions with a Blank Warranty in WarrantyEnd Column'

    Dim r1 As String, r2 As String
    Dim lr As Long
  
        lr = Range("M" & Rows.Count).End(3).Row 'Transaction Type'
        r1 = Range("M2:M" & lr).Address 'Transaction Type'
        r2 = Range("O2:O" & lr).Address 'WarrantyEnd'
  
  'Notes added in Analyst Notes'
    Range("U2:U" & lr).Value = Evaluate("=IF(" & r1 & "=""Addition"",IF(ISBLANK(" & r2 & "),""Review - Blank Warranty Addition"",""""),"""")")

End Sub
 
Please, take the guess work out of what you're working with and post something we can actually see and work with.
Either use the forums XL2BB add in
or share an anonymized file using onedrive, google drive, drop box or box.com (which is free for individual use) or some other file sharing site. Thanks.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Please, take the guess work out of what you're working with and post something we can actually see and work with.
Either use the forums XL2BB add in
or share an anonymized file using onedrive, google drive, drop box or box.com (which is free for individual use) or some other file sharing site. Thanks.
My apologies for the delay

Here is an XL2BB Screenshot of example data I am working with:
Test Pricing Doc.xlsx
ABCDEFGHIJKLMNOPQRSTU
1EquipmentIDModel_IDRSQM_CustNmbrDepartmentIDRSQM_Cust_NameDepartmentCEIDSerialManufacturerModelDescriptionCoverageTransaction TypeEnteredDateWarrantyEndRetiredDateProration DateCharged Date BDS Unit Price Analyst NotesSite Manager Notes
20000000123456781234ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalA Department12345678A-12345A ManuA ModelTestAll Parts & LaborCoverage Change10/22/20188/20/2021$ 32,750.00Review - High Dollar Device
30000000234567892345ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalB Department23456789B-12345B ManuB ModelTestAll Parts & LaborCoverage Change12/27/201912/1/202012/1/20207/1/2021$ 19,250.00Review - High Dollar Device
40000000345678913456ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalC Department34567891C-12345C ManuC ModelTestAll Parts & LaborAddition9/1/20219/1/2021$ 6,900.00Review - High Dollar Device
50000000456789124567ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalD Department45678912D-12345D ManuD ModelTestAll Parts & LaborAddition9/7/20219/7/20219/7/2021$ 6,500.00Review - High Dollar Device
60000000567891235678ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalE Department56789123E-12345E ManuE ModelTestAll Parts & LaborCoverage Change10/22/20187/8/2021$ 5,940.80Review - High Dollar Device
70000000678912346789ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalF Department67891234F-12345F ManuF ModelTestMissing CoverageAddition8/17/20218/17/2021$ 5,000.00Review - Missing Coverage
80000005678912347891ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalG Department567891234G-12345G ManuG ModelTestMissing CoverageCoverage Change10/22/20189/20/2021$ 4,960.00Review - Missing Coverage
90000006789123458912ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalH Department678912345H-12345H ManuH ModelTestMissing CoverageCoverage Change9/11/20191/1/20189/20/2021$ 4,600.00Review - Missing Coverage
100000007891234569123ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalI Department789123456I-12345I ManuI ModelTestMissing CoverageCoverage Change9/13/20199/1/20189/20/2021$ 4,300.00Review - Missing Coverage
1100000089123456712345ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalJ Department891234567J-12345J ManuJ ModelTestMissing CoverageAddition8/6/20218/6/2021$ 2,400.00Review - Missing Coverage
1200000091234567823456ACCT1234567abcdefghijklmnopqrstuvwxyzABC HospitalK Department912345678K-12345K ManuK ModelTestAll Parts & LaborAddition8/6/20218/6/2021$ 2,400.00Review - Blank Warranty Addition
Export Detail
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:A195Cell ValueduplicatestextNO


I would like for the code to read Headers in Row 1 and Find 'Transaction Type' in Column M. Then Find 'Addition' in the Transaction Type Colum, then find 'WarrantyEnd' Column in Column O and if the data is blank then I would like for the code to insert a note in the 'Site Manager Notes' Column stating "Review - Blank Warranty Addition".
 
Upvote 0
then I would like for the code to insert a note in the 'Site Manager Notes' Column stating "Review - Blank Warranty Addition".
Is that to overwrite existing note
or be ahead of or after what already exists ?
 
Upvote 0
Sorry, I had interpreted your original post as both Addition and the note to have the double quote marks around them.
VBA Code:
Sub Testing_v2()

    Dim Trans As Range     'Transaction Type -> M equiv
    Dim WarEnd As Range    'WarrantyEnd -> O equiv
    Dim SiteMan As Range   'Site Manager Notes -> U equiv
    Dim rng As Range, cel As Range
    Dim LR As Long, note As String
    
note = "Review - Blank Warranty Addition"
Application.ScreenUpdating = False
With Sheets("Sheet1")       '<~~~~~ use your actual sheet name
' TheSheet.Range.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    Set Trans = .Range("1:1").Find("Transaction Type", , xlValues, xlWhole)
    Set WarEnd = .Range("1:1").Find("WarrantyEnd", , xlValues, xlWhole)
    Set SiteMan = .Range("1:1").Find("Site Manager Notes", , xlValues, xlWhole)

    LR = .Cells(.Rows.Count, Trans.Column).End(xlUp).Row
    
    Set rng = .Range(.Cells(2, Trans.Column), .Cells(LR, Trans.Column))
    For Each cel In rng
        If cel.Value = "Addition" Then
            If .Cells(cel.Row, WarEnd.Column) = "" Then
            ' USE ONLY ONE OF THE FOLLOWING
            ' to over write
                '.Cells(cel.Row, SiteMan.Column).Value = note
            ' to insert ahead of existing
                .Cells(cel.Row, SiteMan.Column).Value = note & " " & .Cells(cel.Row, SiteMan.Column).Value
            ' to insert after existing
                '.Cells(cel.Row, SiteMan.Column).Value = .Cells(cel.Row, SiteMan.Column).Value & " " & note
            End If
        End If
    Next cel
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Is that to overwrite existing note
or be ahead of or after what already exists ?
It unfortunately does overwrite the existing note but ideally I would love to have a code that would add onto the Note if there is already one existing

Update - sorry I see in your code how you have choose one of the following! I will give that a shot and keep you updated on end result!
 
Upvote 0
Sorry, I had interpreted your original post as both Addition and the note to have the double quote marks around them.
VBA Code:
Sub Testing_v2()

    Dim Trans As Range     'Transaction Type -> M equiv
    Dim WarEnd As Range    'WarrantyEnd -> O equiv
    Dim SiteMan As Range   'Site Manager Notes -> U equiv
    Dim rng As Range, cel As Range
    Dim LR As Long, note As String
   
note = "Review - Blank Warranty Addition"
Application.ScreenUpdating = False
With Sheets("Sheet1")       '<~~~~~ use your actual sheet name
' TheSheet.Range.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    Set Trans = .Range("1:1").Find("Transaction Type", , xlValues, xlWhole)
    Set WarEnd = .Range("1:1").Find("WarrantyEnd", , xlValues, xlWhole)
    Set SiteMan = .Range("1:1").Find("Site Manager Notes", , xlValues, xlWhole)

    LR = .Cells(.Rows.Count, Trans.Column).End(xlUp).Row
   
    Set rng = .Range(.Cells(2, Trans.Column), .Cells(LR, Trans.Column))
    For Each cel In rng
        If cel.Value = "Addition" Then
            If .Cells(cel.Row, WarEnd.Column) = "" Then
            ' USE ONLY ONE OF THE FOLLOWING
            ' to over write
                '.Cells(cel.Row, SiteMan.Column).Value = note
            ' to insert ahead of existing
                .Cells(cel.Row, SiteMan.Column).Value = note & " " & .Cells(cel.Row, SiteMan.Column).Value
            ' to insert after existing
                '.Cells(cel.Row, SiteMan.Column).Value = .Cells(cel.Row, SiteMan.Column).Value & " " & note
            End If
        End If
    Next cel
End With
Application.ScreenUpdating = True
End Sub
It worked! Now I just need to figure out how to incorporate this code from you:
VBA Code:
            ' to insert after existing
                '.Cells(cel.Row, SiteMan.Column).Value = .Cells(cel.Row, SiteMan.Column).Value & " " & note

into the rest of my other subs to add notes onto it!
Thank you!
 
Upvote 0
Sorry, I had interpreted your original post as both Addition and the note to have the double quote marks around them.
VBA Code:
Sub Testing_v2()

    Dim Trans As Range     'Transaction Type -> M equiv
    Dim WarEnd As Range    'WarrantyEnd -> O equiv
    Dim SiteMan As Range   'Site Manager Notes -> U equiv
    Dim rng As Range, cel As Range
    Dim LR As Long, note As String
   
note = "Review - Blank Warranty Addition"
Application.ScreenUpdating = False
With Sheets("Sheet1")       '<~~~~~ use your actual sheet name
' TheSheet.Range.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    Set Trans = .Range("1:1").Find("Transaction Type", , xlValues, xlWhole)
    Set WarEnd = .Range("1:1").Find("WarrantyEnd", , xlValues, xlWhole)
    Set SiteMan = .Range("1:1").Find("Site Manager Notes", , xlValues, xlWhole)

    LR = .Cells(.Rows.Count, Trans.Column).End(xlUp).Row
   
    Set rng = .Range(.Cells(2, Trans.Column), .Cells(LR, Trans.Column))
    For Each cel In rng
        If cel.Value = "Addition" Then
            If .Cells(cel.Row, WarEnd.Column) = "" Then
            ' USE ONLY ONE OF THE FOLLOWING
            ' to over write
                '.Cells(cel.Row, SiteMan.Column).Value = note
            ' to insert ahead of existing
                .Cells(cel.Row, SiteMan.Column).Value = note & " " & .Cells(cel.Row, SiteMan.Column).Value
            ' to insert after existing
                '.Cells(cel.Row, SiteMan.Column).Value = .Cells(cel.Row, SiteMan.Column).Value & " " & note
            End If
        End If
    Next cel
End With
Application.ScreenUpdating = True
End Sub
Do you think if I showed you my other code, how to incorporate "'.Cells(cel.Row, SiteMan.Column).Value = .Cells(cel.Row, SiteMan.Column).Value & " " & note" this to those Subs as well? Essentially, I have a number of Subs that will look for specific criteria and then add a note in the Site Manager Notes Column but if there is an existing note already there, I would love for the code to add on to the existing note instead of overwriting which it currently does.
 
Upvote 0
if you need to go ahead and I'll have a look
No worries if its too crazy or whatever just was wondering the thought process on how to add a specific note to an existing note, below is my code I currently have
VBA Code:
'Blank Warranty Addition'
    Dim TTCol As Range
    Dim WECol As Range
    Dim SMNCol As Range
    Dim rng As Range
    Dim cel As Range
    Dim LR As Long
    Dim note As String
    
    note = "Review - Blank Warranty Addition"

    Application.ScreenUpdating = False

        With Sheets("Export Detail")
        
    'TheSheet.Range.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)'
        Set TTCol = .Range("1:1").Find("Transaction Type", , xlValues, xlWhole)
        Set WECol = .Range("1:1").Find("WarrantyEnd", , xlValues, xlWhole)
        Set SMNCol = .Range("1:1").Find("Site Manager Notes", , xlValues, xlWhole)

        LR = .Cells(.Rows.Count, TTCol.Column).End(xlUp).Row
    
        Set rng = .Range(.Cells(2, TTCol.Column), .Cells(LR, TTCol.Column))
            For Each cel In rng
                If cel.Value = "Addition" Then
                If .Cells(cel.Row, WECol.Column) = "" Then
            'USE ONLY ONE OF THE FOLLOWING'
            'Overwrite Existing Note'
                '.Cells(cel.Row, SiteMan.Column).Value = note'
            'Insert Note Before Existing Note'
                '.Cells(cel.Row, SiteMan.Column).Value = note & " " & .Cells(cel.Row, SiteMan.Column).Value'
            'Insert Note After Existing Note'
                .Cells(cel.Row, SMNCol.Column).Value = .Cells(cel.Row, SMNCol.Column).Value & " " & note
            End If
        End If
    Next cel
End With

Application.ScreenUpdating = True

'Reactivations'
    Dim NCol As Range
    
        On Error Resume Next
    
        Set NCol = Range("1:1").Find("Notes", , , xlWhole, , , False, , False)
            If NCol Is Nothing Then
                MsgBox "Notes Column Not Found"
            Exit Sub
        
        End If
   
        Set SMNCol = Range("1:1").Find("Site Manager Notes", , , xlWhole, , , False, , False)
            If SMNCol Is Nothing Then
                MsgBox "Site Manager Notes Column Not Found"
            Exit Sub
        
        End If
  
        With NCol.EntireColumn
            .Replace "Standard Reactivations", "=true", xlWhole, , False, , False, False
                On Error Resume Next
            .SpecialCells(xlFormulas, xlLogical).Offset(, SMNCol.Column - .Column).Value = "Reactivation"
                On Error GoTo 0
            .Replace "=true", "Standard Reactivations", xlWhole, , False, , False, False
    End With

'Proration'
    Dim PCol As Range
    Dim ACol As Range

        On Error Resume Next
    
        Set PCol = Range("1:1").Find("Proration Date", , , xlWhole, , , False, , False)
        Set ACol = Range("1:1").Find("Site Manager Notes", , , xlWhole, , , False, , False)
   
            If PCol Is Nothing Or ACol Is Nothing Then
                MsgBox "Proration Date Column Not Found"
            Exit Sub
        
        End If
                           
        With Range(PCol.Offset(1), Cells(Rows.Count, PCol.Column).End(xlUp))
            .SpecialCells(xlConstants).Offset(, ACol.Column - PCol.Column).Value = "Review - Prorate?"
            
        If PCol Is Nothing Then Exit Sub
 
    End With
    
'High Dollar Value'
    Dim BCol As Range

        On Error Resume Next

        Set BCol = Range("1:1").Find("BDS Unit Price", , , xlWhole, , , False, , False)
        Set ACol = Range("1:1").Find("Site Manager Notes", , , xlWhole, , , False, , False)
        
            If BCol Is Nothing Or ACol Is Nothing Then
                MsgBox "BDS Unit Price Column Not Found"
            Exit Sub
        
        End If
    
        With Range(BCol.Offset(1), Cells(Rows.Count, BCol.Column).End(xlUp)).Offset(, ACol.Column - BCol.Column)
            .Value = Evaluate(Replace(Replace("if(#>4999,""Review - High Dollar Device"",if(@="""","""",@))", "@", .Address), "#", .Offset(, BCol.Column - ACol.Column).Address))

    End With

'Low Dollar Value'
    Dim BDSCol As Range

        On Error Resume Next

        Set BDSCol = Range("1:1").Find("BDS Unit Price", , , xlWhole, , , False, , False)
        Set SMNCol = Range("1:1").Find("Site Manager Notes", , , xlWhole, , , False, , False)
        
            If BDSCol Is Nothing Or SMNCol Is Nothing Then
                MsgBox "Column Not Found"
            Exit Sub
        
        End If
    
        With Range(BDSCol.Offset(1), Cells(Rows.Count, BDSCol.Column).End(xlUp)).Offset(, SMNCol.Column - BDSCol.Column)
            .Value = Evaluate(Replace(Replace("if(#<-4999,""Review - Low Dollar Device"",if(@="""","""",@))", "@", .Address), "#", .Offset(, BDSCol.Column - SMNCol.Column).Address))

    End With
    
'Missing Coverage'
    Dim CCol As Range
  
        On Error Resume Next

        Set CCol = Range("1:1").Find("Coverage", , , xlWhole, , , False, , False)
            If CCol Is Nothing Then
                MsgBox "Coverage Column Not Found"
            Exit Sub
        
        End If
   
        Set SMNCol = Range("1:1").Find("Site Manager Notes", , , xlWhole, , , False, , False)
            If SMNCol Is Nothing Then
                MsgBox "Site Manager Notes Column Not Found"
            Exit Sub
        
        End If
  
        With CCol.EntireColumn
            .Replace "Missing Coverage", "=true", xlWhole, , False, , False, False
                On Error Resume Next
            .SpecialCells(xlFormulas, xlLogical).Offset(, SMNCol.Column - .Column).Value = "Review - Missing Coverage"
                On Error GoTo 0
            .Replace "=true", "Missing Coverage", xlWhole, , False, , False, False
    End With

'Under Contract'
    Dim VCol As Range
 
        On Error Resume Next

        Set VCol = Range("1:1").Find("VendorContract", , , xlWhole, , , False, , False)
        Set ACol = Range("1:1").Find("Site Manager Notes", , , xlWhole, , , False, , False)
   
            If VCol Is Nothing Or ACol Is Nothing Then
                MsgBox "VendorContract Column Not Found"
            Exit Sub
        
        End If
   
        With Range(VCol.Offset(1), Cells(Rows.Count, VCol.Column).End(xlUp))
            .SpecialCells(xlConstants).Offset(, ACol.Column - VCol.Column).Value = "Review - Contract"
        
    End With
 
Upvote 0
Based on post 12, you don't have a column with the header "Notes", should that be something else ?
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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