-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormattingTools.cls
286 lines (247 loc) · 9.13 KB
/
FormattingTools.cls
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "FormattingTools"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'@Folder("Operation")
Option Explicit
Private wsInteract As clsWorksheetsInteraction
Public Sub AddBorder_Hori()
Dim myRow As Range
' Dim num_lastRow As Long
' Dim str_lastRow As String
' Dim num_currentRow As Long
' Dim str_currentRow As String
Dim fRow As Long, lRow As Long, cRow As Long
Dim lastRowOfSameGroup As Long
Dim colNum As Long, colRef As Range
Dim count As Long
Dim inputBox As Variant
Dim lineType As Long
lineType = Application.inputBox( _
prompt:="PLEASE INPUT THE LINE TYPE NUMBER. " & vbNewLine & vbNewLine _
& "NOTE" & vbNewLine _
& "Input 1 for thin line " & vbNewLine _
& "Input 2 for thick line" _
& "Input 3 for double line", _
default:=1, _
Type:=1)
If lineType = 0 Then Exit Sub
Set colRef = AddInputBox_rng( _
prompt:="PLEASE SELECT THE COLUMN. " & vbNewLine & vbNewLine _
& "NOTE" & vbNewLine _
& "1. The column must be selected. " & vbNewLine _
& "2. The bottom border will be added whenever the data in the current row of the SELECTED COLUMN is different from that in the last row")
If colRef Is Nothing Then Exit Sub
count = 0
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
colNum = colRef.column
fRow = Selection.row
lRow = Selection.row + Selection.count - 1
For cRow = fRow To lRow
lastRowOfSameGroup = FindLastRowOfSameGrp(cRow, colNum, maxRow:=lRow)
If lastRowOfSameGroup = -1 Then Exit For
cRow = lastRowOfSameGroup
If lineType = 1 Then
With rows(lastRowOfSameGroup).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 1
End With
ElseIf lineType = 2 Then
With rows(lastRowOfSameGroup).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = 1
End With
ElseIf lineType = 3 Then
With rows(lastRowOfSameGroup).Borders(xlEdgeBottom)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = 1
End With
Else:
MsgBox ("Please Input Valid Line Type Number")
End
End If
Next cRow
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
Sub BoldMaxOfSameGroup()
Dim myRow As Range
Dim lRow As Long
'Dim str_lastRow As String
Dim cRow As Long
Dim lRowOfSameGrp As Long
'Dim str_currentRow As String
Dim colNum As Long
Dim colNum2 As Long 'the column number to determine the maximum value
Dim count As Long
Dim inputBox As Variant
Dim i As Long, j As Long
Dim maxValue As Double 'a temp variable to compare value
Dim cValue As Double
Dim row_maxValue As Long
Dim colRef As Range, colRef2 As Range
'Dim lineType As Integer
Set colRef = AddInputBox_rng(prompt:="PLEASE SELECT THE COLUMN FOR DETERMINING GROUP. ")
If colRef Is Nothing Then Exit Sub
Set colRef2 = AddInputBox_rng(prompt:="PLEASE SELECT THE COLUMN FOR IDENTIFYING MAXIMUM VALUE. ")
If colRef2 Is Nothing Then Exit Sub
colNum = colRef.column
colNum2 = colRef2.column
count = 0
cRow = Selection.row
lRow = Selection.row + Selection.count - 1
On Error GoTo NextIteration:
For i = cRow To lRow
lRowOfSameGrp = FindLastRowOfSameGrp(i, colNum, maxRow:=lRow)
If lRowOfSameGrp = -1 Then GoTo NextIteration
If Not IsNumeric(Cells(i, colNum2).value2) Then
i = lRowOfSameGrp
GoTo NextIteration
End If
cValue = Cells(i, colNum2).value2
maxValue = cValue
row_maxValue = i
For j = i To lRowOfSameGrp
cValue = Cells(j, colNum2)
If cValue > maxValue Then
maxValue = cValue
row_maxValue = j
End If
Next j
rows(row_maxValue).Font.Bold = True
i = lRowOfSameGrp
NextIteration:
Next i
End Sub
Sub ClearBorder_Hori()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim myRow As Range
For Each myRow In Selection.rows
With myRow.Borders(xlEdgeBottom)
.LineStyle = xlLineStyleNone
End With
Next
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
Sub ClearBorder_Right()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim myCol As Range
For Each myCol In Selection.Columns
With myCol.Borders(xlEdgeRight)
.LineStyle = xlLineStyleNone
End With
Next
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
Sub GetMaxOfSameGroup()
Dim myRow As Range
Dim lRow As Long
'Dim str_lastRow As String
Dim cRow As Long
Dim lRowOfSameGrp As Long
'Dim str_currentRow As String
Dim colNum As Long
Dim colNum2 As Long 'the column number to determine the maximum value
Dim count As Long
Dim inputBox As Variant
Dim i As Long, j As Long
Dim maxValue As Double 'a temp variable to compare value
Dim cValue As Double
Dim row_maxValue As Long
Dim colRef As Range, colRef2 As Range, outputRng As Range
Dim oWS As Worksheet
'Dim lineType As Integer
Set oWS = ActiveSheet
Set colRef = AddInputBox_rng(prompt:="PLEASE SELECT THE COLUMN FOR DETERMINING GROUP. ")
If colRef Is Nothing Then Exit Sub
Set colRef2 = AddInputBox_rng(prompt:="PLEASE SELECT THE COLUMN FOR IDENTIFYING MAXIMUM VALUE. ")
If colRef2 Is Nothing Then Exit Sub
Set outputRng = AddInputBox_rng(prompt:="PLEASE SELECT THE TARGET OUTPUT LOCATION. ")
If outputRng Is Nothing Then Exit Sub
colNum = colRef.column
colNum2 = colRef2.column
count = 0
cRow = Selection.row
lRow = Selection.row + Selection.count - 1
For i = cRow To lRow
lRowOfSameGrp = FindLastRowOfSameGrp(i, colNum)
cValue = Cells(i, colNum2)
maxValue = cValue
row_maxValue = i
For j = i To lRowOfSameGrp
cValue = Cells(j, colNum2)
If cValue > maxValue Then
maxValue = cValue
row_maxValue = j
End If
Next j
outputRng.Worksheet.Cells(outputRng.row + count, outputRng.column) = oWS.Cells(row_maxValue, colNum)
outputRng.Worksheet.Cells(outputRng.row + count, outputRng.column + 1) = oWS.Cells(row_maxValue, colNum2)
count = count + 1
i = j
Next i
End Sub
Public Sub AddThickBottomLineAtPageBreak()
Dim myWS As Worksheet, i As Long
Set myWS = ActiveSheet
Dim myRow As Long
Debug.Print myWS.PageSetup.PrintArea
For i = 1 To myWS.HPageBreaks.count
myRow = myWS.HPageBreaks(i).Location.row - 1
With rows(myRow).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 1
End With
Next
End Sub
Public Sub SetFooter()
ActiveSheet.PageSetup.RightHeader = "Page &P of &N"
ActiveSheet.PageSetup.RightFooter = "&""Arial""&8" & "Printed at &D &T" & Chr(10) & " &Z&F"
'ActiveSheet.PageSetup.RightFooter.Text.Font = 8
End Sub
Private Function AddInputBox_rng(prompt As String, Optional title As String, Optional default As Range) As Range
If default Is Nothing Then
Set default = Range("D1")
End If
On Error GoTo ErrHandler:
Set AddInputBox_rng = Application.inputBox(prompt:=prompt, title:=title, default:=default.address, Type:=8)
On Error GoTo 0
Exit Function
ErrHandler:
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End
End Function
Private Function FindLastRowOfSameGrp(ByVal rRow As Long, ByVal rCol As Long, Optional ws As Worksheet, Optional maxRow As Long) As Long
Dim cRow As Long
Dim savedText As String, cText As String
If ws Is Nothing Then
Set ws = ActiveSheet
End If
cRow = rRow + 1
'find lRow_SchSameGrp
With ws
savedText = .Cells(rRow, rCol).Text
cText = .Cells(cRow, rCol).Text
Do Until .Cells(cRow, rCol).Text <> savedText And .Cells(cRow, rCol).Text <> vbNullString Or cRow > maxRow
cRow = cRow + 1
cText = .Cells(cRow, rCol).Text
Loop
If cRow > maxRow + 1 Then cRow = 0
FindLastRowOfSameGrp = cRow - 1
End With
End Function