+ Reply to Thread
Results 1 to 6 of 6

Loop Through Column Headings and Delete Columns

Hybrid View

mikey3580 Loop Through Column Headings... 11-07-2015, 12:10 AM
alansidman Re: Loop Through Column... 11-07-2015, 12:35 AM
mikey3580 Re: Loop Through Column... 11-07-2015, 12:46 AM
alansidman Re: Loop Through Column... 11-07-2015, 12:48 AM
mikey3580 Re: Loop Through Column... 11-07-2015, 12:50 AM
Alf Re: Loop Through Column... 11-07-2015, 02:33 AM
  1. #1
    Forum Contributor
    Join Date
    11-01-2012
    Location
    NJ
    MS-Off Ver
    Excel 365
    Posts
    120

    Loop Through Column Headings and Delete Columns

    Hello,

    I have a bunch of different files that are all uniquely named with each of the column headings in 30 Columns starting in Column C.

    I am trying to loop through row 1 where the column headings are and delete the columns that do not match the file name. Then move to the next file.

    Below is what I have but I am a novice and can't get it work.

    Any help would be appreciated!

    Thanks

    
     Sub Resave()
      Dim Outpath As String
      Dim InputPath As String
      Dim rngNames As Excel.Range
      Dim rng As Excel.Range
      Dim wkbTemplate As Excel.Workbook
      Dim FileName As String
      Dim c As Range
      Dim myOutput As String
      Dim mypath As String
     
      Outpath = ActiveWorkbook.Path & "\Output\"
     
      FileName = Dir(Outpath & "*.*")
     
                Do While FileName <> ""
                    Workbooks.Open FileName:=Outpath & FileName, ReadOnly:=False
                       
     'Retrieve ActiveWorkbook's File Path
    mypath = ActiveWorkbook.FullName
     
    'Take Off The File Extension
      myOutput = Left(mypath, InStrRev(mypath, ".") - 1)
     
    'Retrieve File Extension
      myOutput = Right(mypath, Len(mypath) - InStrRev(mypath, "."))
     
    'Retrieve File Name with Extension
      myOutput = Right(mypath, Len(mypath) - InStrRev(mypath, "\"))
     
    'Retrieve File Name without Extension
      myOutput = Mid(mypath, InStrRev(mypath, "\") + 1, InStrRev(mypath, ".") - InStrRev(mypath, "\") - 1)
     
            
            'NEED HELP HERE
            Last = Cells(1, Columns.Count).End(xlToLeft).Column
            For i = Last to 3 Step -1
              If Cells(1, i).Value <> myOutput Then
                    Cells(1, i).Value.EntireColumn.Delete
            End If
            Next i
                        Workbooks(FileName).Save
                        ActiveWorkbook.Close
                        FileName = Dir()
            Loop
           
            End Sub

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Loop Through Column Headings and Delete Columns

    What is not working here? Are you getting an error message. Perhaps load a sample file that represents an actual file.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Contributor
    Join Date
    11-01-2012
    Location
    NJ
    MS-Off Ver
    Excel 365
    Posts
    120

    Re: Loop Through Column Headings and Delete Columns

    Hi,
    Getting runtime error 424 - Object Required
    On line

    Cells(1, i).Value.EntireColumn.Delete

    Appreciate the help!

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Loop Through Column Headings and Delete Columns

    Try taking out .value from the line of code so it reads
    cells(1,i).EntireColumn.Delete

  5. #5
    Forum Contributor
    Join Date
    11-01-2012
    Location
    NJ
    MS-Off Ver
    Excel 365
    Posts
    120

    Re: Loop Through Column Headings and Delete Columns

    That's it! Thanks for that!

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Loop Through Column Headings and Delete Columns

    Some small additions / changes you can do.

    You have "Dim'ed" all your variables except i so you could add
    Dim i As Integer
    and you could change

     Workbooks(FileName).Save
          ActiveWorkbook.Close
    to

    Workbooks(FileName).Close SaveChanges:=True
    Alf

+ 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. [SOLVED] Formula to Sum Multiple Columns Based on Column Headings and Other Criteria
    By webfeet2 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-10-2015, 12:20 PM
  2. [SOLVED] Delete Multiple Columns with specific column headings using a Macro.
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-30-2013, 07:28 PM
  3. [SOLVED] Sum columns based on column headings
    By Jario in forum Excel General
    Replies: 6
    Last Post: 06-29-2012, 10:37 AM
  4. Sum several columns based on column headings.
    By Jario in forum Excel General
    Replies: 2
    Last Post: 06-27-2012, 11:51 AM
  5. Transposing one column data to different columns based on their headings
    By bipindurgapal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2012, 06:35 AM
  6. Compare Column Headings and Delete Nonmatch
    By bthurman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-02-2009, 01:40 PM
  7. delete specific columns by reading headings
    By sa02000 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2008, 06:52 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