Delete row based on call value & move rows up

yobee

New Member
Joined
Mar 22, 2022
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hello & Thanks in advance for any help. I trying to write a macro to delete rows based on column "A" cell value of "2" then shift the undeleted rows up. Here's what I have so far but maybe I am over cmplicating things...

Sub DeleteRows()
Dim Matrix()
Dim ActiveSheet As Object
Dim c As Integer
Dim Address As CellRangeAddress

ActiveSheet = ThisComponent.getCurrentController.getActiveSheet
Matrix = ActiveSheet.getCellRangeByName("A3:A79").getDataArray
Address = ActiveSheet.getCellRangeByName("A3").getRangeAddress

For c = UBound(Matrix) To 0 Step -1
If Matrix (c)(0) > 1 Then
Address.StartRow = Address.StartRow + c
Address.EndRow = Address.EndRow + c
ActiveSheet.RemoveRange ( Address , 3)
MsgBox "delete row " & Address.StartRow
Address.StartRow = Address.StartRow - c
Address.EndRow = Address.EndRow - c
End If
Next

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
There may be fancier of better ways to do this, but here is what I would do.

VBA Code:
Sub DeleteRows()
Dim i As Integer
Dim CheckRow As String

i = 1
CheckRow = Range("A" & i)
Do Until CheckRow = ""
    If CheckRow = 2 Then
        Rows(i).Delete Shift:=xlUp
    Else
        i = i + 1
    End If
    CheckRow = Range("A" & i)
Loop
End Sub
 
Upvote 0
Welcome to the Board!

One other tip. Never use reserved words (words of existing objects, methods, properties, functions, etc) like "Address" as the names of your variables, functions, and procedures.
Doing so can cause errors and unexpected results.
 
Upvote 0
Not even sure this is an Excel/VBA question, as that code does not look like VBA.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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