Public iEvents As Integer
Sub EventsOff(Optional Force As Boolean)
If iEvents < 1 Or Force = True Then
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
End If
iEvents = iEvents + 1
End Sub
Sub EventsOn(Optional Force As Boolean)
If iEvents < 2 Or Force = True Then
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End If
If Force = True Then
iEvents = 0
Else
iEvents = iEvents - 1
End If
End Sub
Sub GetMasterDB()
Dim A As String
Dim MelWB As Workbook
Dim ListSht As Worksheet
Dim ListRng As Range
Dim MasterSht As Worksheet
Dim MasterRng As Range
Dim Cel As Range
Dim LastSht As Worksheet
Dim i As Range
Dim Key1 As Range
Dim Key2 As Range
Dim Key3 As Range
Dim R As Range
Dim ThisWB As Workbook
Dim WBName As String
Set ThisWB = ThisWorkbook
Set LastSht = ActiveSheet
Call EventsOff
Application.StatusBar = "Opening Master Database. This may take up to 10 seconds"
'I have a SETUP sheet where I store the names of support workbooks
A = ThisWB.Sheets("Setup").Range("MasterDB").Value
WBName = GetFileName(A)
'If you already have the workbook open
If IsWBOpen(WBName) = True Then
Workbooks(WBName).Activate
Set MelWB = ActiveWorkbook
'Not open by you
Else
Application.DisplayAlerts = False
On Error Resume Next
'Opening a workbook in read only mode is faster
Set MelWB = Workbooks.Open(A, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
Application.DisplayAlerts = True
If MelWB Is Nothing Then
Call EventsOn
Application.StatusBar = False
MsgBox "The Ferndale Master Equipment List workbook cannot be found. Contact the Portal Administrator."
Exit Sub
End If
End If
'There are several tables that I am updating from this workbook
'P&ID P&ID P&ID P&ID P&ID P&ID P&ID P&ID P&ID P&ID P&ID P&ID P&ID
Application.StatusBar = "Retrieving Master P&ID List. This may take up to 5 seconds"
Set ListSht = ThisWorkbook.Sheets("Master P&ID List")
Set Cel = ListSht.Range("PIDEquip_hdr").Offset(1, 0) 'This is a named range on the sheet
Set ListRng = Range(Cel, Cel.Offset(1000000, 3))
ListRng.ClearContents
Set MasterSht = MelWB.Sheets("Master P&ID List")
Set Cel = MasterSht.Range("PIDEquip_hdr").Offset(1, 0) 'This is also a named range
Set MasterRng = Range(Cel, Cel.Offset(1000000, 0).End(xlUp).Offset(0, 4))
MasterRng.Copy
ListSht.Range("PIDEquip_hdr").Offset(1, 0).PasteSpecial xlPasteValues
'This is the next table to update
'Equipment Type List @@@@@@ Equipment Type List @@@@@@ Equipment Type List @@@@@@ Equipment Type List @@@@@@
Set ListSht = ThisWorkbook.Sheets("Setup")
Set Cel = ListSht.Range("TypeList_hdr").Offset(1, 0)
Set ListRng = Range(Cel, Cel.Offset(1000, 0))
ListRng.ClearContents
Cel.Value = "All"
Set MasterSht = MelWB.Sheets("Other List")
Set Cel = MasterSht.Range("TypeList_hdr").Offset(1, 0)
Set MasterRng = Range(Cel, Cel.Offset(100000, 0).End(xlUp))
MasterRng.Copy
ListSht.Range("TypeList_hdr").Offset(2, 0).PasteSpecial xlPasteValues
'And the next
'Master Equipment List..........Master Equipment List..........Master Equipment List..........
Application.StatusBar = "Retrieving Master Equipment List. This may take up to 5 seconds"
Set ListSht = ThisWB.Sheets("Master Equipment List")
Set Cel = ListSht.Range("FuncLoc_hdr").Offset(1, 0)
Set ListRng = Range(Cel, Cel.Offset(1000000, 20))
ListRng.ClearContents
Set MasterSht = MelWB.Sheets("Master Equip List")
Set Cel = MasterSht.Range("FunctionalLoc_hdr").Offset(1, 0)
Set MasterRng = Range(Cel, Cel.Offset(1000000, 0).End(xlUp).Offset(1000, 20))
'Copy the table from the master to this wb
MasterRng.Copy
ListSht.Range("FuncLoc_hdr").Offset(1, 0).PasteSpecial xlPasteValues
Application.DisplayAlerts = False
Application.CutCopyMode = False
'---------------------
MelWB.Close savechanges:=False
'---------------------
'You can delete this part of the code if you don't need to sort
'--------------------------------------------------------------------------------------------------
Application.StatusBar = "Sorting Master Equipment List and checking file status..."
'Sort the master equipment list
Set Cel = ListSht.Range("FuncLoc_hdr")
Set i = Intersect(Cel.End(xlDown).EntireRow, Cel.End(xlToRight).EntireColumn)
Set R = Range(Cel, i)
Set Cel = ListSht.Range("Block_hdr")
Set Key1 = Range(Cel, Cel.End(xlDown))
Set Cel = ListSht.Range("Equip_hdr")
Set Key2 = Range(Cel, Cel.End(xlDown))
Set Cel = ListSht.Range("WorkScope_hdr")
Set Key3 = Range(Cel, Cel.End(xlDown))
ListSht.Sort.SortFields.Clear
ListSht.Sort.SortFields.Add Key:=Key1, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ListSht.Sort.SortFields.Add Key:=Key2, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ListSht.Sort.SortFields.Add Key:=Key3, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ListSht.Sort
.SetRange R
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'---------------------------------------------------------------------------------
Application.DisplayAlerts = True
LastSht.Activate
Call EventsOn
Application.StatusBar = False
End Sub