Assist with possible VBA macro

Castor

New Member
Joined
Mar 20, 2019
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
Hey all,

Tried the search function, did not turn up any results - I am wanting to see if a macro is possible that will delete certain rows - Rows that do not contain values that are adjacent to each other. For example:

Col 1Col 2Col 3Col 4Col 5Col 6Col 7Col 8
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
EEEEEEEEEEEEEEEE
FFFFFFFFFFFFFFFFFFFFFFFF
GGGGGGGG
GGGGGGGG
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL


I am hoping for a macro that can delete the rows that are blank as well as the rows with values that are alone. ie: delete the blanks rows as well as the rows with A, B, D, E, F, J, K and L, and keep the rows with C and G since they are 3 and 2 rows adjacent to each other with values contained within.

Any assist is greatly appreciated! Please let me know if this is not clear.

Thank you
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If your data is in a Table, you could filter for the blank rows, select them, and delete them. For the duplicate rows or adjacent rows with data, you could add a helper column to test if it meets your criteria and then filter on that column to get the rows to be deleted. The order of operations is likely important so test it before deleting your data. Below is an example of the "helper column".
Book1.xlsx
ABCDEFGHI
1Col 1Col 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFALSE
3BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBFALSE
4CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCTRUE
5CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCTRUE
6CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCTRUE
7DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDFALSE
8EEEEEEEEEEEEEEEEFALSE
9FFFFFFFFFFFFFFFFFFFFFFFFFALSE
10GGGGGGGGTRUE
11GGGGGGGGTRUE
12JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJFALSE
13KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKFALSE
14LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLFALSE
Sheet6
Cell Formulas
RangeFormula
I2:I14I2=OR([@[Col 8]]=H1,[@[Col 8]]=H3)

Hope that helps,

Doug
 
Upvote 0
Try:
VBA Code:
Sub DeleteRows()
    Application.ScreenUpdating = False
    Range("A:A").SpecialCells(xlBlanks).EntireRow.Delete
    Dim v As Variant, i As Long, rng As Range, lCol As Long
    lCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    v = Range("A1", Range("A" & Rows.Count).End(xlUp))
    For i = LBound(v) To UBound(v)
        If WorksheetFunction.CountIf(Range("A:A"), v(i, 1)) = 1 Then
            If rng Is Nothing Then Set rng = Range("A" & i).Resize(, lCol) Else Set rng = Union(rng, Range("A" & i).Resize(, lCol))
        End If
    Next i
    rng.EntireRow.Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,416
Messages
6,124,774
Members
449,187
Latest member
hermansoa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top