Solved

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

Posted on 2016-10-20
7
87 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 46

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 33

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 30

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 30

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 30

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

820 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