VBA, the group's help, multi-line summary of the data (like PivotTable function of VBA code), has been ruled out some common error

1 group in question was asked for help

1.1 original problem and error

 

1.2 to get his original code 

Sub 多列汇总()
   Dim l(1 To 1000, 1 To 4)
   Dim arr, 行数
   Dim x, k As Integer
   Dim d As New Dictionary
       arr = Range("q1:t" & Range("t65536").End(xlUp).Row)
       For x = 1 To UBound(arr)
       If d.Exists(arr(x, 1)) Then
            行数 = d(arr(x, 1)) '字典的items赋给行数?
            l(行数, 2) = l(行数, 2) + arr(x, 2)
            l(行数, 3) = l(行数, 3) + arr(x, 3)
            l(行数, 4) = l(行数, 4) + arr(x, 4)
        Else
           k = k + 1 'k代表行数??
           d(arr(x, 1)) = k '把行数放进字典?
           l(k, 1) = arr(x, 1)
           l(k, 2) = arr(x, 2)
           l(k, 3) = arr(x, 3)
           l(k, 4) = arr(x, 4)
        End If
    Next x
    Range("v2").Resize(k, 4) = l
    Range("w" & Range("w65536").End(xlUp).Row + 1) = Application.WorksheetFunction.Sum("w3:w" & Range("w65536").End(xlUp).Row)
    Range("x" & Range("x65536").End(xlUp).Row + 1) = Application.WorksheetFunction.Sum("x3:x" & Range("x65536").End(xlUp).Row)
    Range("y" & Range("y65536").End(xlUp).Row + 1) = Application.WorksheetFunction.Sum("y3:y" & Range("y65536").End(xlUp).Row)
    Range("z" & Range("z65536").End(xlUp).Row + 1) = Application.WorksheetFunction.Sum("z3:z" & Range("z65536").End(xlUp).Row)
    Range(Range("w65536").End(xlUp).Row + 1) = "总计"
End Sub

 

Exclude meaning and common problems of this code is 2

2.1 The meaning of the code and my two days out of the array formulas and pivot tables is a basic purpose

  • According to the classification of data, data summary

 

2.2 error: can not get the sum attribute class worksheetfunction

  • This problem usually used as a worksheet function, when encountered bad data, robust enough
  • worksheetfunction.sum()
  • worksheetfunction.match()
  •  
  • Generally speaking
  • A function in the application based on the line
  • application.sum()
  • application.match()

 

The method given range 2.3 fails on an object acting _global

  • Range(Range("w65536").End(xlUp).Row + 1) = "总计"
  • This range (line number + 1) must be a syntax error, it should be written range ( "v" & () line number + 1)) job

 

 

Code and running of the 3 amendments

  • The basic meaning of the code is
  • Keyword taken key column, if the non-repetitive, written into the array (the array assigned to the following list of go), the line number which is written the dictionary,
  • If it is repeated, the line numbers according to the dictionary, the contents together
Sub 多列汇总()
   Dim l(1 To 1000, 1 To 4)
   Dim arr, 行数
   Dim x, k As Integer
   Dim d As New Dictionary
       arr = Range("q1:t" & Range("t65536").End(xlUp).Row)
       For x = 1 To UBound(arr)
       If d.Exists(arr(x, 1)) Then
            行数 = d(arr(x, 1)) '字典的items赋给行数?
            l(行数, 2) = l(行数, 2) + arr(x, 2)
            l(行数, 3) = l(行数, 3) + arr(x, 3)
            l(行数, 4) = l(行数, 4) + arr(x, 4)
        Else
           k = k + 1 'k代表行数??
           d(arr(x, 1)) = k '把行数放进字典?
           l(k, 1) = arr(x, 1)
           l(k, 2) = arr(x, 2)
           l(k, 3) = arr(x, 3)
           l(k, 4) = arr(x, 4)
        End If
    Next x
    Range("v2").Resize(k, 4) = l
    Range("w" & Range("w65536").End(xlUp).Row + 1) = Application.Sum(Range("w3:w" & Range("w65536").End(xlUp).Row))
    Range("x" & Range("x65536").End(xlUp).Row + 1) = Application.Sum(Range("x3:x" & Range("x65536").End(xlUp).Row))
    Range("y" & Range("y65536").End(xlUp).Row + 1) = Application.Sum(Range("y3:y" & Range("y65536").End(xlUp).Row))
    Range("z" & Range("z65536").End(xlUp).Row + 1) = Application.Sum(Range("z3:z" & Range("z65536").End(xlUp).Row))
    Range("v" & Range("w65536").End(xlUp).Row) = "总计"
End Sub

 

Published 415 original articles · won praise 46 · views 110 000 +

Guess you like

Origin blog.csdn.net/xuemanqianshan/article/details/104338337