1

I have about 750 excel files. I need to clean them so that they all contain same formatting - i.e. so they all contain same number of columns.

Some files (80%) contain extra columns containing labels with an asterisk e.g. "*1 subject".

Is there a way using visual basic to go through all of the files in my folder to delete all columns containing an asterisk so that all files don't have any such columns? Will the fact that an asterisk is a wild card in computer speak make a difference?

2
  • As i mentioned on your previous question ... the best way to do this is to record a macro that does what you want on a single file, and then modify it to work on multiple files. This is the best way to learn vba. The links i posted previously are also valid for this question ... stackoverflow.com/a/31403366/5040941. Commented Jul 14, 2015 at 12:22
  • Hi thanks but the positioning of columns with asterisks changes per file (if they are even present) so what may work on a single file wouldnt necessarily work on another.... thanks for the links and your previous post though, i have looked through them... Commented Jul 14, 2015 at 12:42

1 Answer 1

3

Write a macro that uses filesystemobjects to loop through directory where the spreadsheets are. Loop through each sheet and analyse the column names.

Here is how you would loop through each sheet.

Private Sub CommandButton7_Click()

Dim ws      As Excel.Worksheet
Dim iCol    As Integer
Dim strName As String
Dim iIndex  As Integer

    'Loop through the sheets.
    For iIndex = 1 To Application.Worksheets.Count
        Set ws = Application.Worksheets(iIndex)

        'Loop through the columns.
        For iCol = 1 To ws.UsedRange.Columns.Count
            'Check row 1 of this column for first char of *
            If Left(ws.Cells(1, iCol).Value, 1) = "*" Then
                'We have found a column with the first char of *
                ws.Columns(iCol).EntireColumn.Delete
            End If
        Next iCol

    Next iIndex
    ActiveWorkbook.SaveAs Filename:="C:\temp\newfiles\" & ActiveWorkbook.Name, FileFormat:=xlWorkbookNormal
End Sub

If you want to look for an * anywhere in the cell you could use instr()

Private Sub CommandButton7_Click()

Dim ws      As Excel.Worksheet
Dim iCol    As Integer
Dim strName As String
Dim iIndex  As Integer

    'Loop through the sheets.
    For iIndex = 1 To Application.Worksheets.Count
        Set ws = Application.Worksheets(iIndex)

        'Loop through the columns.
        For iCol = 1 To ws.UsedRange.Columns.Count
            'Check row 1 of this column for the char of *
            If instr(ws.Cells(1, iCol).Value, "*") > 0 Then
                'We have found a column with the char of *
                ws.Columns(iCol).EntireColumn.Delete
            End If
        Next iCol

    Next iIndex
    ActiveWorkbook.SaveAs Filename:="C:\temp\newfiles\" & ActiveWorkbook.Name, FileFormat:=xlWorkbookNormal
End Sub

Here is a basic loop files in a given directory. Hope this gets you there.

Private Sub CommandButton7_Click()
    Dim wb      As Workbook
    Dim ws      As Excel.Worksheet
    Dim iCol    As Integer
    Dim strName As String
    Dim iIndex  As Integer
    Dim strPath As String
    Dim strFile As String

    strPath = "c:\temp\oldfiles\"
    strFile = Dir(strPath & "*.xlsx")

    Do While strFile <> ""

        Set wb = Workbooks.Open(Filename:=strPath & strFile)

        'Loop through the sheets.
        For iIndex = 1 To Application.Worksheets.Count
            Set ws = Application.Worksheets(iIndex)

            'Loop through the columns.
            For iCol = 1 To ws.UsedRange.Columns.Count
                'Check row 1 of this column for the char of *
                If InStr(ws.Cells(1, iCol).Value, "*") > 0 Then
                    'We have found a column with the char of *
                    ws.Columns(iCol).EntireColumn.Delete
                End If
            Next iCol

        Next iIndex
        wb.SaveAs Filename:="C:\temp\newfiles\" & wb.Name, FileFormat:=xlOpenXMLWorkbook
        wb.Close SaveChanges:=False
        strFile = Dir
    Loop

End Sub
Sign up to request clarification or add additional context in comments.

12 Comments

that's excellent - thank you. is there a way to also adapt it to look through subfolders? would this then save the file again and close once columns formatted?
Added a saveas to retain originals and create new files in a defined location with the columns deleted. Subfolders is possible but a bit of work. if reasonable do one folder at a time or move them into one folder.
Here is a good example of looping though files in a folder. Let me know if you need help adapting this to the above code. stackoverflow.com/questions/5851531/…
Hi MatthewD, I tried running the above code, and after removing Set statements and addings parantheses after SaveAs and Close, i stopped getting errors on the script but instead simply get an output msg of: Value cannot be null. Parameter name: solutionDirectory
At what line are you getting the Value cannot be null error?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.