Sub DeleteColumnsByHeaderName() Dim HeaderNames() As Variant Dim HeaderName As Variant Dim LastColumn As Long Dim ColumnIndex As Long Dim Found As Boolean set xlApp = CreateObject("Excel.Application") set xlWbk = xlApp.Workbooks.Open(%f_Path%) set xlWksht = xlWbk.Worksheets("RPA") xlApp.Visible = true HeaderNames = Array("First Name", "Last Name", "Company Name") ' Find the last column in the active sheet LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column ' Loop through the header names For Each HeaderName In HeaderNames Found = False ' Search for the header name in the first row For ColumnIndex = 1 To LastColumn If Cells(1, ColumnIndex).Value = HeaderName Then Found = True ' Delete the entire column if the header name is found Columns(ColumnIndex).Delete Exit For End If Next ColumnIndex ' Display a message if the header name was not found If Not Found Then MsgBox "Header '" & HeaderName & "' not found.", vbExclamation End If Next HeaderName End Sub |