Solved

Excel VBA - I would like VBA to detect the word "subtotal" in a row, delete that row, move down one row. repeat.

Posted on 2014-10-24
3
396 Views
Last Modified: 2014-11-13
I would like VBA to:
1.  Detect the word "subtotal" in a row,
2.  Delete that row,
3.  Move down one row.
4.  Go to step 1.
0
Comment
Question by:brothertruffle880
3 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 250 total points
Comment Utility
Here is the code :

i = 1
j = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Do While i < j

If ActiveSheet.Range("A" & i).Value = "subtotal" Then
ActiveSheet.Rows(i).Delete
i = i - 1
j = j - 1
Else
i = i + 1
End If

Loop

Open in new window

0
 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 250 total points
Comment Utility
This code will check all cells in the active sheet to see if "subtotal" exists in any row and remove that row if so:
Sub Delete_Subtotal_Rows()
    'based on code from dlmille, Experts Exchange Q_27636554
    Dim rng As Range
    Dim r As Range
    Dim chkRow As String
    Dim rng2 As Range
    Dim r2 As Range
    Dim rDelete As Range
    Dim lngLR, lngLC As Long

    lngLR = Cells.SpecialCells(xlLastCell).Row
    lngLC = Cells.SpecialCells(xlLastCell).Column
    
    Set rng = Range("A2", Cells.SpecialCells(xlLastCell))
    
    For Each r In rng
        Set rng2 = Range(Cells(r.Row, 1), Cells(r.Row, lngLC))
        For Each r2 In rng2
            chkRow = chkRow & Trim(r2.Value)
        Next r2
        If InStr(1, chkRow, "subtotal") > 0 Then
            If rDelete Is Nothing Then
                Set rDelete = r
            Else
                Set rDelete = Union(r, rDelete)
            End If
        End If
        chkRow = vbNullString
    Next r
    If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
End Sub

Open in new window


Regards,
-Glenn

PS Haris' code only checks cells in column A.
0
 
LVL 31

Expert Comment

by:Rob Henson
Comment Utility
Were the Subtotals created using the Subtotal Wizard?

If so, the Subtotal Wizard also has an option for removing Subtotals. This will remove the rows as required.

Thanks
Rob H
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now