msfz751

MUtilities

May 19th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' >>>>>>>>>>>>  MUtilities      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  2. ' Description:  This module contains utility procedures designed
  3. '               to assist the programmer during development.
  4. '
  5. ' Authors:      Rob Bovey, www.appspro.com
  6. '               Stephen Bullen, www.oaltd.co.uk
  7. '
  8. ' Chapter Change Overview
  9. ' Ch#   Comment
  10. ' --------------------------------------------------------------
  11. ' 05    Initial version
  12. '
  13. Option Explicit
  14. Option Private Module
  15.  
  16. ' **************************************************************
  17. ' Module Constant Declarations Follow
  18. ' **************************************************************
  19. ' We duplicate these constants here because we want this utility
  20. ' module to be completely self-contained.
  21. Private Const msFILE_TIME_ENTRY As String = "PetrasTemplate.xls"
  22. Private Const msRNG_NAME_LIST As String = "tblRangeNames"
  23. Private Const msRNG_SHEET_LIST As String = "tblSheetNames"
  24.  
  25.  
  26. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  27. ' Comments: This procedure transfers the settings specified in
  28. '           the wksUISettings table into the appropriate
  29. '           worksheets as defined names.
  30. '
  31. '           This procedure cannot be run from outside the
  32. '           add-in. Run it from this module by placing your
  33. '           cursor somewhere inside the body of the procedure
  34. '           and pressing F5. The PetrasTemplate.xlt workbook
  35. '           must be open before this procedure can be run.
  36. '
  37. ' Date          Developer       Chap    Action
  38. ' --------------------------------------------------------------
  39. ' 06/01/08      Rob Bovey       Ch05    Initial version
  40. '
  41. Public Sub WriteSettings()
  42.  
  43.     Dim rngSheet As Range
  44.     Dim rngSheetList As Range
  45.     Dim rngName As Range
  46.     Dim rngNameList As Range
  47.     Dim rngSetting As Range
  48.     Dim sSheetTab As String
  49.     Dim wkbBook As Workbook
  50.     Dim wksSheet As Worksheet
  51.    
  52.     ' Turning off screen updating and calculation
  53.    ' will speed the process significantly.
  54.    Application.ScreenUpdating = False
  55.     Application.Calculation = xlCalculationManual
  56.    
  57.     ' The time entry workbook.
  58.    Set wkbBook = Application.Workbooks(msFILE_TIME_ENTRY)
  59.     ' The list of worksheets in the first column.
  60.    Set rngSheetList = wksUISettings.Range(msRNG_SHEET_LIST)
  61.     ' The list of setting names in the first row.
  62.    Set rngNameList = wksUISettings.Range(msRNG_NAME_LIST)
  63.    
  64.     ' The outer loop processes all the worksheets in the
  65.    ' first column of the table.
  66.    For Each rngSheet In rngSheetList
  67.    
  68.         ' We need an object reference to the worksheet so we
  69.        ' can easily add a sheet-level defined name to it.
  70.        ' The sSheetTabName() function converts a CodeName
  71.        ' into its corresponding sheet tab name.
  72.        sSheetTab = sSheetTabName(wkbBook, rngSheet.Value)
  73.         Set wksSheet = wkbBook.Worksheets(sSheetTab)
  74.        
  75.         ' The inner loop adds each setting to the current sheet.
  76.        ' If the setting already exists it will be replaced.
  77.        For Each rngName In rngNameList
  78.        
  79.             ' The value of the setting is contained in the cell
  80.            ' where the worksheet row and range name column
  81.            ' intersect.
  82.            Set rngSetting = Intersect(rngSheet.EntireRow, _
  83.                                         rngName.EntireColumn)
  84.                
  85.             ' We only create defined names for settings that
  86.            ' have been given a non-zero-length value.
  87.            If Len(rngSetting.Value) > 0 Then
  88.                 wksSheet.Names.Add rngName.Value, _
  89.                             "=" & rngSetting.Value
  90.             End If
  91.            
  92.         Next rngName
  93.        
  94.     Next rngSheet
  95.    
  96.     Application.ScreenUpdating = True
  97.     Application.Calculation = xlCalculationAutomatic
  98.    
  99. End Sub
  100.  
  101.  
  102. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  103. ' Comments: This procedure removes all the settings specified
  104. '           in the wksUISettings table so that the time entry
  105. '           workbook can be easily maintained.
  106. '
  107. '           This procedure cannot be run from outside the
  108. '           add-in. Run it from this module by placing your
  109. '           cursor somewhere inside the body of the procedure
  110. '           and pressing F5. The PetrasTemplate.xlt workbook
  111. '           must be open before this procedure can be run.
  112. '
  113. ' Date          Developer       Chap    Action
  114. ' --------------------------------------------------------------
  115. ' 06/01/08      Rob Bovey       Ch05    Initial version
  116. '
  117. Public Sub RemoveSettings()
  118.  
  119.     Dim wkbBook As Workbook
  120.     Dim wksSheet As Worksheet
  121.    
  122.     ' Turning off screen updating and calculation
  123.    ' will speed the process significantly.
  124.    Application.ScreenUpdating = False
  125.     Application.Calculation = xlCalculationManual
  126.    
  127.     ' The time entry workbook.
  128.    Set wkbBook = Application.Workbooks(msFILE_TIME_ENTRY)
  129.    
  130.     For Each wksSheet In wkbBook.Worksheets
  131.         wksSheet.Unprotect
  132.         wksSheet.Visible = xlSheetVisible
  133.         wksSheet.Activate
  134.         Application.ActiveWindow.DisplayHeadings = True
  135.         wksSheet.EnableSelection = xlNoRestrictions
  136.         wksSheet.ScrollArea = ""
  137.         With wksSheet.UsedRange
  138.             .EntireColumn.Hidden = False
  139.             .EntireRow.Hidden = False
  140.         End With
  141.     Next wksSheet
  142.  
  143.     Application.ScreenUpdating = True
  144.     Application.Calculation = xlCalculationAutomatic
  145.    
  146. End Sub
  147.  
  148.  
  149. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  150. ' Comments: This procedure reads the defined name settings from
  151. '           the user interface worksheets into the wksUISettings
  152. '           table.
  153. '
  154. '           This procedure cannot be run from outside the
  155. '           add-in. You run it from this module by placing
  156. '           your cursor somewhere inside the body of the
  157. '           procedure and pressing F5.
  158. '
  159. ' Date          Developer       Chap    Action
  160. ' --------------------------------------------------------------
  161. ' 06/01/08      Rob Bovey       Ch05    Initial version
  162. '
  163. Public Sub ReadSettings()
  164.  
  165.     Dim lOffset As Long
  166.     Dim rngName As Range
  167.     Dim rngNameList As Range
  168.     Dim rngSetting As Range
  169.     Dim sMsg As String
  170.     Dim vSetting As Variant
  171.     Dim uAnswer As VbMsgBoxResult
  172.     Dim wkbBook As Workbook
  173.     Dim wksSheet As Worksheet
  174.    
  175.     ' This process is irreversible. Warn the user before
  176.    ' clearing the existing contents of the table.
  177.    uAnswer = vbNo
  178.     sMsg = "Do you want to overwrite the table with" _
  179.             & vbLf & "the current template settings?"
  180.     uAnswer = MsgBox(sMsg, vbQuestion + vbYesNo)
  181.    
  182.     If uAnswer = vbYes Then
  183.    
  184.         Application.ScreenUpdating = False
  185.         Application.Calculation = xlCalculationManual
  186.        
  187.         Set wkbBook = Application.Workbooks(msFILE_TIME_ENTRY)
  188.        
  189.         wksUISettings.UsedRange.Offset(1, 0).Clear
  190.    
  191.         wkbBook.Activate
  192.         Set rngNameList = wksUISettings.Range(msRNG_NAME_LIST)
  193.        
  194.         For Each wksSheet In wkbBook.Worksheets
  195.        
  196.             lOffset = lOffset + 1
  197.            
  198.             With wksUISettings.Range("A1").Offset(lOffset, 0)
  199.            
  200.                 .Value = wksSheet.CodeName
  201.                
  202.                 For Each rngName In rngNameList
  203.                
  204.                     Set rngSetting = Intersect(.EntireRow, _
  205.                                         rngName.EntireColumn)
  206.                    
  207.                     ' The setScrollArea setting requires special
  208.                    ' treatment because it's a named range as
  209.                    ' opposed to a named constant.
  210.                    If rngName.Value = "setScrollArea" Then
  211.                    
  212.                         ' This setting may not exist,
  213.                        ' therefore we wrap it in
  214.                        ' On Error Resume Next.
  215.                        On Error Resume Next
  216.                         rngSetting.Value = _
  217.                         wksSheet.Range("setScrollArea").Address
  218.                         On Error GoTo 0
  219.                        
  220.                     Else
  221.                    
  222.                         vSetting = Empty
  223.                         vSetting = Application.Evaluate( _
  224.                             "'" & wksSheet.Name & "'!" & _
  225.                             rngName.Value)
  226.                            
  227.                         If Not IsError(vSetting) Then
  228.                             rngSetting.Value = vSetting
  229.                         End If
  230.                        
  231.                     End If
  232.                    
  233.                 Next rngName
  234.                
  235.             End With
  236.            
  237.         Next wksSheet
  238.    
  239.         ThisWorkbook.Activate
  240.         Application.ScreenUpdating = True
  241.         Application.Calculation = xlCalculationAutomatic
  242.        
  243.     End If
  244.  
  245. End Sub
  246.  
  247.  
  248. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  249. ' Comments: Obtains the sheet tab name of a worksheet from its
  250. '           code name.
  251. '
  252. ' Arguments:    wkbProject      The project workbook to look in.
  253. '               sCodeName       The CodeName of the worksheet whose
  254. '                               sheet tab name you want.
  255. '
  256. ' Returns:      String          The sheet tab name corresponding to sCodeName.
  257. '
  258. ' Date          Developer       Chap    Action
  259. ' --------------------------------------------------------------
  260. ' 06/01/08      Rob Bovey       Ch05    Initial version
  261. '
  262. Private Function sSheetTabName(ByRef wkbProject As Workbook, _
  263.                             ByRef sCodeName As String) As String
  264.     Dim wksSheet As Worksheet
  265.     For Each wksSheet In wkbProject.Worksheets
  266.         If wksSheet.CodeName = sCodeName Then
  267.             sSheetTabName = wksSheet.Name
  268.             Exit For
  269.         End If
  270.     Next wksSheet
  271. End Function
Advertisement
Add Comment
Please, Sign In to add comment