I've made more changes, I think I'm almost there. Now when I run it I get the error:
Run-time error '1004':
The PivotTable field name is not valid. To create a PivotTable report, you must use data that is organized as a list with labeled columns. If you are changing the name of a PivotTable field, you must type a new name for the field.
When I do it manually, it works just fine. There aren't any changes to the PivotTable field or anything like that so I'm stumped. Heres the new code:
Sub ImportNewSource()
Dim Filt As String
Dim FilterIndex As Integer
Dim Title As String
Dim FilePath As Variant
Dim ThisPivot As PivotTable
Dim SourceRng As Range
Dim FileName As String
Dim ShtNum As Integer
Dim LastRow As Long
ShtNum = ActiveWorkbook.Sheets.Count
If ShtNum <> 31 Then
Beep
MsgBox "Error: Too many sheets in workbook, please delete any sheets that you added to this file then try again.", _
vbCritical = vbOKOnly, "Warning!"
Exit Sub
End If
Set ThisPivot = ThisWorkbook.Worksheets(16).PivotTables(1)
Filt = "Excel Files (*.xls), *.xls," & _
"All Files (*.*),*.*"
FilterIndex = 1
Title = "Select the monthly MBR file you wish you import data from"
FilePath = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)
If FilePath = False Then
MsgBox "Cancelled"
Exit Sub
Else
FileName = FileNameOnly(FilePath)
End If
If WorkbookIsOpen(FileName) Then
Beep
MsgBox "MBR file is currently open! Please close the file and try again.", _
vbCritical = vbOKOnly, "File is already open!"
Exit Sub
End If
Workbooks.Open FilePath, UpdateLinks:=False
If SheetExists("Detail") = False Then
ActiveWorkbook.Close SaveChanges:=False
Beep
MsgBox "Invalid PivotTable data in worksheet.", _
vbCritical = vbOKOnly, "Invalid Data"
Exit Sub
Else
LastRow = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
End If
With ThisPivot
.SourceData = Workbooks(FileName).Worksheets("Detail").Range(Cells(4, 10), Cells(LastRow, 116)) _
.Address(ReferenceStyle:=xlR1C1)
.RefreshTable
End With
Set ThisPivot = Nothing
Set SourceRng = Nothing
End Sub
The part in bold is what triggers the error.
Bookmarks