Applying the same filter and operations to multiple sheets

colormestacy

New Member
Joined
Mar 27, 2023
Messages
8
Office Version
  1. 2021
Platform
  1. MacOS
Hi there,

New here, but I'm wondering if there's a time saving solution to a data processing issue I've encountered.

I have multiple files with 20+ sheets. All sheets have the same headers; they correspond to measurements from different experiments.

I need to do the following to each:

1) Filter 'Area' column for measurements between 10 and 100
2) Calculate the AVERAGE of all 'Area' column measurements post-filter
3) Calculate the AVERAGE of all 'Intensity' column measurements post-filter
4) Divide the the result from (3) by the result from (2)
5) Create a new sheet where the results from (2), (3), and (4) from each sheet are listed in a single row of three different columns

A challenge is that each sheet has different measurements, so the filter will produce different numbers of rows per sheet.

Any thoughts on how to create a macro to expedite this would be greatly appreciated.

Stace
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi Stace & welcome to the the forum!
What you're asking for is very achievable, but rather than have helpers guess as to the layout etc. of your sheets, could you please provide a sample sheet using the XL2BB add in, or better still, share a file via Google Drive, Dropbox or something similar? Also, with regard to "measurements between 10 and 100" - do you mean inclusive of those figures or exclusive, in other words, equal to and greater than 10 or just greater than 10?
 
Upvote 0
Hi Stace & welcome to the the forum!
What you're asking for is very achievable, but rather than have helpers guess as to the layout etc. of your sheets, could you please provide a sample sheet using the XL2BB add in, or better still, share a file via Google Drive, Dropbox or something similar? Also, with regard to "measurements between 10 and 100" - do you mean inclusive of those figures or exclusive, in other words, equal to and greater than 10 or just greater than 10?
Thanks, Kevin!

I mean greater than and less than.

Here's the top of the data from one sheet:

20x_tile_dapi-647_555_8_Stitch_PlaqueComp.tif_555on555_master_FILTERED10-100.xlsx
ABCDEFG
1 AreaMeanMinMaxIntDenRawIntDen
216.5926810.835410812944895.8131042057
329.34911805.677543220242110373.552561832
436.9367229.2675407854150145.7941163912
5418.448487.28544213301156504.4473632556
653.3176649.3255424779122058.838511998
7611.9779308.694541014275111493.0832587817
878.0576348.5035492784851147.8381187170
9821.75716708.095540134919363523.6588437588
10918.0099241.014543020679166421.8293862744
111040.58518494.136541248773750584.01617421476
12114.0077045.3445450932628229.262655217
131226.3247835.638541812210206267.0964787575
141394.31113553.9775407442091278282.5929669656
151423.1797211.34540511041167152.4023879701
16156.41911860.38954872522076137.6691767198
17164.2656194.3645400672126420.818613242
181713.01137430.507540665535487020.24311304013
19185.4296073.4685402687032970.207765257
201923.9989203.551541414436220864.0295126378
21205.47211518.37855452071063024.51462834
22215.2566554.1725562776134450.223799609
232243.1277295.988539910045314653.497303284
242315.8988502.412542614053135170.7963137390
252428.6949938.281544417447285167.3876618895
262551.18411788.289540526942603367.02214004487
272619.9057998.139539812745159200.8063695140
282720.4226732.69254049605137493.2743191296
29284.1798540.84554231194235693.321828462
30299.9098363.41355501193082875.4211923585
313027.05711049.924541518675298973.9046939352
32314.0936225.3685402801925480.211591410
333212.3657420.4395450989691754.1812129666
34333.1455697.2195397607117918.438415897
35345.7737207.36654261109841609.809965787
36358.3589864.54156051501782450.4421913721
373613.1847912.399541914934104314.3252421194
383711.2888142.82154061285991915.8742133419
39386.2046579.6945432798040820.901947476
reg_20x_tile_dapi-647_555_8_Sti
 
Upvote 0
Thanks Stace (y)
I'll get onto this later today (local time) and can I assume all sheets have the same columns as you example? Is "Intensity" column F?
 
Upvote 0
Hi Stace & welcome to the the forum!
What you're asking for is very achievable, but rather than have helpers guess as to the layout etc. of your sheets, could you please provide a sample sheet using the XL2BB add in, or better still, share a file via Google Drive, Dropbox or something similar? Also, with regard to "measurements between 10 and 100" - do you mean inclusive of those figures or exclusive, in other words, equal to and greater than 10 or just greater than 10?

Thanks Stace (y)
I'll get onto this later today (local time) and can I assume all sheets have the same columns as you example? Is "Intensity" column F?

Yes, and it's G. Thanks!
 
Upvote 0
This might take a few goes to get it just right, but here's a start. Put the code in a Standard module in its own file & save it as a macro-enabled workbook. Test it on a copy of one of your files & see if it does what you expect. The code first asks the user to open the file to be summarised (which I expect will be closed until you run the code). I took the liberty of calling the summary sheet "Summary" and placing it as the first sheet of the file with the data - that can be changed if you wish. Also look at the number format of the values in the summary sheet - can also be changed to whatever you want.
Let me know how it goes.

