Solved

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

Posted on 2016-10-20
7
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 48

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 31

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 31

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 31

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

623 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