Code To Open Files Within Folder And Insert A Column With Header And Close

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I need a code please that will open some files within a folder and insert a column in AD with a header of Eurostd and close and save.

Is this possible please? Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Is it 'some' or all files in the folder?

Also, which sheet(s) should the column be inserted in?
 
Upvote 0
It is all and they are all sheet 1. There are some before close codes in them also so they are .xlsm files.
 
Upvote 0
Perhaps something like this then.
Code:
Dim wb As Workbook
Dim FSO As Object
Dim fld As Object
Dim fl As Object
Dim strPath As String

    strPath = "C:\test\" ' change path as required

    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set fld = FSO.GetFolder(strPath)
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    For Each fl In fld.Files
        If Right(fl, 4) = ".xlsm" Then
            Set wb = Workbooks.Open(fl.Path)
            wb.Sheets(1).Range("AD:AD").EntireColumn.Insert xlShiftToRight
            wb.Sheets(1).Range("A1").Value = "Eurostd"
            wb.Close SaveChanges:=True
        End If
    Next fl

    Application.EnableEvents = True
    Application.ScreenUpdating = False
 
Last edited:
Upvote 0
Thanks where do I put this code? There is no start or end sub?
 
Last edited:
Upvote 0
You can add your own, for example.
Code:
Sub InsertEurostdCol()

Dim wb As Workbook
Dim FSO As Object
Dim fld As Object
Dim fl As Object
Dim strPath As String

    strPath = "C:\test\" ' change path as required

    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set fld = FSO.GetFolder(strPath)
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    For Each fl In fld.Files
        If Right(fl, 4) = ".xlsm" Then
            Set wb = Workbooks.Open(fl.Path)
            wb.Sheets(1).Range("AD:AD").EntireColumn.Insert xlShiftToRight
            wb.Sheets(1).Range("A1").Value = "Eurostd"
            wb.Close SaveChanges:=True
        End If
    Next fl

    Application.EnableEvents = True
    Application.ScreenUpdating = False

End Sub
 
Upvote 0
I get an error 76 path not found and points to this line?

Set fld = FSO.GetFolder(strPath)
 
Upvote 0
Actually my fault typo in path, but it did run and did nothing?

I have just opened a blank sheet and inserted code in my PMW. But didn't do a thing when run.
 
Last edited:
Upvote 0
Think there was a typo in what I copied into my post, try this.
Code:
Sub InsertEurostdCol()

Dim wb As Workbook
Dim FSO As Object
Dim fld As Object
Dim fl As Object
Dim strPath As String

    strPath = "C:\test\" ' change path as required

    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set fld = FSO.GetFolder(strPath)
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    For Each fl In fld.Files

        If Right(fl.Name, 4) = ".xlsm" Then

            Set wb = Workbooks.Open(fl.Path)
            wb.Sheets(1).Range("AD:AD").EntireColumn.Insert xlShiftToRight
            wb.Sheets(1).Range("A1").Value = "Eurostd"
            wb.Close SaveChanges:=True
        End If
    Next fl

    Application.EnableEvents = True
    Application.ScreenUpdating = False

End Sub
 
Upvote 0
Sorry still does nothing. No errors or anything, just nothing?
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top