This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmSheetConcat.bas
139 lines (114 loc) · 3.67 KB
/
mSheetConcat.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Attribute VB_Name = "mSheetConcat"
Option Explicit
Private Function SheetExistence( _
ByRef wbkActive As Workbook, _
ByVal strSheetNameToFind As String, _
ByVal blnSheetExists As Boolean) As Boolean
Dim objSheet As Object
For Each objSheet In wbkActive.Worksheets
If strSheetNameToFind = objSheet.Name _
And blnSheetExists = False Then
SheetExistence = True
Exit Function
End If
Next objSheet
End Function
Private Function addset_sht( _
ByRef wbkActive As Workbook, _
ByVal strSheetName As String) As Worksheet
Dim blnSheetExists As Boolean
blnSheetExists = SheetExistence( _
wbkActive, _
strSheetName, _
False)
With wbkActive
Select Case blnSheetExists
Case True
Set addset_sht = .Sheets(strSheetName)
Exit Function
End Select
' If the sub goes here, then sheet is note exists
'and we will create it
Dim shtNew As Worksheet
Set shtNew = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
shtNew.Name = strSheetName
Set addset_sht = shtNew
End With
End Function
Public Sub ActiveSheetsConcat()
On Error GoTo ErrorHandler
Application.Calculation = xlManual
Dim clsModeToast As cRuleToast
Set clsModeToast = New cRuleToast
Dim shtTotal As Worksheet
Dim rngDataPaste As Range
Dim rngDataCopy As Variant
Dim rngShtName As Range
Dim strNameShtWork As String
Dim lastrow As Long
Dim lastrow2 As Long
Const NAMESHTTOTAL As String = "Total"
Const DATACOPYCOLUMNS As Integer = 30
Dim wbkActive As Workbook
Dim wbkNewBook As Workbook
Dim blnIsNewBookCreated As Boolean
Set wbkActive = ActiveWorkbook
' We need to do check of an extension
'
With wbkActive
Select Case True
Case .FileFormat = xlOpenXMLWorkbookMacroEnabled _
Or .FileFormat = xlWorkbookDefault _
Or .FileFormat = xlExcel12
Set wbkNewBook = wbkActive
blnIsNewBookCreated = False
Case .FileFormat = xlWorkbookNormal _
Or .FileFormat = xlExcel9795 _
Or .FileFormat = xlExcel8 _
Or .FileFormat = xlExcel7 _
Or .FileFormat = xlExcel5 _
Or .FileFormat = xlExcel4 _
Or .FileFormat = xlExcel3 _
Or .FileFormat = xlExcel2FarEast _
Or .FileFormat = xlExcel2
'Important note: we will recreate whole workbook
'to handle limit in 65536 rows in older versions of excel.
Set wbkNewBook = Workbooks.Add
blnIsNewBookCreated = True
wbkActive.Activate
End Select
End With
Set shtTotal = addset_sht(wbkNewBook, NAMESHTTOTAL)
Dim shtWork As Worksheet
Dim rngFirstCell As Range
For Each shtWork In wbkActive.Worksheets
With shtWork
If NAMESHTTOTAL = .Name Then GoTo nexti
Set rngFirstCell = .Range("A1")
lastrow = rngFirstCell.CurrentRegion.Rows.Count
strNameShtWork = .Name
rngDataCopy = .Range(rngFirstCell, .Cells(lastrow, DATACOPYCOLUMNS)).Value
End With
With shtTotal
Set rngFirstCell = .Range("A1")
lastrow2 = rngFirstCell.CurrentRegion.Rows.Count + 1
Set rngDataPaste = .Range(.Cells(lastrow2, 2), _
.Cells(lastrow2 + lastrow - 1, DATACOPYCOLUMNS + 1))
rngDataPaste.Value = rngDataCopy
Set rngShtName = .Range(.Cells(lastrow2, 1), .Cells(lastrow2 + lastrow - 1, 1))
rngShtName.Value2 = strNameShtWork
End With
nexti:
Next shtWork
Select Case blnIsNewBookCreated
Case True
wbkActive.Close SaveChanges:=False
wbkNewBook.Activate
End Select
clsModeToast.OpenToast enmControlType.ectSuccess
Exit sub
ErrorHandler:
Dim dangerText As String
dangerText = Err.Description & " " & Err.Number
clsModeToast.OpenToast enmControlType.ectDanger, dangerText
End Sub