+ Reply to Thread
Results 1 to 6 of 6

Delete Columns in Multiple Sheets with repeat macro

Hybrid View

  1. #1
    Registered User
    Join Date
    03-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Delete Columns in Multiple Sheets with repeat macro

    Hi all -

    I'm trying to delete multiple columns which are identical across multiple sheets based on an array which is built using user input.

    Through scouring these forums I have gotten this code:

    
    For Each ws In Worksheets
            
        If ws.Name <> "Start" Then
            MsgBox ("executed")
      
      For delColumn = LBound(SelectArray) To UBound(SelectArray)
        On Error Resume Next
        ws.Columns(Application.Match(SelectArray(delColumn), Rows(1), 0)).Delete
        On Error GoTo 0
      Next delColumn
      
        End If
      Next ws
    The code executes the number of times that there is sheets (as exhibited by the msgbox) but the columns are only deleted on the first sheet.

    What do I need to change?

    Thanks in advance for your help.
    Last edited by candie025; 05-16-2014 at 08:59 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Delete Columns in Multiple Sheets with repeat macro

    Hi,

    Welcome to the forum

    Try something like this...
    Sub ClearData()
    Dim ws As Worksheet
    Dim rRng As Range
    Dim addr As String
        Set rRng = Application.InputBox("Select the range to delete across the sheets.", Type:=8)
        addr = rRng.Address
        For Each ws In Worksheets
            If ws.Name <> "Start" Then
                ws.Range(addr).Clear
            End If
        Next ws
    End Sub
    Regards,
    Rudi

  3. #3
    Registered User
    Join Date
    03-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Delete Columns in Multiple Sheets with repeat macro

    Thanks for the input Rudi but that isn't going to quite work.

    The user input has already been selected. The "SelectArray" is built with a listbox which allows the users to select from a list comprising of all the column headers in the sheet (all are in row 1).

    If a header is selected (say "date" which resides in C1) it is moved from FullArray to SelectArray. Then on each sheet in the workbook I want to compare the headers on the sheet (all of row 1) to the items in SelectArray. If they match then that column (column C) is fully deleted from the sheet and so on and so forth.

    The code I presented executes the deletion perfectly but allows this only on the first sheet where the sheet name is not "start". For some reason subsequent executions of the code to check the first row against the array then delete the matching columns is what is not properly executing.

    Thanks again.

  4. #4
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Delete Columns in Multiple Sheets with repeat macro

    I could be wrong, but try prefixing the SelectArray with Worksheets("Start").
    i'm thinking that you need a reference to the sheet...
    For example:

    For Each ws In Worksheets
    
    If ws.Name <> "Start" Then
    MsgBox ("executed")
    
    For delColumn = LBound(Worksheets("Start").SelectArray) To UBound(Worksheets("Start").SelectArray)
    On Error Resume Next
    ws.Columns(Application.Match(Worksheets("Start").SelectArray(delColumn), Rows(1), 0)).Delete
    On Error GoTo 0
    Next delColumn
    
    End If
    Next ws
    OR....

    For Each ws In Worksheets
    
    If ws.Name <> "Start" Then
    MsgBox ("executed")
    
    For delColumn = LBound(ws.SelectArray) To UBound(ws.SelectArray)
    On Error Resume Next
    ws.Columns(Application.Match(ws.SelectArray(delColumn), Rows(1), 0)).Delete
    On Error GoTo 0
    Next delColumn
    
    End If
    Next ws

  5. #5
    Registered User
    Join Date
    03-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Delete Columns in Multiple Sheets with repeat macro

    Actually with a little tinkering my husband help me solve it. I had to command the rows on the worksheet.

    Here is the fixed code:

    
      Dim delColumn As Variant
      Dim ws As Worksheet
      
      For Each ws In Worksheets
            
        If ws.Name <> "Start" Then
            
      For delColumn = LBound(SelectArray) To UBound(SelectArray)
        On Error Resume Next
        ws.Columns(Application.Match(SelectArray(delColumn), ws.Rows(1), 0)).Delete
        On Error GoTo 0
      Next delColumn
       
        End If
        
      Next ws
    Thanks again for your replies
    Last edited by candie025; 05-16-2014 at 09:00 PM.

  6. #6
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Delete Columns in Multiple Sheets with repeat macro

    Ah... I was on the right track, but you guys figured out the fix
    I see it was a need for the sheet reference... I just had it in the wrong place.

    Glad you solved it.

+ 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. Repeat Macro for multiple Sheets - Next without For
    By MelForsyth in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-14-2013, 03:57 AM
  2. 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
  3. Need macro help with transposing columns to rows and repeat a row value multiple times
    By InnovativeAJ in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2012, 11:53 AM
  4. Excel 2007 : Repeat Columns in multiple sheets
    By jbode in forum Excel General
    Replies: 1
    Last Post: 01-04-2011, 04:39 PM
  5. Macro to loop through all sheets and delete unwanted columns
    By LemonTwist in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-24-2009, 12:03 PM

Tags for this Thread

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