Solved

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

Posted on 2016-10-20
7
79 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 32

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 29

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 29

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 29

Expert Comment

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

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

777 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