vendredi 9 août 2019

If then statement to delete columns

New to VBA and trying to understand how to write an efficient code for data cleaning purposes.

I have a spreadsheet where my first step would be to remove entire columns with certain headers (about 25). I tried writing If Then statements for each header, but I find it only works on the first item, then I get error 424 (object undefined). I don't completely understand the VBA structure yet, so not sure how I can write this more efficiently

Sub DataCleaning()

    Set MR = Range("A1:ZA1")
    For Each Cell In MR
    If Cell.Value = "subject" Then Cell.EntireColumn.Delete
    If Cell.Value = "Study" Then Cell.EntireColumn.Delete
    If Cell.Value = "site" Then Cell.EntireColumn.Delete

End Sub

This code will eventually have a series of additional steps as well, like relabeling headers and will be applied to more than 1 tab, so each header won't necessarily be found all the time.

Any help in structuring this better would be appreciated!!

Aucun commentaire:

Enregistrer un commentaire