简体   繁体   English

查找动态范围的列和行 VBA 的最小值、最大值和平均值

[英]Finding min, max and average of a dynamic range of columns and rows VBA

I am trying to find the MIN , MAX and AVERAGE of each column ( Note: number of columns varies each day).我试图找到每MINMAXAVERAGE注意:列数每天都不同)。 A summary table, on the next worksheet, of the MIN , MAX and AVERAGE of each column is the desired output.在下一个工作表上,每列的MINMAXAVERAGE的汇总表是所需的输出。 Each of the columns has a unique serial number (As per summary information in photo below).每列都有一个唯一的序列号(根据下面照片中的摘要信息)。

Also, I am trying to find the MIN , MAX and AVERAGE of each row ( Note: number of rows also varies each day).另外,我试图找到每一MINMAXAVERAGE注意:行数每天也不同)。 These values are desired to be listed in Columns CZ (min), DA (Max), DB(Average).这些值需要列在 CZ(最小值)、DA(最大值)、DB(平均值)列中。

Please find my attempt below, which gave the following error:请在下面找到我的尝试,它给出了以下错误:

"Run-time error '1004': Unable to get the Min property of the WorksheetFunction class" “运行时错误‘1004’:无法获取 WorksheetFunction 类的 Min 属性”

I have attempted this with formulas but the excel sheet slows down dramatically, so was hoping VBA would speed the processing up.我已经用公式尝试过这个,但 Excel 工作表的速度会大大减慢,所以希望 VBA 能够加快处理速度。 I would have estimated 2000-4000 rows and up to 100 columns of temperature data.我估计有 2000-4000 行和多达 100 列的温度数据。

Sub Range_End_Method()
'Finds the last non-blank cell in a single row or column

Dim Row As Long
Dim Col As Long
Dim MinValue As Integer
Dim Min_Values As Range
Dim Cycle As Integer
Dim RangeNew As Variant

    'Find the last non-blank cell in column C(1)
    Row = ThisWorkbook.Sheets("1. Paste Raw Data").Cells(Rows.Count, 3).End(xlUp).Row

    'Find the last non-blank cell in row 9
    Col = ThisWorkbook.Sheets("1. Paste Raw Data").Cells(9, Columns.Count).End(xlToLeft).Column

For i = 3 To Col

    RangeNew = Range(Cells(9, i).Address, Cells(Row, i).Address).Address
    MinValue = Application.WorksheetFunction.Min(RangeNew)
    Cycle = 3
    MinValue = Sheets("5. Summary Information").Cells((Cycle + i), i)

    Next

End Sub

在此处输入图片说明

在此处输入图片说明

I don't think the code below will do what you want but it features correct syntax for setting a range and for determining the last used row and column in a worksheet.我不认为下面的代码会做你想要的,但它具有用于设置范围和确定工作表中最后使用的行和列的正确语法。 Given the correct syntax you may now be able to introduce the correct logic.给定正确的语法,您现在可以引入正确的逻辑。

Sub Range_End_Method()
    ' Variatus@STO 22 Feb 2020

    Dim MinValue As Integer
    Dim MinRng As Range
    Dim Cycle As Integer
    Dim Rl As Long                      ' last used row
    Dim Cl As Long                      ' last used column
    Dim i As Integer

    'Find the last non-blank cell in column C(1)
    ' rows and columns must be counted in the same sheet!
    With ThisWorkbook.Sheets("1. Paste Raw Data")
        Rl = .Cells(.Rows.Count, 3).End(xlUp).Row

        'Find the last non-blank cell in row 9
        Cl = .Cells(9, .Columns.Count).End(xlToLeft).Column

        Cycle = 3
        For i = 3 To Cl
            Set MinRng = .Range(.Cells(9, i), .Cells(Rl, i))
            Debug.Print MinRng.Address
            MinValue = Application.WorksheetFunction.Min(MinRng)
            Sheets("5. Summary Information").Cells((i + Cycle), i).Value = MinValue
        Next i
    End With
End Sub

I draw your attention to the line Debug.Print MinRng.Address which I have added to support your testing.我提请您注意我为支持您的测试而添加的Debug.Print MinRng.Address行。 This line will print the address of the MinRange to the Immediate window on each iteration and you can see if it is what you intend.这一行将在每次迭代时将 MinRange 的地址打印到立即窗口,您可以查看它是否是您想要的。

Note also that I have reversed your code for writing the MinValue to sheet 5. It appeared more logical to me this way but I doubt that the coordinates of the target cell are specified correctly.另请注意,我已将您用于将 MinValue 写入工作表 5 的代码颠倒了。这种方式对我来说似乎更合乎逻辑,但我怀疑目标单元格的坐标是否正确指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM