VBA to delete column in all sheets except for the two specified ones

3139 views excel
6

I am trying to delete certain columns in all my excel sheets, except for two sheets. Those called Import and Export.

Here is my code.

Sub deletecolums()

Dim ws As Worksheet

If ws.Name <> "Import" And ws.Name <> "Export" Then


Range("A:C,D:E,G:I,L:L,N:P,Q:R,T:T").Delete


End If


Next ws


End Sub

answered question

What is your issue?

1 Answer

13

You're missing key bits in your code

Sub deletecolums()
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Sheets
        With ws
            If .Name <> "Import" And .Name <> "Export" Then
                Union(.Range("A:C"), .Range("D:E"), .Range("G:I"), .Range("L:L"), .Range("N:P"), .Range("Q:R"), .Range("T:T")).Delete
            End If
        End With
    Next ws

End Sub

posted this

Have an answer?

JD

Please login first before posting an answer.