Advertisement
ChrisProsser

text_file_importer_vba

Jul 24th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. ' module level constants / variables
  4. Const mcPathUpdateCell = "$C$2"
  5. Const mcPathDisplayCell = "$B$2"
  6. Const mcCtrlShName = "Control"
  7. Const mcRowOffset = 4
  8. Const mcMaxFiles = 10
  9. Const mcHeadCol = "A"
  10. Const mcFileCol = "B"
  11. Const mcDelimCol = "C"
  12. Const mcHeadsReqCol = "D"
  13. Const mcColOffset = 5
  14. Const mcListStartRow = 2
  15.    
  16.    
  17. Private Sub Workbook_Open()
  18. On Error GoTo Workbook_Open_err
  19.  
  20.     Const cProcName = "Workbook_Open"
  21.    
  22.     ActiveWorkbook.Sheets(mcCtrlShName).Select
  23.  
  24.     Call Gen.InitialiseGlobalVars
  25.    
  26.     ' store current path details in global variables
  27.    gvPath = Range(mcPathUpdateCell).Value
  28.     gvPathFormula = Range(mcPathDisplayCell).Formula
  29.    
  30.     Call GetFileLists
  31.    
  32.    
  33. Workbook_Open_exit:
  34.     Exit Sub
  35.    
  36. Workbook_Open_err:
  37.     If gvErrNotes <> "Exit" Then
  38.         Call Gen.ErrHandler(cProcName, Err.Number, Err.Description)
  39.         If gvErrNotes <> "Exit" Then
  40.             Resume Next
  41.         End If
  42.     End If
  43.    
  44. End Sub
  45.  
  46.  
  47. Sub CheckPath()
  48. On Error GoTo CheckPath_err
  49.  
  50.     Const cProcName = "CheckPath"
  51.    
  52.     gvPath = Range(mcPathUpdateCell).Value
  53.     If gvPath = "" Then
  54.         MsgBox ("A path is required in Cell " & mcPathDisplayCell & " for the " & _
  55.                 "automatic file locator to function correctly." & vbCrLf & vbCrLf & _
  56.                 "Please select a path using the ellipsis button and try again.")
  57.         gvErrNotes = "Exit"
  58.         Error (60001)
  59.     End If
  60.    
  61.    
  62. CheckPath_exit:
  63.     Exit Sub
  64.    
  65. CheckPath_err:
  66.     If gvErrNotes <> "Exit" Then
  67.         Call Gen.ErrHandler(cProcName, Err.Number, Err.Description)
  68.         If gvErrNotes <> "Exit" Then
  69.             Resume Next
  70.         End If
  71.     End If
  72.  
  73. End Sub
  74.  
  75.  
  76. Public Sub GetFileLists()
  77. On Error GoTo GetFileLists_err
  78.  
  79.     ' local constants / variables
  80.    Const cProcName = "GetFileLists"
  81.     Dim vHeading As String
  82.     Dim vRange As String
  83.     Dim vListStartCell As String
  84.     Dim vLastRow As Double
  85.     Dim vListCol As String
  86.     Dim vListColNo As Integer
  87.     Dim vHeadRow As Integer
  88.     Dim i As Integer
  89.    
  90.     MsgBox ("Finding KH4 output file names to refresh lists...")
  91.    
  92.     ' initialise variables
  93.    gvErrNotes = Empty
  94.     vHeading = Empty
  95.     vRange = Empty
  96.     vListStartCell = Empty
  97.     vLastRow = 0
  98.     vListCol = Empty
  99.     vListColNo = 0
  100.     vHeadRow = 0
  101.     i = 0
  102.    
  103.     ' check if there a path to follow
  104.    Call CheckPath
  105.    
  106.     ' loop through rows from A5 for mcMaxFiles to pick up each heading
  107.    For i = 1 To mcMaxFiles
  108.         vListColNo = mcColOffset + i
  109.         vHeadRow = mcRowOffset + i
  110.         vHeading = Range(mcHeadCol & vHeadRow).Value
  111.        
  112.         ' check if cell has a value before continuing
  113.        If vHeading <> "" Then
  114.             vListCol = Gen.ColNoToLetter(vListColNo)
  115.             vListStartCell = vListCol & mcListStartRow
  116.            
  117.             ' set heading for file list in hidden cells
  118.            Range(vListStartCell).Offset(-1, 0).Value = vHeading
  119.             vLastRow = WorksheetFunction.Max(Gen.LastRow(mcCtrlShName, _
  120.                 vListStartCell), mcListStartRow) ' highest out of 2 or lastrow
  121.            vRange = vListCol & mcListStartRow & ":" & vListCol & vLastRow
  122.            
  123.             ' unhide and clear old values
  124.            Columns(vListCol).EntireColumn.Hidden = False
  125.             Range(vRange).ClearContents
  126.            
  127.             ' get new file list for current heading
  128.            Call Gen.PrintFileList(gvPath, True, vListStartCell, True, vHeading)
  129.            
  130.             ' sort list
  131.            vLastRow = WorksheetFunction.Max(Gen.LastRow(mcCtrlShName, _
  132.                 vListStartCell), mcListStartRow)
  133.             vRange = "$" & vListCol & "$" & mcListStartRow & ":$" & _
  134.                 vListCol & "$" & vLastRow
  135.             Columns(vListCol).Sort key1:=Range(vRange), _
  136.                 order1:=xlDescending, Header:=xlNo
  137.            
  138.             ' get lists (for dropdowns on filenames) and default values
  139.            With Range(mcFileCol & vHeadRow)
  140.                 .Value = Range(vListStartCell).Value ' sets default to first in list
  141.                
  142.                 ' create validation list with dropdown
  143.                With .validation
  144.                     .Delete
  145.                     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
  146.                         Operator:=xlBetween, Formula1:="=" & vRange
  147.                     .IgnoreBlank = True
  148.                     .InCellDropdown = True
  149.                 End With
  150.             End With
  151.        
  152.         ' hide column
  153.        Columns(vListCol).EntireColumn.Hidden = True
  154.            
  155.         ' break loop if no heading in cell
  156.        Else
  157.             MsgBox ("No heading found on row " & vHeadRow & ", stopping loop." & _
  158.                     vbCrLf & vbCrLf & (i - 1) & " lists updated.")
  159.             Exit For
  160.            
  161.         End If
  162.        
  163.         ' next iteration through loop
  164.        Next i
  165.      
  166.     Range(mcFileCol & (mcRowOffset + 1)).Select
  167.    
  168.    
  169. GetFileLists_exit:
  170.     Exit Sub
  171.    
  172. GetFileLists_err:
  173.     If gvErrNotes <> "Exit" Then
  174.         Call Gen.ErrHandler(cProcName, Err.Number, Err.Description)
  175.         If gvErrNotes <> "Exit" Then
  176.             Resume Next
  177.         End If
  178.     End If
  179.  
  180. End Sub
  181.  
  182.  
  183. Public Sub ImportData()
  184. On Error GoTo ImportData_err
  185.  
  186.     ' local constants / variables
  187.    Const cProcName = "GetFileLists"
  188.     Dim vBasePath As String
  189.     Dim vHeadRow As Integer
  190.     Dim vHeading As String
  191.     Dim vPath As String
  192.     Dim vDelim As String
  193.     Dim vHeadsReq As String
  194.     Dim vPasteFrom As String
  195.     Dim i As Integer
  196.    
  197.     ' initialise variables
  198.    gvErrNotes = Empty
  199.     vBasePath = Empty
  200.     vHeadRow = 0
  201.     vHeading = Empty
  202.     vPath = Empty
  203.     vDelim = Empty
  204.     vHeadsReq = Empty
  205.     vPasteFrom = Empty
  206.     i = 0
  207.    
  208.     ' check if there a path to follow
  209.    Call CheckPath
  210.     vBasePath = gvPath
  211.    
  212.     ' *** process to list and remove old sheets???
  213.    
  214.     ' loop through rows from A5 for mcMaxFiles to pick up each heading
  215.    For i = 1 To mcMaxFiles
  216.         ActiveWorkbook.Sheets(mcCtrlShName).Select
  217.         vHeadRow = mcRowOffset + i
  218.         vHeading = Range(mcHeadCol & vHeadRow).Value
  219.        
  220.         ' check if cell has a value before continuing
  221.        If vHeading <> "" Then
  222.    
  223.             ' get full paths
  224.            If Right(vBasePath, 1) <> "\" Then
  225.                 vBasePath = vBasePath & "\"
  226.             End If
  227.             vPath = vBasePath & Range(mcFileCol & vHeadRow).Value
  228.            
  229.             ' set file options
  230.            vDelim = Range(mcDelimCol & vHeadRow).Value
  231.             vHeadsReq = Range(mcHeadsReqCol & vHeadRow).Value
  232.             vPasteFrom = IIf(vHeadsReq = "Yes", "$A$1", "$A$2")
  233.            
  234.             ' call data Import_Text_Data procedure
  235.            Call Gen.Import_Text_Data(vHeading, vPath, vDelim, vPasteFrom)
  236.            
  237.        
  238.            
  239.         ' break loop if no heading in cell
  240.        Else
  241.             MsgBox ("No heading found on row " & vHeadRow & ", stopping loop." & _
  242.                     vbCrLf & vbCrLf & (i - 1) & " files imported.")
  243.             Exit For
  244.            
  245.         End If
  246.        
  247.         ' next iteration through loop
  248.        Next i
  249.    
  250.    
  251. ImportData_exit:
  252.     Exit Sub
  253.    
  254. ImportData_err:
  255.     If gvErrNotes <> "Exit" Then
  256.         Call Gen.ErrHandler(cProcName, Err.Number, Err.Description)
  257.         If gvErrNotes <> "Exit" Then
  258.             Resume Next
  259.         End If
  260.     End If
  261. End Sub
  262.  
  263.  
  264. Public Sub CallGetFolder()
  265. On Error GoTo CallGetFolder_err
  266.  
  267.     ' local constants / variables
  268.    Const cProcName = "CallGetFolder"
  269.     Dim vFolder As String
  270.     Dim vStartFrom As String
  271.    
  272.     ' initialise variables
  273.    gvErrNotes = Empty
  274.     vFolder = Empty
  275.     vStartFrom = Empty
  276.    
  277.     If Range(mcPathUpdateCell).Value = "" Then
  278.         vStartFrom = Environ("UserProfile")
  279.     Else
  280.         vStartFrom = Range(mcPathUpdateCell).Value
  281.     End If
  282.    
  283.     vFolder = Gen.GetFolder(vStartFrom) '"\\julie\logs\Keyhole"
  284.    
  285.     ' update if a value is returned
  286.    If vFolder = "" Then
  287.         Debug.Print "Not updating cell as no value returned"
  288.     Else
  289.         Range(mcPathUpdateCell).Value = vFolder
  290.     End If
  291.    
  292.     ' check if formula is still where it should be - replace if not
  293.    If Range(mcPathDisplayCell).Formula = "" Then
  294.         Range(mcPathDisplayCell).Formula = _
  295.             "=hyperlink(cell(""contents""," & mcPathUpdateCell & "), " & _
  296.                        "cell(""contents""," & mcPathUpdateCell & "))"
  297.         Range(mcPathDisplayCell).style = "lnk"
  298.     ' Else: Refresh Formula
  299.    Else
  300.         Range(mcPathDisplayCell).Calculate
  301.     End If
  302.    
  303.     ' store current path details in global variables
  304.    gvPath = Range(mcPathUpdateCell).Value
  305.     gvPathFormula = Range(mcPathDisplayCell).Formula
  306.    
  307.  
  308. CallGetFolder_exit:
  309.     Exit Sub
  310.    
  311. CallGetFolder_err:
  312.     If gvErrNotes <> "Exit" Then
  313.         Call Gen.ErrHandler(cProcName, Err.Number, Err.Description)
  314.         If gvErrNotes <> "Exit" Then
  315.             Resume Next
  316.         End If
  317.     End If
  318. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement