VBA to search multiple names in Column B, return value in Column A

Beh162

Board Regular
Joined
Jan 15, 2015
Messages
130
Right now I have it setup to look in column B for a name and then return a value in Column A directly next to it. I have it working for one name, but would I just continue this process over and over per name? Or is there a better approach?

VBA Code:
Sub info()
Dim i As Long
For i = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    If ActiveSheet.Cells(i, 2) = "Doe, John" Then
        ActiveSheet.Range("A" & i) = "Clothing"
    End If
Next i
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You should keep the list of names on a worksheet, the you can loop through the list.
If you hard code the names in the code, you will always be editing the code.

Prepare a test sheet like the following

1599914025872.png


Add a button and use this code.

VBA Code:
Sub Button1_Click()
    Dim rng As Range, c As Range
    Dim Frng As Range, f As Range

    Set rng = Range("P2:P5")
    Set Frng = Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)

    For Each c In rng.Cells
        For Each f In Frng.Cells

            If f = c Then f.Offset(, -1) = c.Offset(, 1)

        Next f
    Next c

End Sub
 
Upvote 0
You should keep the list of names on a worksheet, the you can loop through the list.
If you hard code the names in the code, you will always be editing the code.

Prepare a test sheet like the following

View attachment 22232

Add a button and use this code.
Wouldn't I need to create a table each time I download the new data?
 
Upvote 0
Just to clarify I download an excel sheet each day that has data populated and I run my saved VBA's. One of my sheets there are a few people that get their department manually changed each time. It's probably a group of 10 people at most that change to say "Clothing" I see what you mean if someone is then added, I would need to add them to the VBA or remove.
Screen Shot 2020-09-12 at 11.21.54 AM.png
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,973
Members
449,059
Latest member
oculus

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