VBA: Filter before union

dpatel20

New Member
Joined
Feb 3, 2014
Messages
11
Hi, I am creating a union for later use (export to CSV). Is there a way to filter the columns before the union WITHOUT copying and pasting to a new sheet.

So...

Code:
    With sh.ListObjects("MasterData")
        Set rngToSave = Application.Union(.ListColumns("FATHER'S MOBILE").DataBodyRange, _

                                            .ListColumns("CENTRE").DataBodyRange, _
                                            .ListColumns("MOTHER@S MOBILE").DataBodyRange, _
                                            .ListColumns("ADDRESS2").DataBodyRange)
    End With

In the above, can I filter out rows where both 'FATHER'S MOBILE' and 'MOTHER'S MOBILE' are blank?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
One way

Add a column to your table and filter on that column (in my test it was the 9th column in the table)
- column returns
0 (NEITHER parent has a mobile
1 (ONE parent has a mobile)
2 (BOTH parents have a mobile)

Formula is
=COUNTA([@[FATHER''S MOBILE]],[@[MOTHER''S MOBILE]])


Code:
    [I][COLOR=#ff0000]On Error Resume Next [/COLOR][/I]                           'required to prevent code crashing
   [COLOR=#ff0000][I] sh.ShowAllData    [/I][/COLOR]                              'clear prior filter
    With sh.ListObjects("MasterData")
[I][COLOR=#ff0000]        .Range.AutoFilter Field:=[/COLOR][COLOR=#0000cd][B]9[/B][/COLOR][COLOR=#ff0000], Criteria1:="<>0", Operator:=xlAnd[/COLOR][/I]
        Set rngToSave = Application.Union(.ListColumns("FATHER'S MOBILE").DataBodyRange, _
            .ListColumns("CENTRE").DataBodyRange, _
            .ListColumns("MOTHER'S MOBILE").DataBodyRange, _
            .ListColumns("ADDRESS2").DataBodyRange)[I][COLOR=#ff0000].SpecialCells(xlCellTypeVisible)[/COLOR][/I]
    End With
      
    MsgBox rngToSave.Address(0, 0)                  'remove after testing
 [COLOR=#ff0000][I]   sh.ShowAllData[/I][/COLOR]
    [COLOR=#ff0000]On Error GoTo [/COLOR][COLOR=#ff0000]0[/COLOR]
Excel 2016 (Windows) 32 bit
A
B
C
D
E
F
G
H
I
1
List
FATHER'S MOBILE
CENTRE
MOTHER'S MOBILE
ADDRESS2
Rand01
Rand02
Rand03
Mobiles
2
1​
999​
Q
123​
ADDE1F01G01H01
2​
3
2​
888​
W
234​
ADDE2F02G02H02
2​
4
3​
777​
EADDE3F03G03H03
1​
5
4​
R
345​
ADDE4F04G04H04
1​
6
5​
AADDE5F05G05H05
0​
7
6​
BADDE6F06G06H06
0​
8
7​
C
456​
ADDE7F07G07H07
1​
9
8​
333​
T
567​
ADDE8F08G08H08
2​
10
9​
222​
YADDE9F09G09H09
1​
11
10​
111​
U
678​
ADDE10F10G10H10
2​
Sheet: Master
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,545
Messages
6,125,455
Members
449,228
Latest member
moaz_cma

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