VBA: enter value in cell if keyword appear in another cell

ippeichan

New Member
Joined
May 22, 2015
Messages
5
Hi there,

I'm doing our family accounting based on a regular download of bank account statement details in Excel. Based on certain keywords appearing in column H, I want a macro to fill out the category in column I. E.g. if cell H1 contains 'Wallmart' (apart from the usual incomprehensible stuff), I1 should be updated to 'Groceries'. The macro will have to loop through all cells to the end (the number of cells changes each period). Several 'keyword to category' combinations will need to be added.

I've read tons of posts, but so far none seems to do what I'm looking for. Any help to get me started would be much appreciated!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Welcome to the forum, give this ago:

Sub AddColData()
Range("H1").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1).Select
If ActiveCell = "Wallmart" Then ActiveCell.Offset(0, 1).Value = "Groceries"
Loop

End Sub
 
Upvote 0
Hi. Give this a go. I have changed 'Wallmart' to 'Walmart' as I thought it only had one 'L'.

Code:
Sub groceries()

Dim c as Range
Dim myAddress as Variant
 
With Range("H1:H" & Range("H" & Rows.Count).End(xlUp).Row)
    Set c = .Find("Walmart", LookIn:=xlValues, LookAt:=xlPart)
    If Not c Is Nothing Then
        myAddress = c.Address
        Do
            c.Offset(0, 1).Value = "Groceries"
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> myAddress
    End If
End With

End Sub
 
Upvote 0
Hi. Give this a go. I have changed 'Wallmart' to 'Walmart' as I thought it only had one 'L'.

Code:
Sub groceries()

Dim c as Range
Dim myAddress as Variant
 
With Range("H1:H" & Range("H" & Rows.Count).End(xlUp).Row)
    Set c = .Find("Walmart", LookIn:=xlValues, LookAt:=xlPart)
    If Not c Is Nothing Then
        myAddress = c.Address
        Do
            c.Offset(0, 1).Value = "Groceries"
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> myAddress
    End If
End With

End Sub

Excellent, that works! The other sample from Trevor works as well now, it was indeed due to the typo. My bad!

Based on your code sample, I managed to add a second keyword search as well.

Many thanks both for your excellent help!
 
Upvote 0
Sorry Steve the fish, one more question. I'd like to add a subcategory as well...

eg if keyword 'mortgage' is found in column H, colum I should be 'home' and j should be 'mortgage'.

Could you be so kind to factor this in to your sample?

Many thanks in advance!!
 
Upvote 0
Code:
c.Offset(0, 1).Value = "Home"
c.Offset(0, 2).Value = "Mortgage"
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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