Guest User

Untitled

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