Vba to open a series of workbooks and update one tab

p4nny

Board Regular
Joined
Jan 13, 2015
Messages
246
Hi,

I hope someone can help..

I would like some vba code to open a series of workbooks (based off a list of workbook names) and up date one tab (called raw data), refresh pivots, refresh formulae and close. I would like this to run until the list of workbook names is blank.

Hope this makes sense. if you need more info or I'm not being clear please let me know

Thanks in advance

Anthony
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
See if this does what you want.
Code:
Sub update()
Dim wb As Workbook, sh As Worksheet, rng As Range, fPath As String
Set rng = Sheets(2).Range("A2", Sheets(2).Cells(Rows.Count, 1).End(xlUp))
fPath = ThisWorkbook.Path
If Right(fPath, 1) <> "/" Then fPath = fPath & "/"
For Each c In rng
On Error GoTo SKIP:
    Set wb = Workbooks.Open(fPath & c.Value) 'If your list does not include the file extion, this could error.
    Set sh = wb.Sheets("raw data")
    For Each Pivot In sh.PivotTables
        Pivot.RefreshTable
        Pivot.update
    Next
SKIP:
    If Err.Number > 0 Then
        MsgBox Err.Number & ":  " & Err.Description & vbLf & wb.Name & " was not found, or the worksheet evoked an error."
    End If
Next
End Sub

This code is untested.
 
Upvote 0
I can't write it for you but I will share something similar. Note I am writing this to be reusable for my other reports and thus have a worksheet called config. It uses the config worksheet to piece together my file names to open. As a result may be a little more complex than you need and TBH I still haven't finished it myself so it is definitely not copy and paste but may guide you in the right direction.

Code:
Sub Main()
'
' Macro to populate SCB_3
'
Application.DisplayAlerts = False

Dim configSheet As Worksheet
Dim linkedFiles As String
Dim dataFiles() As String
Dim expectedDateSuffix As String
Dim dataDirectory As String
Dim dataSheet As Worksheet
Dim formulas As String

'Get configuration
Set configSheet = Config
    configSheet.Activate
    expectedDateSuffix = Cells(5, "B").Value
    dataDirectory = Cells(6, "B").Value + Left(expectedDateSuffix, 7) + "\"
'Set dataSheet = Cells(7, "B").Value
    formulas = Cells(8, "B").Value
    firstDataRow = 2
    dataFiles = Split(Cells(4, "B").Value, ";") 'Create array of data files
    
    'create full path for each dataFile
    For a = 0 To UBound(dataFiles)
        dataFiles(a) = dataFiles(a) + expectedDateSuffix + ".xlsx"
    Next a
  
    
   
    For a = 0 To UBound(dataFiles)
        Workbooks.Open (dataDirectory + dataFiles(a))
        Windows(dataFiles(a)).Activate
        Worksheets("Agent Summary").Activate
        
        'Filter date to current Month
        Worksheets("Agent Summary").PivotTables("PivotTable5").PivotFields("Date Range").PivotFilters.Add _
        Type:=xlAfter, Value1:="2015-01-31"
        
        'Add in "AgentID Field"
        With Worksheets("Agent Summary").PivotTables("PivotTable5").PivotFields("AgentID")
            .Orientation = xlRowField
            .Position = 5
        End With
        
        'If it is the Moncton file remove Avail Time
        If dataFiles(a) = dataFiles(UBound(dataFiles)) Then
            Worksheets("Agent Summary").PivotTables("PivotTable5").PivotFields("AvailTime").Orientation = _
            xlHidden
        End If
         ActiveSheet.PivotTables("PivotTable5").PivotCache.Refresh
        
        'Find the last data row and column and copy the data
        lastRow = Worksheets("Agent Summary").Range("A" & Rows.Count).End(xlUp).Row
        MsgBox lastRow
        
        Range(Cells(3, 1), Cells(lastRow, 26)).Copy
        
        'Paste to lastrow+1 in auto_scb_3
        Windows("auto_scb_3.xlsb").Activate
        Data.Activate
        lastRow = Data.Range("A" & Rows.Count).End(xlUp).Row
        Range("A" + CStr(lastRow)).PasteSpecial (xlPasteValues)
    Next a
    
    'Paste Formulas:
    Range("AA2", "AU2").Copy
    Range("AA5", "AU5").PasteSpecial (xlPasteFormulas)
    End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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