Share via

Macro to delete specific columns and as well all columns to the right of a named column

Anonymous
2022-05-26T16:20:19+00:00

Each day I receive a database, from which I delete specific named columns (more than three) and as well all others to the right of a specific column.

Could a macro delete three named columns (or more, which I can duplicate code lines for) and as well a named column and ALL others to its right?

Many thanks for all assistance.

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

8 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2022-05-27T06:03:47+00:00

    You want to code sometimes like this:

    Sub Fails()

    Columns("B,D,F:G,I:XFD").Delete
    End Sub

    But unfortunately this syntax did not work. But this one does:

    Sub Works()

    Range("B1,D1,F1:G1,I1:XFD1").EntireColumn.Delete
    End Sub

    Any further questions?

    Andreas.

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2022-05-27T03:51:51+00:00

    Afraid I tried one on the list of two and it did not delete all the columns.

    Dim sh As Worksheet

    'as mentioned in comments by FunThomas, deleting should be done "backwards", i.e. for right to left 
    
    For Each sh In Worksheets 
    
        sh.Columns("COL ABC").Delete 
    
        sh.Columns("COL DEF").Delete 
    
        sh.Columns("COL GHI").Delete 
    
    Next 
    

    End Sub

    1 person found this answer helpful.
    0 comments No comments
  3. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2022-05-27T17:51:25+00:00

    I'm sorry, I don't understand. Please show me a sample file.

    Andreas.

    0 comments No comments
  4. Anonymous
    2022-05-27T14:38:42+00:00

    How would I use the same code with column names as opposed to column letters, which change as you delete one or more?

    0 comments No comments