web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to delete the mult...
Power Automate
Answered

How to delete the multiple columns by using VB Script in Power automate desktop

(0) ShareShare
ReportReport
Posted on by 150

I have tried with one VB script it is working on the VB editor, when I use it in the workflow it's not working.

 

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("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
 
 
Screenshot (46).png
I have the same question (0)
  • UshaJyothiKasibhotla Profile Picture
    225 Moderator on at

    Set objExcel = CreateObject("Excel.Application")
    Set wb = objExcel.Workbooks.Open("File Path")
    objExcel.Visible = True
    wb.Worksheets("Sheet1").Range("A:A").EntireColumn.Delete
    wb.Worksheets("Sheet1").Range("D:D").EntireColumn.Delete
    wb.Save
    wb.Close

     

    Try this

    Hope this helps

    Usha

  • kiran_g Profile Picture
    150 on at

    But I want to delete it by using the Headers

  • UshaJyothiKasibhotla Profile Picture
    225 Moderator on at

    Hi @kiran_g 

    Try this

    ' Assuming headersToDelete is an array containing headers to delete
    Dim headersToDelete
    headersToDelete = Array("Header1", "Header2", "Header3")

    ' Assuming sheet is the name of your Excel sheet
    Set excelApp = GetObject("Excel.Application")
    Set excelWorkbook = excelApp.Workbooks.Open("Path to Your Excel File")
    Set excelSheet = excelWorkbook.Sheets("Sheet1")

    ' Loop through headersToDelete and delete corresponding columns
    For Each header In headersToDelete
    On Error Resume Next
    columnIndex = Application.Match(header, excelSheet.Rows(1), 0)
    On Error GoTo 0

    If Not IsError(columnIndex) Then
    excelSheet.Columns(columnIndex).Delete
    End If
    Next

    ' Save and Close Excel
    excelWorkbook.Save
    excelWorkbook.Close
    excelApp.Quit

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    You're not referencing the worksheet object next to your Cells and Columns objects. Try this:

    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("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 = xlWksht.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 xlWksht.Cells(1, ColumnIndex).Value = HeaderName Then
     Found = True
     ' Delete the entire column if the header name is found
     xlWksht.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

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • kiran_g Profile Picture
    150 on at

    not working 3.PNG

  • kiran_g Profile Picture
    150 on at

     not working & i have also given 

    xlApp.Visible = true

    but it's not opening the file 

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Do you actually provide the path to the file instead of "path" in the script there?

     

  • kiran_g Profile Picture
    150 on at

    3.PNGYes, i have tried with full path and path variable

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Can you share the final script in there? Also, is there any error in %ScriptError% when you run it?

  • kiran_g Profile Picture
    150 on at

    4.PNG5.PNG

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 253 Super User 2026 Season 2

#2
11manish Profile Picture

11manish 172 Super User 2026 Season 2

#3
trice602 Profile Picture

trice602 134 Super User 2026 Season 2

Last 30 days Overall leaderboard