VBA replace all filtered values

mark84

New Member
Joined
Jan 22, 2021
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,
I need your support to solve this problem.
My sheet is named "XXX"
In VBA I write the code to filter all values in Column8 expept for 0 value.

Sub step1()
With Sheets("XXX")
.Range("A1:P1").AutoFilter Field:=8, Criteria1:="<>0"
End With


Now I need to replace all these filtered values (without 0) with "5" value.

I tried to use the function "Replace", but in the field "What:=" I don't want to insert a specific value, I want to replace all filtered values.

Could someone help me please?
Thanks in advance

Regards,
Marco
 

Attachments

  • exc1.PNG
    exc1.PNG
    31.4 KB · Views: 16

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi guys,
I need your support to solve this problem.
My sheet is named "XXX"
In VBA I write the code to filter all values in Column8 expept for 0 value.

Sub step1()
With Sheets("XXX")
.Range("A1:P1").AutoFilter Field:=8, Criteria1:="<>0"
End With


Now I need to replace all these filtered values (without 0) with "5" value.

I tried to use the function "Replace", but in the field "What:=" I don't want to insert a specific value, I want to replace all filtered values.

Could someone help me please?
Thanks in advance

Regards,
Marco
Hey Mark,
Is there a purpose to filtering the data, rather than just telling VBA to replace any value <> to 0 with a 5, in the given column?
 
Upvote 0
I need an automatic process to correct the discounts that my company gets from an external tool.
Can you help me?
 
Upvote 0
Hi mark,
my guess like this
VBA Code:
Sub step1()
With Sheets("XXX")
.Range("A1:P1").AutoFilter Field:=8, Criteria1:="<>0"
Dim c As Range
For Each c In Range("H2:H4") 'change until   finish last cell contains value in column H 
    If c.Value <> 0 Then c.Value = 5
Next c
 End With
End Sub
 
Upvote 0
Try the following...

VBA Code:
Sub step1()

    Dim lastRow As Long
    With Sheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "H").End(xlUp).Row
    End With

    Dim filteredData As Range
    With Sheets("Sheet1").Range("A1:P" & lastRow)
        .AutoFilter Field:=8, Criteria1:="<>0"
        On Error Resume Next
        Set filteredData = .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not filteredData Is Nothing Then
            .Offset(1, 7).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible).Value = 5
        End If
        .AutoFilter
    End With

End Sub

Hope this helps!
 
Upvote 0
Another option...
VBA Code:
Option Explicit
Sub mark84()
    Dim ws As Worksheet
    Set ws = Worksheets("XXX")
    With ws.Range("H2:H" & ws.Cells(Rows.Count, "H").End(xlUp).Row)
        .Value2 = Evaluate("IF(" & .Address(, , , 1) & "<>0,5,0)")
    End With
End Sub
 
Upvote 0
Solution
Hi mark,
my guess like this
VBA Code:
Sub step1()
With Sheets("XXX")
.Range("A1:P1").AutoFilter Field:=8, Criteria1:="<>0"
Dim c As Range
For Each c In Range("H2:H4") 'change until   finish last cell contains value in column H
    If c.Value <> 0 Then c.Value = 5
Next c
 End With
End Sub
Thanks for your answer.
First of all, the range H in my file should be set in this way: Range("H2:H" & LastRow).
With your code all the values in column H (not only the filtered cells) will become 5.
Is there a way to change only the filtered values?
Thanks in advance
 
Upvote 0
First of all, the range H in my file should be set in this way: Range("H2:H" & LastRow).
@Domenic & Kevin did it and their codes are best than me (no loop)
so the question is why you don't answer them and tell them how the code goes?!:rolleyes:
for me I will take @kevin9999 's code is really short and efficient &elegant;)

 
Upvote 0
It seems all perfect codes.
About @kevin9999's code I don't undestand this part:

With ws.Range("H2:H" & ws.Cells(Rows.Count, "H").End(xlUp).Row)
.Value2 = Evaluate("IF(" & .Address(, , , 1) & "<>0,5,0)")

How does it work Evaluate? Never seen it

Thanks
 
Upvote 0
I think you got what you ask for it.
these guys deserve LIKE and mark solved one of them.
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,147
Members
449,098
Latest member
Doanvanhieu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top