Macro with filter criiteria

XOLLI

New Member
Joined
Mar 17, 2018
Messages
4
Hi,

I need some help, I am trying to create a macro where based on the data below it selects the values greater than 4 and then writes the rows and column in another part of the worksheet.

M1 M2 M3

M1 1 2 7

M2 8 1 9

M3 3 6 1



The macro then lists these as:

Item 1 Item 2
M1 M3
M2 M1
M2 M3
M3 M2


Any idea?


Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this for results on sheet2.
Code:
[COLOR="Navy"]Sub[/COLOR] MG17Mar13
 [COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nRay()
    Ray = ActiveSheet.Cells(1).CurrentRegion
     c = 1
        ReDim nRay(1 To 2, 1 To c)
            nRay(1, 1) = "Item1": nRay(2, 1) = "Item2"
[COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
   [COLOR="Navy"]For[/COLOR] ac = 2 To UBound(Ray, 2)
        [COLOR="Navy"]If[/COLOR] Ray(n, ac) > 4 [COLOR="Navy"]Then[/COLOR]
            c = c + 1
            ReDim Preserve nRay(1 To 2, 1 To c)
                nRay(1, c) = Ray(n, 1)
                nRay(2, c) = Ray(1, ac)
         [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] ac
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2").Range("A1").Resize(c, 2)
    .Value = Application.Transpose(nRay)
    .Borders.Weight = 2
    .Columns.AutoFit
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick

Thanks for that, however I am receiving an error its says Run-time error '13'
Type Mismatch
 
Upvote 0
Thanks a lot!

Please can you explain how the function works so can use it for a similar example I have??
 
Upvote 0
The Code:- If your data hasmore rows or more columns, it should not matter as the code takes care of that.

Inthe code "Ray" (at top of code) is an array of the Current Region starting "A1",that is the group/Area of cells that will cover all columns across starting column 1, until a blank column ismet and all rows down starting row 1, until a blank row is met.
Youdon't need to alter anything except perhaps the destination for the results(Sheet2) as shown at the bottom of the code.

Youcan try this for multi columns and multi rows as long as the basic format isthe same, it should work .
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,117
Members
452,545
Latest member
boybenqn

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