vba help with file

Copper Contributor

Hello,

 

I am relatively new to creating VBA courses in some of my excel files (no prior training/courses) and I created a VBA code within Form Control to delete certain rows in a worksheet based on criteria.  That is working; however, I was hoping to add an additional step in the code so that after it deletes the required rows, it will then delete columns C - E.

 

Below is the specific code I used (the tab with the data and the Form Control button is called 'Extract'

 

Private Sub CommandButton1_Click()

lastrow = ThisWorkbook.Worksheets("Extract").Cells(Rows.Count, 1).End(xlUp).Row

For i = lastrow To 2 Step by - 1
If ThisWorkbook.Worksheets("Extract").Cells(i, 1).Value = "" Or Left(Cells(i, 1).Value, 6) = "REPORT" Or Left(Cells(i, 1).Value, 6) = "------" Or Left(Cells(i, 1).Value, 6) = "BUSINE" Or Left(Cells(i, 1).Value, 6) = "CURREN" Or Left(Cells(i, 1).Value, 6) = "GENERA" Or Left(Cells(i, 1).Value, 6) = "DESCRI" Then

Rows(i).Delete


End If

ThisWorkbook.Worksheets("Extract").Cells(1, 1).Select
Next

End Sub

 

 

Ideally, after it deletes the rows, i then want it to delete certain columns

 

Thank you

 

1 Reply

@TRJ290 

Sub CommandButton1_Click()
Dim lastrow As Long
Dim i As Long

lastrow = ThisWorkbook.Worksheets("Extract").Cells(Rows.Count, 1).End(xlUp).Row

For i = lastrow To 2 Step -1
If ThisWorkbook.Worksheets("Extract").Cells(i, 1).Value = "" Or _
Left(Cells(i, 1).Value, 6) = "REPORT" Or Left(Cells(i, 1).Value, 6) _
= "------" Or Left(Cells(i, 1).Value, 6) = "BUSINE" Or _
Left(Cells(i, 1).Value, 6) = "CURREN" Or Left(Cells(i, 1).Value, 6) _
= "GENERA" Or Left(Cells(i, 1).Value, 6) = "DESCRI" Then

Rows(i).Delete

End If

'ThisWorkbook.Worksheets("Extract").Cells(1, 1).Select
Next

lastrow = ThisWorkbook.Worksheets("Extract").Cells(Rows.Count, 1).End(xlUp).Row
Range(Cells(1, 3), Cells(lastrow, 5)).Delete

End Sub

You can try this code. In the attached file you can click the button in cell A1 to run the macro.