Solved

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

Posted on 2016-10-20
7
117 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 47

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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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

Technology Partners: 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

Suggested Solutions

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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 lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 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