vba help - replace filter via array

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
853
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want to filter Score greater than 40 Fifty's and greater than 25 Century.

Filter on Column C and D Data,
Expected Output are in Column Range("H2:j7") , how to use array to meet condition.


Below is attempted code only able to copy First Column

VBA Code:
Sub FilterData()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  With Sheets("Sheet1")
    a = .Range("A2:D7").Value
  End With
  ReDim b(1 To UBound(a), 1 To 2)
  For i = 1 To UBound(a)
    If a(i, 3) > 40 And a(i, 4) > 25 Then  'Greater than 40 Fifty and Greater than 25 Century
      k = k + 1
      b(k, 1) = a(i, 1)' how to add remaning cells here
    End If
  Next i
  If k > 0 Then Sheets("Sheet1").Range("h2").Resize(k).Value = b
End Sub



Below is a sample data with expected output.
Book6
ABCDEFGHIJ
1Player NameCountryFiftyCenturyExpected Output"=====>"NameCountryCentury
2SachinIndia10050SachinIndia50
3DhoniIndia9028DhoniIndia28
4PontingAustralia8040PontingAustralia40
5PetersonEngland10025GilchristAustralia80
6GayleWest Indies4032
7GilchristAustralia6580
Sheet1
Cell Formulas
RangeFormula
I2:I5I2=VLOOKUP(H2,A1:$D$7,2,0)
J2:J5J2=VLOOKUP(H2,$A$1:$D$7,4,0)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$D$7I2:J2, J3:J5



Thanks
mg
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,616
Office Version
  1. 365
Platform
  1. Windows
Do you just want the Century column, or do you want the Fifty column as well?
 

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
853
Office Version
  1. 2010
Platform
  1. Windows
Hi Fluff,

if you give both solution for 3 columns and 4 Columns code, I will be ok for it.

I am learning Array with small examples. Thanks




Thanks
mg
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,616
Office Version
  1. 365
Platform
  1. Windows
This will give you all 4 columns
VBA Code:
Sub FilterData()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, j As Long
 
  With Sheets("Sheet1")
    a = .Range("A2:D7").Value
  End With
  ReDim b(1 To UBound(a), 1 To UBound(a, 2))
  For i = 1 To UBound(a)
    If a(i, 3) > 40 And a(i, 4) > 25 Then  'Greater than 40 Fifty and Greater than 25 Century
      k = k + 1
      For j = 1 To UBound(a, 2)
         b(k, j) = a(i, j) ' how to add remaning cells here
      Next j
    End If
  Next i
  If k > 0 Then Sheets("Sheet1").Range("h2").Resize(k, UBound(b, 2)).Value = b
End Sub
 

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
853
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Hi Fluff,

Very nice piece of code, one more help , If I want 3 Columns.
Say Name , Country and Centry , What will be code for this situation.




Thanks
mg
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,616
Office Version
  1. 365
Platform
  1. Windows
try
VBA Code:
Sub FilterData()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, j As Long
 
  With Sheets("Sheet1")
    a = .Range("A2:D7").Value
  End With
  ReDim b(1 To UBound(a), 1 To UBound(a, 2) - 1)
  For i = 1 To UBound(a)
    If a(i, 3) > 40 And a(i, 4) > 25 Then  'Greater than 40 Fifty and Greater than 25 Century
      k = k + 1
      For j = 1 To UBound(a, 2)
         If j < 3 Then
            b(k, j) = a(i, j) ' how to add remaning cells here
         ElseIf j = 4 Then
            b(k, j - 1) = a(i, j)
         End If
      Next j
    End If
  Next i
  If k > 0 Then Sheets("Sheet1").Range("h2").Resize(k, UBound(b, 2)).Value = b
End Sub
 

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
853
Office Version
  1. 2010
Platform
  1. Windows
Hi Fluff,

Perfect ! it worked , getting output as expected,
Thanks for your help. 🕺 (y)




Thanks
mg
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,616
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,713
Messages
5,626,453
Members
416,186
Latest member
shamm28

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
Top