+ Reply to Thread
Results 1 to 10 of 10

Macto to delete Columns in row 1 on all sheets containing YTD

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Macto to delete Columns in row 1 on all sheets containing YTD

    I have code below, which I need amended to only delete the columns on all sheets containing YTD in row 1

    the code is deleting all the columns except the columns containing YTD in row 1


     Sub YTD_Text()
      
       Dim ws As Worksheet, x
        For Each ws In Worksheets
            x = ws.Range("c1").Resize(, ws.Cells.SpecialCells(11).Column).Address(0, 0)
            x = Filter(ws.Evaluate("if(iserror(search(""ytd""," & x & ")),address(1,column(" & x & "),4))"), False, 0)
            If UBound(x) > -1 Then ws.Range(Join(x, ",")).EntireColumn.Delete
        Next
    
    End Sub
    it would be appreciated if someone could kindly assist me
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,653

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    replace "iserror" with "isnumber"

  3. #3
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    Thanks Jindon

    it works perfectly

  4. #4
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    Hi Jindon


    I run this on another workbook where I have several sheets and I get a run time error below


    "The item with the specified name was not found" and the code below is highlighted

        If UBound(x) > -1 Then ws.Range(Join(x, ",")).EntireColumn.Delete

    Kindly amend code

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,653

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    Can you post that workbook?

  6. #6
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    Hi Jindon


    Please find sample data where run time error occurring


    Kind test & correct
    Attached Files Attached Files

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,653

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    Why did you change False to True?
            x = Filter(ws.Evaluate("if(isnumber(search(""ytd""," & x & ")),address(1,column(" & x & "),4))"), True, 0)

  8. #8
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    Sorry I amended this as I wanted to test something and forgot to change it back to false
    Last edited by Howardc1001; 12-30-2020 at 06:32 AM.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,653

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    ws.Evaluate("if(isnumber(search(""ytd""," & x & ")),address(1,column(" & x & "),4))")
    will generate the array with the cells address like A1, B1, C1..(for the cell that contains "ytd") and False(for the cell that doesn't contain "ytd").
    So, need to filter out "False" via Filter Function and last 0 means actually "False" for excluding from the array.
    So that the result after the filter function is only Array of cells address.

  10. #10
    Forum Contributor
    Join Date
    07-12-2018
    Location
    South Africa
    MS-Off Ver
    Office 2021
    Posts
    2,776

    Re: Macto to delete Columns in row 1 on all sheets containing YTD

    Many thanks for the updated code and explanation

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 8
    Last Post: 12-29-2019, 12:43 AM
  2. [SOLVED] Need to delete all columns to the right of column I on all sheets in workbook
    By carlito2002wgn in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-11-2019, 02:11 AM
  3. [SOLVED] Add columns+headers and delete sheets
    By PaulM100 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2018, 05:48 AM
  4. How to compare columns in 2 sheets then delete if no match
    By needtoknowasu in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-26-2017, 12:13 AM
  5. [SOLVED] Compare two columns in two sheets and delete if existed
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2016, 06:30 AM
  6. Macro to Delete columns in different sheets within a workbook
    By makku in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-17-2012, 09:38 AM
  7. Delete Columns and hide sheets
    By dodom75 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-08-2008, 04:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1