-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_exc_SummaWbkCellData.bas
111 lines (92 loc) · 3.36 KB
/
mod_exc_SummaWbkCellData.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
Attribute VB_Name = "mod_exc_SummaWbkCellData"
Option Explicit
' error handling tag **************************
Const cStrModuleName As String = "mod_exc_SummaWbkCellData"
' **************************
'
' (c) Join the Bits ltd
'
' Create a summary table by collating specific cell data from all workbooks in a given folder tree
'
' This module is used to enumerate all XLS files in a folder tree
' chosen by the user, and create an output table for each file.
' The column names, and the cell reference for each data value, are
' taken from a sheet in the current workbook called CellList with columns
' NewColumnName
' Worksheet
' Column
' Row
' So the output worksheet contains a filename Column and each of
' the columns defined in CellList, along with a row of data for each file
'
' 160721.AMG use generic workbook opening code in mod_off_FilesFoldersSitesLinks
' 160721.AMG derived from mod_exc_SummaWbkMeta and mod_exc_SummaWkshtSchemas
'
' REFERENCES
' ==========
'
' This module uses the following references (paths and GUIDs may vary)
' (only those required by it's dependent modules)
' DEPENDENCIES
' ============
'
' This module uses the following vba-lib modules
' AND any References specified within them
'
' vba-lib / mod_off_FilesFoldersSitesLinks
' vba-lib / mod_off_ExportListToExcel
' vba-lib / mod_exc_DataTables
'
' IMPROVEMENTS
' ============
'
' * none for now
'
Const cStrFileFilter As String = ".xls|.xlsx|.xltx|.xlsm"
Const cStrCellListSheetName = "CellList"
Dim shtCellList As Excel.Worksheet
Dim rngCellList As Excel.Range
Sub SummariseCellDataFromWorkbooksInPath()
Dim strFileNames() As String
strFileNames() = arrFilteredPathnamesInUserTree(cStrFileFilter, bRecurse:=True)
' func to return the number of elements without error (0 if none)
If strFileNames(0) <> "" Then
Set shtCellList = Excel.ActiveWorkbook.Worksheets(cStrCellListSheetName)
Set rngCellList = rngGetTableDataFromSheet(shtFromWorksheet:=shtCellList, lngNumHeaders:=1)
PrepareListWithHeaders
Dim ifile As Integer
For ifile = 0 To UBound(strFileNames)
AddCellDataToListFor strFileNames(ifile)
Next
ExcelOutputShow
MsgBox "Finished summarising Excel workbook cell data from source folder"
End If
End Sub
Function PrepareListWithHeaders()
ExcelOutputCreateWorksheet
ExcelOutputWriteValue "Filename"
Dim rw As Range
For Each rw In rngCellList.Rows
ExcelOutputWriteValue CStr(rw.Cells(1, 1).Value)
Next
ExcelOutputNextRow
End Function
Function AddCellDataToListFor( _
strWbkName As String _
)
Application.StatusBar = "reading from [" & strWbkName & " ]..."
ExcelOutputWriteValue JustFileName(strWbkName)
Dim wbk As Workbook
Set wbk = wbkOpenSafelyToRead(strWbkName)
If Not wbk Is Nothing Then
Dim rw As Excel.Range
For Each rw In rngCellList.Rows
' debug ExcelOutputWriteValue CStr(rw.Cells(1, 2).Value) & "|" & CStr(rw.Cells(1, 3).Value) & "|" & CStr(rw.Cells(1, 4).Value)
' should this use C<Type> for safety?
ExcelOutputWriteValue wbk.Worksheets(rw.Cells(1, 2).Value).Cells(rw.Cells(1, 4).Value, rw.Cells(1, 3).Value).Value
Next rw
End If
wbkCloseSafely wbk
ExcelOutputNextRow
Application.StatusBar = False
End Function