+ Reply to Thread
Results 1 to 6 of 6

Delete identical columns from multiple files

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2020
    Location
    Mankato, MN, USA
    MS-Off Ver
    2016
    Posts
    6

    Delete identical columns from multiple files

    I am working with a data set that has hundreds of columns, about 3/4 of which must be deleted. They are irregularly spaced, so I will need to go through manually one time in order to select them and delete them. I estimate it will take about 20 minutes to get through the entire file, which goes down to column RV.

    Here's the problem: I have hundreds of these files. ALL of the files have identical columns (containing different data below), and the same columns need to be deleted in each data set. In other words. If AC needs to be deleted in File 1, AC will also need to be deleted in File 2. Is there a way that I can record the columns that must be deleted and then simply copy/paste those columns into a spot that will remove them?

    Thank you so much for all your help over the years. You guys are super heroes, seriously.

  2. #2
    Forum Contributor
    Join Date
    03-18-2014
    Location
    Singapore
    MS-Off Ver
    Excel 2016 / 2019
    Posts
    251

    Re: Delete identical columns from multiple files

    Hi bklebig, Power Query can automate your deletion process

    So are you combining these identical files (with same headers) into one single table in the end ?

    upload sample datasets and explain clearly on your requirement will be good
    Christopher Yap

  3. #3
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Delete identical columns from multiple files

    Your could try this code...
    Sub Button1_Click()
    
        Dim wb1 As Workbook
        Dim wb2 As Workbook
        Dim sourcesheet As Worksheet
        Dim col As String
        Dim colno As Long
        Dim fullfilename As String
        Dim filename As String
        
        Set wb1 = ActiveWorkbook
        fullfilename = Range("B1").Value & Range("B2").Value
        filename = Range("B2").Value
        
        On Error GoTo notfound
        Workbooks.Open fullfilename
        Set wb2 = ActiveWorkbook
        Set sourcesheet = wb2.Worksheets("Sheet1")
        On Error GoTo 0
        
        For i = 1 To 9999
            col = wb1.Worksheets("Sheet1").Range("D" & CStr(i)).Value
            If col = "" Then
                Exit For
            End If
            colno = wb1.Worksheets("Sheet1").Range("E" & CStr(i)).Value - (i - 1)
            sourcesheet.Columns(colno).EntireColumn.Delete
        Next i
        Workbooks(filename).Close SaveChanges:=True
        wb1.Activate
    
        Exit Sub
        
    notfound: MsgBox ("File not found")
    End Sub
    PS: Make sure you have a backup of your original file!!!
    PPS: Assumes you enter your columns in ascending order in column D. Drag down formula in Column E to match column D.
    Attached Files Attached Files
    Last edited by Croweater; 08-31-2020 at 04:02 AM.

  4. #4
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Delete identical columns from multiple files

    I've changed this so that you don't have to put in the filename each time.
    If you create a test folder with all of your workbooks in, it will run through the lot and delete the columns for you, saving the changed workbook each time.
    Sub Button1_Click()
    
        Dim wb1 As Workbook
        Dim wb2 As Workbook
        Dim sourcesheet As Worksheet
        Dim col As String
        Dim colno As Long
        Dim fullfilename As String
        Dim filename As String
     
        Dim oFSO As Object
        Dim oFolder As Object
        Dim oFile As Object
        Dim i As Long
     
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set oFolder = oFSO.GetFolder(Range("B1").Value)
        Set wb1 = ActiveWorkbook
        
        For Each oFile In oFolder.Files
     
            Range("B2").Value = oFile.Name
            Application.ScreenUpdating = False
            
            fullfilename = Range("B1").Value & Range("B2").Value
            filename = Range("B2").Value
        
            Workbooks.Open fullfilename
            Set wb2 = ActiveWorkbook
            Set sourcesheet = wb2.Worksheets("Sheet1")
            
            For i = 1 To 9999
                col = wb1.Worksheets("Sheet1").Range("D" & CStr(i)).Value
                If col = "" Then
                    Exit For
                End If
            colno = wb1.Worksheets("Sheet1").Range("E" & CStr(i)).Value - (i - 1)
            sourcesheet.Columns(colno).EntireColumn.Delete
        Next i
        
        Workbooks(filename).Close SaveChanges:=True
        wb1.Activate
        Application.ScreenUpdating = True
        
        Next oFile
    
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    07-13-2020
    Location
    Mankato, MN, USA
    MS-Off Ver
    2016
    Posts
    6

    Re: Delete identical columns from multiple files

    Croweater, you're my hero. Worked perfectly. Thank you so much.

  6. #6
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Delete identical columns from multiple files

    Happy to have helped.

+ 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. How to get identical values from multiple columns
    By amazzuca in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-28-2020, 05:26 AM
  2. Replies: 4
    Last Post: 04-25-2019, 05:28 AM
  3. Replies: 1
    Last Post: 01-25-2016, 03:19 PM
  4. Filtering multiple columns simultaneoulsy which may have identical text
    By jlwalker63 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2015, 09:37 AM
  5. Replies: 2
    Last Post: 05-27-2010, 11:39 AM
  6. dynamically linking multiple identical structured files
    By perinouk in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-12-2007, 08:09 AM
  7. [SOLVED] How do I combine multiple whooksheets with identical columns
    By HF in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-04-2005, 09:06 AM

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