嘗試粘貼到VB中的新模塊。
這假定你的原始數據是Sheet(1)
和排序的數據將在一個新的空白工作表Sheet(2)
Sub AddNumbers()
Dim RowNo, ColNo As Long
'Skip Header Row
RowNo = 2
Do Until Sheets(1).Cells(RowNo, 1) = ""
If Sheets(1).Cells(RowNo, 2) <= 30 Then
Sheets(2).Cells(1, 1) = "30 or less"
ColNo = 1
Sheets(2).Cells((Sheets(2).Cells(Rows.Count, 1).End(xlUp).row + 1), ColNo) = Sheets(1).Cells(RowNo, 3)
ElseIf Sheets(1).Cells(RowNo, 2) > 30 And Sheets(1).Cells(RowNo, 2) <= 60 Then
Sheets(2).Cells(1, 2) = "Between 30 and 60"
ColNo = 2
Sheets(2).Cells((Sheets(2).Cells(Rows.Count, 2).End(xlUp).row + 1), ColNo) = Sheets(1).Cells(RowNo, 3)
ElseIf Sheets(1).Cells(RowNo, 2) > 60 And Sheets(1).Cells(RowNo, 2) <= 90 Then
Sheets(2).Cells(1, 3) = "Between 60 and 90"
ColNo = 3
Sheets(2).Cells((Sheets(2).Cells(Rows.Count, 3).End(xlUp).row + 1), ColNo) = Sheets(1).Cells(RowNo, 3)
End If
RowNo = RowNo + 1
Loop
' Add Subtotals
ColNo = 1
Do Until Sheets(2).Cells(1, ColNo) = ""
Sheets(2).Cells((Sheets(2).Cells(Rows.Count, ColNo).End(xlUp).row + 1), ColNo).Formula = "=SUM(" & Col_Letter(ColNo) & "2:" & Col_Letter(ColNo) & (Sheets(2).Cells(Rows.Count, ColNo).End(xlUp).row) & ")"
Sheets(2).Cells((Sheets(2).Cells(Rows.Count, ColNo).End(xlUp).row), ColNo).Font.Bold = True
ColNo = ColNo + 1
Loop
End Sub
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
這可以使用SUMIF()或SUMIFS()的公式來完成。是否有VBA的理由? –
請發佈您迄今爲止編寫的VBA代碼。如果您還沒有任何VBA代碼,那麼您可能需要查看以下解釋VBA編程和數組[適用於初學者](http://www.homeandlearn.org/index.html)的網站:http: //www.homeandlearn.org/excel_vba_and_arrays.html – Ralph
@ScottCraner我在VBA srcip中需要這個,因爲我不知道我要搜索數字<30條件的區域有多大。我希望excel告訴用戶每個ID在這個數字<30條件下的總值是多少。這個例子很簡單,因爲我正在用它來練習一個更大的問題。我將pst的代碼是迄今爲止我寫的實際宏。循環是我嘗試讓循環做某事的失敗嘗試。我試圖做一些簡單而失敗的事情。請幫助 –