Solved

Excel VBA, find a string in a column, update a cell

Posted on 2016-10-20
7
50 Views
Last Modified: 2016-10-20
I need to search for a string in say column B, if that string exists, i need to update the cell in that same row in column D, with a value.  How can i do this?  I can't do it with a formula, because my code wipes out the values in Column D.  So, at the end of my code i want to add a do loop that will populate column D with a value if a string exists in Column B
0
Comment
Question by:jnikodym
7 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 41852384
Sub FindAndCopy()
Dim rngFound As Range
Dim strSearch As String

strSearch = InputBox("What do you want to find in column B?", "Find")

Set rngFound = Columns("B:B").Find(What:=strSearch, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
If Not rngFound Is Nothing Then
    Cells(rngFound.Row, "D") = strSearch
End If
End Sub

Open in new window

0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 41852393
You could do it fairly simply without VBA.

Apply a filter to the existing data and then use the dropdown on the chosen column and Select Text filters and use Contains and enter the required search value. Click OK.

This will then show only those rows that include the search value.

You can then go to the first cell in column D and enter the required value. Double clicking the bottom right corner of this cell will then populate that value down the rest of the data but will only populate the visible cells. Then disable the filter.
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41852401
You may try something like this......
Sub FindAndPlaceValue()
Dim lr As Long
Dim findWhat As String, replaceWithWhat
findWhat = "String to find here"
replaceWithWhat = "Value to place in column D here"
lr = Cells(Rows.Count, 2).End(xlUp).Row
With Range("B1:B" & lr)
   .AutoFilter field:=1, Criteria1:=findWhat
   If .SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
      Range("B2:B" & lr).SpecialCells(xlCellTypeVisible).Offset(0, 2).Value = replaceWithWhat
   End If
   .AutoFilter
End With
End Sub

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jnikodym
ID: 41852419
Martin Liss,
This works for the first instance it finds the value, but i need it to keep searching down the document and do it for every instance.

Rob,
  I need this to be done automatically, without the user having to go through the steps you provided.
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41852422
Did you try the solution I offered?
0
 

Author Comment

by:jnikodym
ID: 41852429
Sudodh Tiwari,
  Your solution worked great.  Thank you
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41852432
You're welcome. Glad to help.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now