使用VBA和Excel计算可变数据大小的平均值

时间:2019-02-06 11:53:06

标签: excel vba

我的任务是将多个文本文件上传到多个工作表并计算低于该数据的平均值。一些文本文件有200行,有些文本文件有5行, 每行中数据的数量不同。我已经成功地将每个文件分开,但是我无法使函数取平均值。txt文件的示例:https://ufile.io/7ii41

Sub CombineTextFiles()
    Dim xFilesToOpen As Variant
    Dim I As Integer
    Dim xWb As Workbook
    Dim xTempWb As Workbook
    Dim xDelimiter As String
    Dim xScreen As Boolean
    Dim lRow As Long
    Dim lCol As Long
    Dim Rws As Long
    Dim Col As Integer
    Dim r As Range
    Dim FrNg As Range
    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    xFilesToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt", , "Open", , True)
    If TypeName(xFilesToOpen) = "Boolean" Then
        MsgBox "No files were selected", , "Error"
        GoTo ExitHandler
    End If
    I = 1
    Set xTempWb = Workbooks.Open(xFilesToOpen(I))
    xTempWb.Sheets(1).Copy
    Set xWb = Application.ActiveWorkbook
    xTempWb.Close False
    xWb.Worksheets(I).Columns("A:A").TextToColumns _
      Destination:=Range("A1"), DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, _
      ConsecutiveDelimiter:=False, _
      Tab:=False, Semicolon:=False, _
      Comma:=True, Space:=False, _
      Other:=False
      lRow = Cells.Find(what:="*", _
                    after:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
     average=??

    Do While I < UBound(xFilesToOpen)
        I = I + 1
        Set xTempWb = Workbooks.Open(xFilesToOpen(I))
        With xWb
            xTempWb.Sheets(1).Move after:=.Sheets(.Sheets.Count)
            .Worksheets(I).Columns("A:A").TextToColumns _
              Destination:=Range("A1"), DataType:=xlDelimited, _
              TextQualifier:=xlDoubleQuote, _
              ConsecutiveDelimiter:=False, _
              Tab:=False, Semicolon:=False, _
              Comma:=True, Space:=False, _
              Other:=False
        End With
        lRow = Cells.Find(what:="*", _
                    after:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
    average=??

    Loop

ExitHandler:
    Application.ScreenUpdating = xScreen
    Set xWb = Nothing
    Set xTempWb = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description, , "Error"
    Resume ExitHandler
End Sub

1 个答案:

答案 0 :(得分:0)

您可以将以下两个快速功能添加到工作簿中,以计算列的平均值。

Private Function ColumnAvg(colRng As Range)

    ColumnAvg = 0
    On Error Resume Next

    ColumnAvg = Application.WorksheetFunction.Sum(Columns(colRng.Column)) / _ 
                Application.WorksheetFunction.CountA(Columns(colRng.Column))

End Function

Private Function RowAvg(rowRng As Range)

    RowAvg = 0
    On Error Resume Next

    RowAvg = Application.WorksheetFunction.Sum(Rows(rowRng.Row)) / _ 
             Application.WorksheetFunction.CountA(Rows(rowRng.Row))

End Function

注意:这些标记为私有函数,因为它们将计算整个列或整个行。这些功能只能在VBA中而不是在表单中调用。确保将这些功能放在同一模块中。

代码示例:

1-x = ColumnAvg(Range("A1"))

2-{{​​1}}

3-x = ColumnAvg(Range("A:A"))

如果要计算整个工作表的平均值,则可以使用此功能...

x = ColumnAvg(Cells(1,1))