简体   繁体   中英

Excel macro by group

The table example

  column1     | column2 |  column3
    --------------------------------------------
              |         |
      A       |     1   |  
      A       |     2   |  
      A       |     3   |
              |         | 
      B       |     4   |  
      B       |     4   |
              |         | 
      C       |     1   |  
      C       |     1   |  
      C       |     5   |  

As you can see, they are grouped in the first column, second column is some value. I have no knowledge of VB and I haven't been able to figure it out myself for over an hour.

What I want to do:

For each group:

count how many values are less than 2.

If there is 1 or more such values, set column3 in the blank row above the group to (numbers of values under 2 in group / total number of values in group)

If there are none, do nothing.

Output of above table would be:

  column1     | column2 |  column3
    --------------------------------------------
              |         |   1/3
      A       |     1   |  
      A       |     2   |  
      A       |     3   |
              |         | 
      B       |     4   |  
      B       |     4   |
              |         |   2/3
      C       |     1   |  
      C       |     1   |  
      C       |     5   |  

Making some assumptions (that your column numbers were the same as in the example and that there is always a blank row between groups, here's a snippet to start with:

Sub Test2()
Dim LastRow As Integer, First As Integer, Last As Integer
LastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious, After:=[A1]).Row + 1
First = 3
For i = 3 To LastRow
    If Cells(i, 1).Value = "" Then
        Last = i - 1
        Cells(First - 1, 3).Value = Summ(First, Last)
        First = i + 1
    End If
Next i
End Sub
Private Function Summ(First As Integer, Last As Integer)
Dim Count As Integer, Total As Integer
For t = First To Last
    Total = Total + 1
    If Cells(t, 2).Value < 2 Then Count = Count + 1
Next t
Summ = Count / Total
End Function
Sub calculatebygroups()
    last_row = Cells.SpecialCells(xlCellTypeLastCell).Row
    group_counter = 0
    value_counter = 0
    row_to_write = 0
    For n = 1 To last_row + 1
        If Range("A" & n).Value = "" Then
            If row_to_write <> 0 And value_counter <> 0 Then
                Range("C" & row_to_write).Value = "'" & value_counter & "/" & group_counter
            End If
            row_to_write = n
            group_counter = 0
            value_counter = 0
        Else
            group_counter = group_counter + 1
            If Range("B" & n).Value < 2 Then
                value_counter = value_counter + 1
            End If
        End If
    Next n
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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