Macro to find name in header and put in word in column below header matching the number of rows in another column

tropics123

Board Regular
Joined
May 11, 2016
Messages
85
I'm trying to get the macro to look for "Mary Lee" in header (row 1) and puts in the word "LOAN" below it. It should go all the way down to match the number of rows in column A. The name "Mary Lee" may appear multiple times in the header. Data will change daily, so the number of rows in Column A will change.

This is what the macro is currently doing. It stopped after finding "Mary Lee" the first time and overrode her name. Macro is at the bottom. Thanks for your help!


[TABLE="class: cms_table_grid, width: 500, align: center"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]Column B
Mary Lee
LOAN[/TD]
[TD]Column C
Luke Skywalker
[/TD]
[TD]Column D
Mary Lee
[/TD]
[TD]Column E
Bob Smith
[/TD]
[TD]Column F
Mary Lee
[/TD]
[TD]Column G
Joe Watson[/TD]
[/TR]
[TR]
[TD]2/5/19[/TD]
[TD]LOAN[/TD]
[TD]$90[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]$23[/TD]
[/TR]
[TR]
[TD]3/15/19[/TD]
[TD]LOAN[/TD]
[TD]$19[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]$587[/TD]
[/TR]
[TR]
[TD]5/15/19[/TD]
[TD][/TD]
[TD]$35[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]$6560[/TD]
[/TR]
</tbody>[/TABLE]

Desired result:

[TABLE="class: cms_table_grid, width: 500, align: center"]
<tbody>[TR]
[TD]Column A[/TD]
[TD]Column B
Mary Lee
[/TD]
[TD]Column C
Luke Skywalker
[/TD]
[TD]Column D
Mary Lee
[/TD]
[TD]Column E
Bob Smith
[/TD]
[TD]Column F
Mary Lee
[/TD]
[TD]Column G
Joe Watson[/TD]
[/TR]
[TR]
[TD]2/5/19[/TD]
[TD]LOAN[/TD]
[TD]$90[/TD]
[TD]LOAN[/TD]
[TD][/TD]
[TD]LOAN[/TD]
[TD]$23[/TD]
[/TR]
[TR]
[TD]3/15/19[/TD]
[TD]LOAN[/TD]
[TD]$19[/TD]
[TD]LOAN[/TD]
[TD][/TD]
[TD]LOAN[/TD]
[TD]$587[/TD]
[/TR]
[TR]
[TD]5/15/19[/TD]
[TD]LOAN[/TD]
[TD]$35[/TD]
[TD]LOAN[/TD]
[TD][/TD]
[TD]LOAN[/TD]
[TD]$6560[/TD]
[/TR]
</tbody>[/TABLE]


Borrowed some of this macro idea from JoeMo

Sub AddinWordLOAN()


Sheets("Sheet3").Select


Dim tgtHdrs As Variant, Fnd As Range, fAdr As String
Dim LastRow As Long
tgtHdrs = Array("Mary Lee") 'Look for name "Mary Lee" in the header row 1 and put in the word LOAN below it
Application.ScreenUpdating = False
For i = LBound(tgtHdrs) To UBound(tgtHdrs)
Set Fnd = Rows(1).Find(what:=tgtHdrs(i), lookat:=xlWhole, MatchCase:=False)
If Not Fnd Is Nothing Then
fAdr = Fnd.Address
End If
Do
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range(Fnd.Offset(2, 0), Cells(Rows.Count, Fnd.Column).End(xlUp)).Value = "LOAN" 'the word LOAN should go all the way down to match number of rows in column A
Set Fnd = Rows(1).FindNext(Fnd)
If Fnd Is Nothing Then Exit Do
If Fnd.Address = fAdr Then Exit Do
Loop
Next i
Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG28Jun25
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Col [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = ActiveSheet.Range("A1").CurrentRegion

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Col [COLOR="Navy"]In[/COLOR] Rng.Rows(1).Cells
    [COLOR="Navy"]If[/COLOR] Col.Value = "Mary Lee" [COLOR="Navy"]Then[/COLOR]
        Col.Offset(1).Resize(Rng.Rows.Count - 1) = "Loan"
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Col
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,737
Messages
6,174,206
Members
452,551
Latest member
croud

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