Delete columns based on name, loop through list

imeade

New Member
Joined
Jun 28, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a "Data" sheet and a "Reference" sheet. On the data sheet I want to delete specific columns based on the header name. The following code I have below looks up the named column based on a value on the "Reference" sheet in column E. Right now the code is a specific reference, what I want to do is create a loop that will first look at cell E2, delete the column on "Data", then move on to E3, delete the column, then move on to E4 and so forth. How can I layer in a loop on a specific range in column E on the "Reference" sheet.


Sub columns()

Dim delColumn1 As Range
Dim delColumn2 As Range

Set delColumn1 = Sheets("Reference").Range("e2")
Set delColumn2 = Sheets("Reference").Range("e3")

Sheets("Data").Select

Set ColumnName = Rows(1).Find(delColumn1, , xlValues, xlWhole)
If Not ColumnName Is Nothing Then
ColumnName.EntireColumn.Delete
End If

Set ColumnName = Rows(1).Find(delColumn2, , xlValues, xlWhole)
If Not ColumnName Is Nothing Then
ColumnName.EntireColumn.Delete
End If


End Sub


Thanks!
 

Attachments

  • 1673542099979.png
    1673542099979.png
    47.6 KB · Views: 11

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi,
see if this update to your code will do what you want

Rich (BB code):
Sub DeleteColumns()
    
    Dim delColumn   As Range, cell As Range
    
    Set delColumn = Worksheets("Reference").Range("e2:e3")
    
    For Each cell In delColumn.Cells
        If Len(cell.Value) > 0 Then
            Set ColumnName = Worksheets("Data").Rows(1).Find(cell.Value, , xlValues, xlWhole)
            If Not ColumnName Is Nothing Then ColumnName.EntireColumn.Delete
        End If
        Set ColumnName = Nothing
    Next cell
    
End Sub

Change the list range address shown in red as required

Dave
 
Upvote 0
Hi,
see if this update to your code will do what you want

Rich (BB code):
Sub DeleteColumns()
   
    Dim delColumn   As Range, cell As Range
   
    Set delColumn = Worksheets("Reference").Range("e2:e3")
   
    For Each cell In delColumn.Cells
        If Len(cell.Value) > 0 Then
            Set ColumnName = Worksheets("Data").Rows(1).Find(cell.Value, , xlValues, xlWhole)
            If Not ColumnName Is Nothing Then ColumnName.EntireColumn.Delete
        End If
        Set ColumnName = Nothing
    Next cell
   
End Sub

Change the list range address shown in red as required

Dave
Perfect, thanks Dave!
 
Upvote 0
most welcome & appreciate feedback

Dave
Hi Dave - I have another similiar question/thread and was wondering if you could assist:


Thanks
Ian
 
Upvote 0
Hi Dave - I have another similiar question/thread and was wondering if you could assist:


Thanks
Ian

You should start a new thread - you can provide link back to this thread if think it will help forum

Dave
 
Upvote 0

Forum statistics

Threads
1,220,996
Messages
6,157,275
Members
451,410
Latest member
Connie1978

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