Deleting rows

J

Jan Kronsell

I use this code to delete alle rows, that contains an "x" in the first
column:

nr = Sheets(1).Range("a65536").End(xlUp).Row


For i = nr To 2 Step -1
If UCase(Range("a" & i)) = "X" Then
Range("a" & i).EntireRow.Delete shift:=xlUp
End If
Next i

But how do I change it to delete all columns, that contains an "x" in the
first row? Thats is, what should I put in the Ucase and Range to make it
work?

nc = Sheets(1).Range("a65536").End(xlToLeft).Column

For h = nc To 2 Step -1
If UCase(???????) = "X" Then
Range(????).EntireColumn.Delete shift:=xlRight
End If
Next h

Jan
 
D

Don Guillett

Sub deletecolumnswithX()
For i = cells(1, Columns.Count) _
.End(xlToLeft).Column To 1 Step -1
If UCase(cells(1, i)) = "X" Then Columns(i).Delete
Next i
End Sub

Sub deleteRowswithX()
For i = cells(Rows.Count, 1) _
.End(xlUp).Row To 1 Step -1
If UCase(cells(i, 1)) = "X" Then Rows(i).Delete
Next i
End Sub
 
J

Jan Kronsell

I figured it out myself, finally :)

For h = ncn To 2 Step -1
If UCase(Cells(1, h).Value) = "X" Then
Cells(1, h).EntireColumn.Delete shift:=xlRight
End If
Next h

Does the trick.

Jan
 
J

Jan Kronsell

Thank you.

Jan

Don said:
Sub deletecolumnswithX()
For i = cells(1, Columns.Count) _
.End(xlToLeft).Column To 1 Step -1
If UCase(cells(1, i)) = "X" Then Columns(i).Delete
Next i
End Sub

Sub deleteRowswithX()
For i = cells(Rows.Count, 1) _
.End(xlUp).Row To 1 Step -1
If UCase(cells(i, 1)) = "X" Then Rows(i).Delete
Next i
End Sub
 
Y

ytayta555

            Range("a" & i).EntireRow.Delete shift:=xlUp

An very small sugestion ; if you have problems
with speed of deleting this rows , try method
ClearContents instead of Delete :
Range("a" & i).EntireRow.ClearContents shift:=xlUp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top