-1

What I am trying to to achieve is the max value in all sheets in my workbook and gather them in an specific sheet My vba code is working for one specific cell and when I have tried to add for loops Nothing happen and my excel would be not respond and freeze I will be thankful if any one could help.

Dim wsDst As Worksheet
Dim ws As Worksheet
Dim x As Long
Dim lngMax As Long
Set wsDst = Sheets("Summary")
 Application.ScreenUpdating = False
  For Each ws In ActiveWorkbook.Worksheets

    If ws.Name <> wsDst.Name And ws.Name <> "Amirhossein" Then
       For ZZ = 4 To 9999
        For Q = 25 To 9999
         With ws
            x = Application.WorksheetFunction.max(.Cells(ZZ, 26))
            If x > lngMax Then
                wsDst.Cells(Q, 10).Value = x
                lngMax = wsDst.Cells(Q, 10).Value
            End If
        End With
    Next Q
    Next ZZ

    End If
Next ws
  • Do you know extracting the max `Long` value of each sheet, or for each column in each sheet. If for each column, where would you like to return this maximum value? Do all the involved sheets have the same number of columns? – FaneDuru Jan 01 '21 at 19:03
  • Yes they all have the same number of column and the cell that i want it max value among the sheets is constant – AmirHossein K.Baghery Jan 01 '21 at 21:10

1 Answers1

0

Try the next version, please. It checks each value of cells value from X:Z columns and extract Max, which is placed in the same cell of 'Summary' sheet:

Sub testMaxXZMultipleSheets()
  Dim sh As Worksheet, wsDst As Worksheet, arr, arrRng
  Dim k As Long, i As Long, j As Long
  
  Set wsDst = Sheets("Summary")
  ReDim arr(ThisWorkbook.Worksheets.Count - 1) 'redim the array to the maximum number of sheets
  For Each sh In ThisWorkbook.Sheets           'put all sheet objects in the arr array
    If sh.Name <> wsDst.Name And sh.Name <> "Amirhossein" Then
        Set arr(k) = sh: k = k + 1
    End If
  Next
  ReDim Preserve arr(k - 1) 'keep only the array elements keeping a sheet object
  For j = 24 To 26          'iterate only between columns X:Z (24:26):
    For m = 4 To arr(1).Range("X" & Rows.Count).End(xlUp).Row 'it assumes that all shets have the same number of rows
        ReDim arrRng(UBound(arr))
        For i = 0 To UBound(arr)        'create an array of each value of the same cell for all sheets in arr array
          arrRng(i) = IIf(IsError(arr(i).Cells(m, j).Value), 0, arr(i).Cells(m, j).Value)
        Next i
        wsDst.Cells(m, j).Value = WorksheetFunction.max(arrRng) 'put the Max value in the same 'Summary' position
    Next m
 Next j
 MsgBox "Ready..."
End Sub

Please, send some feedback after testing it.

FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • Thank FaneDuru for the answer, the first code is suitable for me but I have got overflow error by running it and debugging shows this line: lngMax = Application.WorksheetFunction.Max(ws.UsedRange) – AmirHossein K.Baghery Jan 01 '21 at 20:42
  • @AmirHossein K.Baghery: Are there hidden or protected sheets involved? – FaneDuru Jan 01 '21 at 21:25
  • @ FaneDuru No i don't have any hidden or protected sheet – AmirHossein K.Baghery Jan 02 '21 at 07:35
  • I am afraid there are errors (formula errors) in one of more sheets. I will try adapting the code to check it previously and warn. – FaneDuru Jan 02 '21 at 10:45
  • @AmirHossein K.Baghery: Please, test the updated code. It will stop and activate the sheet having (formula) errors and show the problematic cell addresses. Please send some feedback after testing. – FaneDuru Jan 02 '21 at 13:04
  • ,I have got no cell were found error and debugging shows this line : Set rngErr = ws.UsedRange.SpecialCells(xlCellTypeFormulas, xlErrors) – AmirHossein K.Baghery Jan 02 '21 at 13:22
  • @AmirHossein K.Baghery: This means that not an error looks to be your workbook problem. If you do not share the workbook, I am afraid I cannot imagine where the problem is... If not something confidential, please use a transfer site. [Here](https://easyupload.io/) is a free one, easy to be used. Without that, I cannot imagine where the problem is... – FaneDuru Jan 02 '21 at 13:27
  • thank you so much for your time shared link: https://easyupload.io/gdcmra if you check the tab Summary you see i extract max with aggregate but any time i run the macro new sheet will be create and i should add new address in new sheet manually :( If you check macro code i add your code in name of ExtractMaxAllSheets.Thanks again – AmirHossein K.Baghery Jan 02 '21 at 16:39
  • @AmirHossein K.Baghery: OK. I will download it and come back in some minutes... – FaneDuru Jan 02 '21 at 17:04
  • @AmirHossein K.Baghery: There are some "#N/A" strings in your problematic sheets and VBA considers them as errors (which they used to be...). But not coming from a formula, it cannot be found using `SpecialCells`. I will adapt the code to deal with such a situation, but you shouldn't transform the formula result in its value. You should deal with `ISNA` formula. Something like `=IF(ISNA(VLOOKUP(X23,X17:Y22,2,0)),0,VLOOKUP(X23,X17:Y22,2,0))`. Or let the formula returning the error "#N/A"... Anyhow, I will make the code dealing with your situation, too... – FaneDuru Jan 02 '21 at 17:16
  • You can download the solved workbook from [Here](https://easyupload.io/ylzymw). It has a button on the Summary sheet. When pressing it, the result will be placed as you tried, in columns "X:Z". I will also adapt the above code, too. Adapted. Please, test it and send some feedback. – FaneDuru Jan 02 '21 at 17:37
  • thank you so much but it is not the result that i want. What you did is extracting the max value in whole sheet individually but what i want is the max value of Range z4 in all sheet that their name is date, and followed by max value of z5 in all sheets, z6 and etc. And gather them in "summary" sheet . I need this solution for max value of cell y4 in all sheets that has name of date and followed by max value of y5, y6 and etc. and the last column is X4, X5 and etc...thanks alot – AmirHossein K.Baghery Jan 02 '21 at 18:06
  • @AmirHossein K.Baghery: I do not understand anything from what you say. Please copy between double quotes where did you ask about that. – FaneDuru Jan 02 '21 at 18:12
  • The code that i post does extract only the max value of cell z4 in sheets that their names are date, What i asked was add for loop to my code...Maybe I was not clear about the result I want. Sorry about that. – AmirHossein K.Baghery Jan 02 '21 at 18:22
  • @AmirHossein K.Baghery: Let us take it from the beginning. I could not understand anything from your code. So, you must accurately explain what you are trying to accomplish. Now I could understand that you need to find the maximum of "X4" from all sheets (except two of them), but I do not understand where this maximum to be returned. In "X4" of "Summary" sheet? Then, do you like processing only columns "X:Z"? And, please, explain where the maximum value to be returned. – FaneDuru Jan 02 '21 at 20:26
  • @AmirHossein K.Baghery: Please, test the updated version. – FaneDuru Jan 02 '21 at 21:23
  • Thank You so so much thats exactly what i wanted. You are a time saver. – AmirHossein K.Baghery Jan 03 '21 at 03:48