VBA Script to Update Files in a folder, updating Links

jaihawk8

Board Regular
Joined
Mar 23, 2018
Messages
58
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to simply life for our commissions folks. Currently, they have to open each Sales Person's Earnings Statement, click on Data-> Edit Links, choose the file to update, then when it does, change the date to the current pay period, save the file, and then close it.

I would like to write a VBA script that will use the information in the table below:

Earnings Statement File Path is located in cell B19

When it needs to update the links, I would want it to use the Path in B20 and the File in B21 (wasn't sure if I needed to concatenate that all together or not).

Once that's updated, I would like it to change cell C3 in the earnings statement to the value that's in cell B3 of this worksheet.

Right now, I'm just trying to get it to at least open the file and update it (not changing cell C3, but I'm failing on the following step:

1711742017112.png


Here's the layout of the Main File:

Earnings Statement Generator - Test CD.xlsm
ABC
1Payroll Date:04.12.24Date in text format "xx.xx.xx"
2
3Payroll Date:4/12/2024Date in short date format "xx/xx/xxxx"
4
5Payroll Period:April
6
7Who is Running the Report:Jai
8
9For What District:CD
10
11Rep or Analyst:Analysts
12
13Main File PathC:\Users\jbohl\OneDrive - Quadient\Central District\2024 Payroll Files\
14Month DirectoryApr 24 Earnings Statements\
15District FolderCD\
16Pay Period Folder04.12.24
17Log FileFY24 CD Commission Log - Analyst.xlsm
18
19
20
21File PathC:\Users\jbohl\OneDrive - Quadient\Central District\2024 Payroll Files\Apr 24 Earnings Statements\CD\04.12.24\Analysts\
22Update Links PathC:\Users\jbohl\OneDrive - Quadient\Central District\2024 Payroll Files\Earnings Statements\
23Update Links FileFY24 CD Commission Log - Analyst.xlsm
24
Main
Cell Formulas
RangeFormula
B13,B15B13=VLOOKUP(B7,$E:$F,2,FALSE)
B14B14=VLOOKUP(B5,$E:$F,2,FALSE)
B16B16=+B1
B17B17=VLOOKUP(F34,$E:$F,2,FALSE)
B21B21=CONCATENATE(B13,B14,B15,B16,"\",B11,"\")
B22B22=CONCATENATE(B13,"Earnings Statements\")
B23B23=+B17
Cells with Data Validation
CellAllowCriteria
B7List=$E$2:$E$4
B9List=$E$21:$E$22
B5List=$E$7:$E$18
B11List=$E$25:$E$26


Here's the scripting I've used so far:

VBA Code:
Sub UpdateAndSaveFiles()    Dim folderPath As String    Dim fileName As String    Dim wb As Workbook    Dim updateLinksFileName As String        ' Get the folder path from cell B19    folderPath = ThisWorkbook.Sheets("Sheet1").Range("B19").Value        ' Check if the folder path ends with a backslash, if not, add one    If Right(folderPath, 1) <> "\" Then        folderPath = folderPath & "\"    End If        ' Get the update links file name from cell B20    updateLinksFileName = ThisWorkbook.Sheets("Sheet1").Range("B20").Value        ' Loop through each file in the directory    fileName = Dir(folderPath & "*.xlsm")    Do While fileName <> ""        ' Open the workbook        Set wb = Workbooks.Open(folderPath & fileName)                ' Update the sheet        wb.RefreshAll                ' Update links        wb.UpdateLinks Name:=folderPath & updateLinksFileName, Type:=xlExcelLinks                ' Save and close the workbook        wb.Close SaveChanges:=True                ' Get the next file name        fileName = Dir    LoopEnd Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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