VBA, changing Columns for Rows

Farback

Board Regular
Joined
Mar 27, 2009
Messages
149
I use this code to look down coumn C and to delete any row that had "Value A" or "Value" in it:

Sub Delete_Surplus_Rows()
Dim FindString As String
Dim iRow As Long
Dim LastRow As Long
Dim FirstRow As Long
Dim CalcMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
FirstRow = 4
With ActiveSheet
.DisplayPageBreaks = False
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

For iRow = LastRow To FirstRow Step -1

If IsError(.Cells(iRow, "C").Value) Then
'Do nothing
'This avoids an error if there is a error in the cell
ElseIf .Cells(iRow, "C").Value = "Value A" Then
.Rows(iRow).Delete
ElseIf .Cells(iRow, "C").Value = "Value B" Then
.Rows(iRow).Delete
End If

Next iRow

End With
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub

In another spreadsheet, I now need to look along row 3 for two values, say "Value C" and "Value B", and to delete the columns that contain them. How do I amend my code to do this?
 
I thought you might.

Try:

Code:
Set delRange = range("C5",cells(rows.count,"C").end(xlup))
Alternatively, right after
Code:
delRows.EntireRow.Hidden = True
insert the line
Code:
range("A1:A4").EntireRow.Hidden = True
so they become hidden (and thus avoid being deleted)
There's probably a couple more ways, like not using 'cells' for the delete range and defining a range that doesn't include rows 1 - 4, or making delRange start off by including the other rows you want to keep
 
Last edited:
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi Weaver. Sorry for not responding sooner, got involved in other things. I used your second solution (hiding the top 4 rows) and it works fine. Once again many thanks for your assistance, it's much appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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