Guest User

Untitled

a guest
Jan 20th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. Dim Desktop As Variant
  2. Dim Files As Object
  3. Dim Folder As Variant
  4. Dim oShell As Object
  5. Dim Tmplts As Variant ' Templates folder
  6. Dim wsLocal As Worksheet
  7. Dim wsGroup As Worksheet
  8. Dim wb As Object
  9.  
  10. ' Check Box 2 "Select All" must be checked to run the macro.
  11. If ActiveSheet.Shapes("Check Box 2").ControlFormat.Value = xlOff Then Exit Sub
  12.  
  13. Application.ScreenUpdating = False
  14. Application.EnableEvents = False
  15.  
  16. ' Prompt user to locate the Templates folder.
  17. With Application.FileDialog(msoFileDialogFolderPicker)
  18. If .Show = True Then
  19. Tmplts = .SelectedItems(1)
  20. Else
  21. Exit Sub
  22. End If
  23. End With
  24.  
  25. Set oShell = CreateObject("Shell.Application")
  26.  
  27. Set Desktop = oShell.Namespace(0)
  28.  
  29. ' Create the Output folder on the User's Desktop if it does not exist.
  30. Set Folder = Desktop.ParseName("Output")
  31. If Folder Is Nothing Then
  32. Desktop.NewFolder "Output"
  33. Set Folder = Desktop.ParseName("Output")
  34. End If
  35.  
  36. Set Files = oShell.Namespace(Tmplts).Items
  37. Files.Filter 64, "*.xlsm"
  38.  
  39. For Each wb In Files
  40. Set wb = Workbooks.Open(Filename:=wb.Path, UpdateLinks:=False)
  41.  
  42. Call BreakLinks(wb)
  43.  
  44. On Error Resume Next
  45. Set wsLocal = wb.Worksheets("RVP Local GAAP")
  46. Set wsGroup = wb.Worksheets("RVP Group GAAP")
  47. On Error GoTo 0
  48.  
  49. ' Check that both worksheets exist before updating.
  50. If Not wsLocal Is Nothing And Not wsGroup Is Nothing Then
  51. Call ProcessNamedRanges(wb)
  52.  
  53. MsgBox "Ranges have been updated sucessfully."
  54.  
  55. ' Save the workbook to the folder and close.
  56. On Error Resume Next
  57. wb.SaveAs Filename:=Folder.Path & "" & wb.Name
  58. ActiveWorkbook.Close True
  59. On Error GoTo 0
  60. End If
  61. Next wb
  62.  
  63. Application.ScreenUpdating = True
  64. Application.EnableEvents = True
  65.  
  66. End Sub
  67.  
  68. Sub ProcessNamedRanges(ByRef wb As Workbook)
  69.  
  70. Dim dstRng As Range
  71. Dim rng As Range
  72. Dim rngName As Range
  73. Dim rngNames As Range
  74. Dim wks As Worksheet
  75.  
  76. Set wks = ThisWorkbook.Sheets("Output - Flat")
  77.  
  78. ' Exit if there are no named ranges listed.
  79. If wks.Range("D4") = "" Then Exit Sub
  80.  
  81. Set rngNames = wks.Range("D4").CurrentRegion
  82. Set rngNames = Intersect(rngNames.Offset(1, 0), rngNames.Columns(2))
  83.  
  84. 'Loop through all the values in NamedRange
  85. For Each rngName In rngNames
  86. ' Verify the Named Range exists.
  87. On Error Resume Next
  88. Set dstRng = wb.Names(rngName.Text).RefersToRange
  89. If Err = 0 Then
  90. ' Create a link from the Template worksheet to the Report Balance.
  91. dstRng.Value = rngName.Offset(0, 1).Value
  92. dstRng.Offset(0, -2).Formula = "=" & rngName.Offset(0, 1).Address(True, Ture, xlA1, True)
  93. Else
  94. 'answer = MsgBox("The Named Range """ & rngName.Value & """ Does Not Exist" & vbLf & vbLf & "Continue?", vbYesNo + vbExclamation)
  95. 'If answer = vbNo Then Exit Sub
  96. End If
  97. On Error GoTo 0
  98. Next rngName
  99.  
  100. End Sub
  101.  
  102. Sub BreakLinks(ByRef wb As Workbook)
  103.  
  104. Dim i As Long
  105. Dim wbLinks As Variant
  106.  
  107. wbLinks = wb.LinkSources(xlExcelLinks)
  108.  
  109. If Not IsEmpty(wbLinks) Then
  110. For i = 1 To UBound(wbLinks)
  111. ActiveWorkbook.BreakLink wbLinks(i), xlLinkTypeExcelLinks
  112. Next i
  113. End If
  114.  
  115. End Sub
Add Comment
Please, Sign In to add comment