【问题标题】:Calculating average of variable data size using VBA and Excel使用 VBA 和 Excel 计算可变数据大小的平均值
【发布时间】:2019-02-06 11:53:06
【问题描述】:

我的任务是将多个文本文件上传到多个工作表并计算低于该数据的平均值。有些文本文件有 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

【问题讨论】:

标签: excel vba


【解决方案1】:

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

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 - x = ColumnAvg(Range("A:A"))

3 - x = ColumnAvg(Cells(1,1))

如果你想计算整个工作表的平均值,那么你可以使用这个函数...

Private Function AvgEverything()
Dim Text As String

    Text = "A1:" & Split(Cells(1, ActiveSheet.Columns.Count).Address(True, False), "$")(0) _ 
                 & ActiveSheet.Rows.Count
    AvgEverything = Application.WorksheetFunction.Sum(Range(Text)) / _
                    Application.WorksheetFunction.CountA(Range(Text))

End Function

【讨论】:

  • 我需要计算所有数字的平均值,而不是逐行计算
  • 我刚刚更新了上面的答案,加入了第三个用于计算整个工作表的宏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多