VBA Code:
Option Explicit
Sub Stace()
    'Declare variables
    Dim wb As Workbook, ws As Worksheet
    Dim Filename As String, n As String, i As Long, j As Long
    Dim a As Double, b As Double, c As Double
    
    'Get user to open file
    Filename = Application.GetOpenFilename _
    (filefilter:="Excel files (*.xlsx),*.xlsx", MultiSelect:=False)
    Set wb = Workbooks.Open(Filename)
    
    'Check if the sheet "Summary" already exists
    'If it does - clear it; If it doesn't - create it
    For i = 1 To wb.Worksheets.Count
        If wb.Worksheets(i).Name = "Summary" Then
            j = 1
        End If
    Next i
    If j = 1 Then
        Set ws = wb.Worksheets("Summary")
        ws.Cells.ClearContents
    Else
        wb.Worksheets.Add(before:=wb.Worksheets(1)).Name = "Summary"
        Set ws = wb.Worksheets("Summary")
    End If
    
    'Add headers to Summary Sheet
    With ws.Cells(1).Resize(, 4)
        .Value = Array("Sheet", "Area (avg)", "Intensity (avg)", "Intensity/Area")  '<~~ change as you see fit
        .ColumnWidth = 30
        .Font.Bold = True
    End With
    
    'Loop through each sheet extracting values & placing in Summary sheet
    For i = 2 To wb.Worksheets.Count
        Set ws = wb.Worksheets(i)
        n = ws.Name
        a = WorksheetFunction.AverageIfs(ws.Columns(2), ws.Columns(2), ">10", ws.Columns(2), "<100")
        b = WorksheetFunction.AverageIfs(ws.Columns(7), ws.Columns(2), ">10", ws.Columns(2), "<100")
        c = b / a
        
        With wb.Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)
            .Resize(, 4).Value2 = Array(n, a, b, c)
        End With
    Next i
    
    'Tidy up
    With wb.Worksheets(1)
        .Columns("A:D").AutoFit
        .Columns("B:D").NumberFormat = "0.00"   '<~~ change to your desired number format
    End With
    Application.Goto Reference:=wb.Worksheets(1).Range("A1"), scroll:=True
End Sub
 
Upvote 0
Just added a trap if no file selected:
VBA Code:
Option Explicit
Sub Stace_V2()
    'Declare variables
    Dim wb As Workbook, ws As Worksheet
    Dim Filename, n As String, i As Long, j As Long
    Dim a As Double, b As Double, c As Double
    
    'Get user to open file
    Filename = Application.GetOpenFilename _
    (filefilter:="Excel files (*.xlsx),*.xlsx", MultiSelect:=False)
    If Filename = False Then
        MsgBox "No file selected"
        Exit Sub
    End If
    Set wb = Workbooks.Open(Filename)
    
    'Check if the sheet "Summary" already exists
    'If it does - clear it; If it doesn't - create it
    For i = 1 To wb.Worksheets.Count
        If wb.Worksheets(i).Name = "Summary" Then
            j = 1
        End If
    Next i
    If j = 1 Then
        Set ws = wb.Worksheets("Summary")
        ws.Cells.ClearContents
    Else
        wb.Worksheets.Add(before:=wb.Worksheets(1)).Name = "Summary"
        Set ws = wb.Worksheets("Summary")
    End If
    
    'Add headers to Summary Sheet
    With ws.Cells(1).Resize(, 4)
        .Value = Array("Sheet", "Area (avg)", "Intensity (avg)", "Intensity/Area")  '<~~ change as you see fit
        .ColumnWidth = 30
        .Font.Bold = True
    End With
    
    'Loop through each sheet extracting values & placing in Summary sheet
    For i = 2 To wb.Worksheets.Count
        Set ws = wb.Worksheets(i)
        n = ws.Name
        a = WorksheetFunction.AverageIfs(ws.Columns(2), ws.Columns(2), ">10", ws.Columns(2), "<100")
        b = WorksheetFunction.AverageIfs(ws.Columns(7), ws.Columns(2), ">10", ws.Columns(2), "<100")
        c = b / a
        
        With wb.Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Offset(1)
            .Resize(, 4).Value2 = Array(n, a, b, c)
        End With
    Next i
    
    'Tidy up
    With wb.Worksheets(1)
        .Columns("A:D").AutoFit
        .Columns("B:D").NumberFormat = "0.00"   '<~~ change to your desired number format
    End With
    Application.Goto Reference:=wb.Worksheets(1).Range("A1"), scroll:=True
End Sub
 
Upvote 0
Whew! Thanks, Kev.

I copy-pasted the code onto a Standard module in a new .xlsm file I created in a test directory with a copy of one of the .xlsx files I want to analyze.

I ran it, but it said

Run-time error '1004'

Method 'GetOpenFilename' of object '_Application' failed
 
Upvote 0
Whew! Thanks, Kev.

I copy-pasted the code onto a Standard module in a new .xlsm file I created in a test directory with a copy of one of the .xlsx files I want to analyze.

I ran it, but it said

Run-time error '1004'

Method 'GetOpenFilename' of object '_Application' failed
When you pressed Debug, which particular line of code was highlighted yellow?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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