Results 1 to 3 of 3

Thread: VBA Code to extract subtotals

  1. #1
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13

    VBA Code to extract subtotals

    I am looking for VBA code to extract the row number and sub-total in Col B in sheet 1, where the value is not zero for eg if the sub-total value is for eg 50, -50 etc., but to ignore zeroes

    I would like row numbers containing the sub-total values that are not zero to be extracted to sheet2.

    I have manually extracted the Row # in Col A and the value in Col B on Sheet2 to give an idea of what the extraction must look like

    Your assistance in this regard is most appreciated


    VBA Code to extract subtotals
    Attached Files Attached Files

  2. #2
    Member Charles's Avatar
    Join Date
    Aug 2012
    Posts
    63
    Rep Power
    13
    HI,

    The following code may help.

    Code:
    Sub Total_Extraction()
    Application.ScreenUpdating = False
    Dim c As Variant
    Dim Ffind As Long
    Dim Slrow As Long
    ''' Doing seach for the formula "SubTotal ''
    With Sheets("Sheet1").Range("B1:B" & Sheets("Sheet1").Range("B65536").End(xlUp).Row)
        Set c = .Find("SUBTOTAL", Lookat:=xlPart)
        If Not c Is Nothing Then
            '''' get row nr and Value in cell copy to sheet2 ''
            Ffind = c.Row
            '' See if value < 0 '' if it is finish code''
            If Cells(c.Row, 2).Value < 0 Then
                Slrow = Sheets("Sheet2").Range("A65536").End(xlUp).Row + 1
                Sheets("Sheet2").Range("A" & Slrow).Value = c.Row
                Sheets("Sheet2").Range("B" & Slrow).Value = Sheets("Sheet1").Cells(c.Row, 2).Value
            End If
            Do
                Set c = .FindNext(c)
                If c.Row = Ffind Then Exit Sub
                If Cells(c.Row, 2).Value < 0 Then
                Slrow = Sheets("Sheet2").Range("A65536").End(xlUp).Row + 1
                Sheets("Sheet2").Range("A" & Slrow).Value = c.Row
                Sheets("Sheet2").Range("B" & Slrow).Value = Sheets("Sheet1").Cells(c.Row, 2).Value
            End If
            Loop While c.Row <> Ffind
        End If
    End With
    
    End Sub

  3. #3
    Senior Member
    Join Date
    Apr 2012
    Posts
    193
    Rep Power
    13
    Hi Charles

    Thanks for the help, much appreciated

    I made one small change , I needed to include positive and negative calues <> zero -see snippet of code below


    If Cells(c.Row, 2).Value <> 0 Then ....

    If Cells(c.Row, 2).Value <> 0 Then .....

    Are there any good VBA books your can recommend for novices?

Similar Threads

  1. VBA To Extract Email Address From Text
    By dunndealpr in forum Excel Help
    Replies: 43
    Last Post: 06-05-2019, 03:56 PM
  2. Calculating Subtotals Within Groups of Data Using UNION
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 1
    Last Post: 05-24-2013, 11:54 AM
  3. Replies: 2
    Last Post: 03-21-2013, 08:51 PM
  4. VBA Code to Extract data
    By Howardc in forum Excel Help
    Replies: 1
    Last Post: 07-24-2012, 11:37 PM
  5. VBA Function To Extract Decimal Numbers
    By PcMax in forum Excel Help
    Replies: 7
    Last Post: 11-19-2011, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •