Check the value before filtering

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Welcome . I want to set the condition for verifying the word YES before filtering. If it does not exist, it is ignored. If the condition is met, the filter will be executed, and the same thing with the word ok With the possibility of executing it on the existing element only

VBA Code:
Public Sub drewhx15()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim Rng As Range
  Dim lr As Long, lr2 As Long

  Set sh1 = ThisWorkbook.Worksheets("feuil1")
  Set sh2 = ThisWorkbook.Worksheets("Sh")
 
  With sh1
    Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
  End With
  With Rng
    .AutoFilter Field:=1, Criteria1:="ok"
    lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("B" & lr)
    lr2 = sh2.Range("B" & Rows.Count).End(3).Row
    sh2.Range("D" & lr & ":D" & lr2).Value = sh1.Range("M2").Value
  
    .AutoFilter Field:=1, Criteria1:="yes"
    lr = sh2.Range("F" & Rows.Count).End(3).Row + 1
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("F" & lr)
    lr2 = sh2.Range("F" & Rows.Count).End(3).Row
    sh2.Range("H" & lr & ":H" & lr2).Value = sh1.Range("M2").Value
  
    .Parent.AutoFilterMode = False
  End With
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about:

VBA Code:
Public Sub drewhx15()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim Rng As Range, f As Range
  Dim lr As Long, lr2 As Long

  Set sh1 = ThisWorkbook.Worksheets("feuil1")
  Set sh2 = ThisWorkbook.Worksheets("Sh")
 
  With sh1
    Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
  End With
  With Rng
    Set f = .Columns(1).Find("ok", , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      .AutoFilter Field:=1, Criteria1:="ok"
      lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
      .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("B" & lr)
      lr2 = sh2.Range("B" & Rows.Count).End(3).Row
      sh2.Range("D" & lr & ":D" & lr2).Value = sh1.Range("M2").Value
    End If
    
    Set f = .Columns(1).Find("yes", , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      .AutoFilter Field:=1, Criteria1:="yes"
      lr = sh2.Range("F" & Rows.Count).End(3).Row + 1
      .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("F" & lr)
      lr2 = sh2.Range("F" & Rows.Count).End(3).Row
      sh2.Range("H" & lr & ":H" & lr2).Value = sh1.Range("M2").Value
    End If
    
    .Parent.AutoFilterMode = False
  End With
End Sub
 
Upvote 0
Not sure what you mean by the last phrase but another option:
(repeat for yes)

VBA Code:
  With Rng
    Dim cntCrit As Long                         ' Move to the Dim area
    cntCrit = WorksheetFunction.CountIfs(Rng.Columns(1), "ok")
    
    If cntCrit <> 0 Then
        .AutoFilter Field:=1, Criteria1:="ok"
        lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
        .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("B" & lr)
        lr2 = sh2.Range("B" & Rows.Count).End(3).Row
        sh2.Range("D" & lr & ":D" & lr2).Value = sh1.Range("M2").Value
    End If
 
Upvote 0
Solution
How about:

VBA Code:
Public Sub drewhx15()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim Rng As Range, f As Range
  Dim lr As Long, lr2 As Long

  Set sh1 = ThisWorkbook.Worksheets("feuil1")
  Set sh2 = ThisWorkbook.Worksheets("Sh")
 
  With sh1
    Set Rng = .Range("b5:d" & .Cells(.Rows.Count, "A").End(xlUp).Row)
  End With
  With Rng
    Set f = .Columns(1).Find("ok", , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      .AutoFilter Field:=1, Criteria1:="ok"
      lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
      .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("B" & lr)
      lr2 = sh2.Range("B" & Rows.Count).End(3).Row
      sh2.Range("D" & lr & ":D" & lr2).Value = sh1.Range("M2").Value
    End If
   
    Set f = .Columns(1).Find("yes", , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      .AutoFilter Field:=1, Criteria1:="yes"
      lr = sh2.Range("F" & Rows.Count).End(3).Row + 1
      .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).Copy sh2.Range("F" & lr)
      lr2 = sh2.Range("F" & Rows.Count).End(3).Row
      sh2.Range("H" & lr & ":H" & lr2).Value = sh1.Range("M2").Value
    End If
   
    .Parent.AutoFilterMode = False
  End With
End Sub
Thank you.Very useful clips...
Frankly, all credit is due to you. I only needed some additions, so that the code was executed despite the absence of the target value, which leads to the loss of the file format. Really, thank you
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,148
Members
449,066
Latest member
Andyg666

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