Hello everybody!
So, I'd like to create a macro that will be very useful in my tasks, he he. For this to work, I need to filter a specific value and then the cells of the same rows, but in another column they will be replaced by the value I use in the code. Of course, I accept other suggestions for how I can accomplish this task : - D. That's all, I'll wait and thank you for your attention. Follow the example below:
So, I'd like to create a macro that will be very useful in my tasks, he he. For this to work, I need to filter a specific value and then the cells of the same rows, but in another column they will be replaced by the value I use in the code. Of course, I accept other suggestions for how I can accomplish this task : - D. That's all, I'll wait and thank you for your attention. Follow the example below:
VBA Code:
Sub FilterA_and_ReplaceB()
Dim W1 As Worksheet
Dim rowNumber As Long
Set W1 = Sheets("Sheet1")
W1.Select
rowNumber = 2
''' The While and Do Until structures will iterate row by row of the table until they find an empty cell
While (W1.Cells(rowNumber, "A").Value <> "")
Do Until (W1.Cells(rowNumber, "A").Value = "")
rowNumber = rowNumber + 1
Loop
''' If the value of a cell in column "A" matches the search criteria, that value is filtered
If (W1.Cells(rowNumber, "A").Value = "Example") Then
W1.Cells.Range("A1").Select
ActiveSheet.Range("$A$1:$B$1048576").AutoFilter Field:=1, Criteria1:="Example"
'''Once the filter is done, this is where I should insert a code that marks column "B" starting from line 2.
'''The contents of the cells in column "B" will be replaced and the filter selection will be deselected. All of this in a dynamic way.
W1.ShowAllData
W1.Cells(1, 1).Select
MsgBox ("The cells in column B were successfully replaced.")
Exit Sub
Else
Msgbox ("The cells in column B were already replaced!")
End If
Wend
End Sub
Last edited by a moderator: