jnikodym
asked on
Excel VBA, find a string in a column, update a cell
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
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.
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.
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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sudodh Tiwari,
Your solution worked great. Thank you
Your solution worked great. Thank you
You're welcome. Glad to help.
Open in new window