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
 
When it's determined that the note needs to be put into the 'Site Manager Notes Column'
this line checks the number of characters that may already be in the cell using the Len function
VBA Code:
If Len(.ListColumns("Site Manager Notes").DataBodyRange(i, 1).Value) = 0 Then
If there aren't any characters then the note is just written to the cell using
VBA Code:
.ListColumns("Site Manager Notes").DataBodyRange(i, 1).Value = note
if there are characters in the cell the above line is passed over and the Else portion of the IF kicks in
the InStr function checks for the note already being in the cell
VBA Code:
If InStr(.ListColumns("Site Manager Notes").DataBodyRange(i, 1).Value, note) = 0 Then
if note isn't already in the cell, it is added to what is alreay in the cell with this line
VBA Code:
                   .ListColumns("Site Manager Notes").DataBodyRange(i, 1).Value = _
                    .ListColumns("Site Manager Notes").DataBodyRange(i, 1).Value & Chr(10) & note
Chr(10) is the ascii code for Alt+Enter which is what you have to use to move to a new line when typing directly in a cell.

Hope that helps
NoSparks
Thank you! You have helped me out tremendously
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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