Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SubTotal Code

Status
Not open for further replies.

truitt20

Technical User
Dec 10, 2004
74
US
Hi All,

Currently I have the following code which works great at subtotaling column"D:D" in my sheet:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lRow As Long, WF As WorksheetFunction
Set WF = Application.WorksheetFunction
If Not Application.Intersect(Target, Range("D:D")) Is Nothing Then
With Sheet5
lRow = .[A1].CurrentRegion.Rows.Count + 1
.Cells(lRow, 1) = Now
.Cells(lRow, 2) = WF.Subtotal(9, Sheet4.Range("D:D"))
End With
End If
Set WF = Nothing
End Sub


Another question for y'all. this code works perfect, but how do I only subtotal column "d:d" based on criteria in column "AF". For example I want to subtotal "D:D" but only for the rows where column "AF" has <>"*JAPAN*"?

thanks
 
Hi there,

Don't really have the data to test with, but you might try ...


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lRow As Long, ws As Worksheet, rng As String, wf As WorksheetFunction
    Set ws = Sheets("Sheet5")
    Set wf = Application.WorksheetFunction
    If Application.Intersect(Target, Me.Range("D:D")) Is Nothing Then Exit Sub
    lRow = ws.Range("A:A").Find("*", after:=ws.Cells(1, 1), _
        searchorder:=xlByRows, searchdirection:=xlPrevious)
    ws.Cells(lRow, 1) = Now
    rng = "Sheet4!D:D"
    ws.Cells(lRow, 2) = Evaluate("=SUMPRODUCT(NOT(ISNUMBER(SEARCH(" & rng & ",""JAPAN"")))*" & _
        "SUBTOTAL(9,OFFSET(" & rng & ",ROW(" & rng & ")-2,0,1,1)))")
End Sub

HTH

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top