macro to open every file in a folder and change Data in specific cells?

steve400243

Active Member
Joined
Sep 15, 2016
Messages
429
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello I have several hundred files in a folder on My Desktop with a general name 'New Folder'. I am looking for a way to open each file and input the following into the noted cells in each file. Thanks for looking and any help provided. I also provided an example of one of the files with the correct formulas in the mentioned cells.

in cell H5 for each file -
VBA Code:
=IF(H4="","-",H4+6)

in cell H6 for each file -
Code:
=IF(H4="","-",H4+11)

in cell H7 for each file -
Code:
=IF(H4="","-",H4+15)

YM UPWARD 2080.xlsx
ABCDEFGH
1Packet Received:
2ETA Date:01/17/22Container Released:
3Master B/L:YMLUW236642080Received Container:
4VesselYM UPWARDDevanned On:
5Container#:YMLU9536528Storage - 7th Day:-
6Intensive Exam Date:12th day:-
7VACIS Exam Date:GO Date: -
8Customs Exam Date:
9
CFS SHEET
Cell Formulas
RangeFormula
H5H5=IF(H4="","-",H4+6)
H6H6=IF(H4="","-",H4+11)
H7H7=IF(H4="","-",H4+15)
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
try this
VBA Code:
Sub OPEN_ALL_FILES_IN_FOLDER_RUN_MACRO()

Dim Path As String
Path = "C:\Users\xxxxx\Desktop\New Folder\"
Dim fileName As String

' ANY EXCEL FILE
fileName = Dir(Path & "*.xls*")

Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Do While fileName <> ""
Workbooks.Open Path & fileName

' *****************************************************add code to run on each workbook in the folder********************************************
    Range("H5").FormulaR1C1 = "=IF(R[-1]C="""",""-"",R[-1]C+6)"
    Range("H6").FormulaR1C1 = "=IF(R[-2]C="""",""-"",R[-2]C+11)"
    Range("H7").FormulaR1C1 = "=IF(R[-3]C="""",""-"",R[-3]C+15)"
' ****************************************************end code to run on each workbook in the folder*********************************************
Workbooks(fileName).Close True
fileName = Dir()
Loop
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Solution
try this
VBA Code:
Sub OPEN_ALL_FILES_IN_FOLDER_RUN_MACRO()

Dim Path As String
Path = "C:\Users\xxxxx\Desktop\New Folder\"
Dim fileName As String

' ANY EXCEL FILE
fileName = Dir(Path & "*.xls*")

Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Do While fileName <> ""
Workbooks.Open Path & fileName

' *****************************************************add code to run on each workbook in the folder********************************************
    Range("H5").FormulaR1C1 = "=IF(R[-1]C="""",""-"",R[-1]C+6)"
    Range("H6").FormulaR1C1 = "=IF(R[-2]C="""",""-"",R[-2]C+11)"
    Range("H7").FormulaR1C1 = "=IF(R[-3]C="""",""-"",R[-3]C+15)"
' ****************************************************end code to run on each workbook in the folder*********************************************
Workbooks(fileName).Close True
fileName = Dir()
Loop
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Thanks for your time in looking at this. I did not ask where I would put this code to run? Would I create just a blank excel file and place in the folder?
 
Upvote 0
you can have excel file with macro anywhere you want just make sure you have correct path to your folder on below line
Path = "C:\Users\xxxxx\Desktop\New Folder\"
 
Upvote 0
you can have excel file with macro anywhere you want just make sure you have correct path to your folder on below line
Path = "C:\Users\xxxxx\Desktop\New Folder\"
Got it thanks, Just needed to slow down a bit. Works as needed after changing the file path. Many Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,520
Members
448,968
Latest member
Ajax40

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