-
Notifications
You must be signed in to change notification settings - Fork 1
/
mSheetRepair.bas
282 lines (215 loc) · 7.9 KB
/
mSheetRepair.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
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
Attribute VB_Name = "mSheetRepair"
Option Explicit
Function ConvertToLetter(ByVal iCol As Integer) As String
On Error GoTo ErrorHandler
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
Exit Function
ErrorHandler:
Call p_ErrorHandler(X, "ConvertToLetter" & Err.Number & Err.Description & Err.HelpContext)
End
End Function
Function getClearName(ByVal vCurrStr As String)
Dim strPattern As String: strPattern = "[^a-zA-Z0-9]" 'The regex pattern to find special characters
Dim strReplace As String: strReplace = "" 'The replacement for the special characters
Dim regEx
Set regEx = CreateObject("vbscript.regexp") 'Initialize the regex object
' Configure the regex object
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
' Perform the regex replacement
getClearName = regEx.Replace(vCurrStr, strReplace)
End Function
Function DoubleChar(ByVal iCol As Integer) As String
DoubleChar = LCase(ConvertToLetter(iCol)) & Int(iCol * Rnd + iCol)
DoubleChar = "." & getClearName(DoubleChar)
End Function
Function isCheckName(ByVal vSheetName As String) As Boolean
On Error GoTo ErrorHandler
Dim vWS_Count, i
vWS_Count = ActiveWorkbook.Worksheets.Count
isCheckName = True
For i = 1 To vWS_Count
If InStr(ActiveWorkbook.Worksheets(i).Name, vSheetName) > 0 Then
isCheckName = False
End If
Next i
Exit Function
ErrorHandler:
Call p_ErrorHandler(X, "isCheckName" & Err.Number & Err.Description & Err.HelpContext)
End
End Function
Function getRandomNum()
Dim vNumber, VCount, vSec, vGO
On Error Resume Next
vNumber = 1
VCount = ActiveWorkbook.Worksheets.Count
vGO = 0
getRandomNum = 0
While vNumber < VCount
vNumber = VCount * vNumber
vSec = Second(Now)
vNumber = vNumber - (Round(vNumber / vSec)) '(vNumber * vSec) Mod vNumber
vNumber = vNumber * (vNumber * Second(Now))
vNumber = vNumber - (Round(vNumber / vSec))
vNumber = Int(vNumber - (Round(vNumber / 100)))
vNumber = vSec + vNumber - 100 * Int(vNumber / 100)
vNumber = Abs(vNumber)
vNumber = vNumber * (vNumber * vSec - vSec * Int(vNumber * vSec / (vSec * 10)))
vNumber = Int(vNumber * vSec - 100 * Int(vNumber * vSec / 100))
vNumber = Left("" & vNumber * vSec, 3) * Right("" & vNumber * Second(Now), 3) * Right("" & vNumber * Second(Now), 1) * Left("" & vNumber * Second(Now), 1)
vNumber = Left("" & vNumber * Second(Now), 1) * Right("" & vNumber * Second(Now), 1) + Right("" & vNumber * Second(Now), 2) + Left("" & vNumber * Second(Now), 2)
vNumber = Left("" & vNumber * Second(Now), 2)
vGO = vGO + 1
If vGO > 99 Then
vNumber = Left("" & vNumber * Second(Now), 2) + VCount
End If
Wend
getRandomNum = vNumber
End Function
Function NewName() As String
On Error GoTo ErrorHandler
Dim vArrName() As String
Dim vNumber, vGO, vErrStr
vArrName() = Split(ActiveSheet.Name, ".")
vArrName() = Split(vArrName(0), " ")
vArrName() = Split(vArrName(0), ",")
Dim isNameCorrect
isNameCorrect = False
vGO = 0
vErrStr = 1
While Not isNameCorrect
vNumber = getRandomNum()
vErrStr = 2
If vNumber > 10 And Err.Number = 0 Then
NewName = Left(vArrName(0), 3) & DoubleChar(vNumber)
vErrStr = 3
isNameCorrect = isCheckName(NewName)
vErrStr = 4
Else
Err.Clear
End If
vGO = vGO + 1
If vGO > 20 Then
NewName = Left(vArrName(0), 3) & DoubleChar(vNumber + vGO + Second(Now))
vErrStr = 5
isNameCorrect = isCheckName(NewName)
vErrStr = 6
End If
Wend
Exit Function
ErrorHandler:
Call p_ErrorHandler(X, "NewName " & Err.Number & Err.Description & Err.HelpContext & " vGO " & vGO & " vErrStr " & vErrStr)
End
End Function
Sub p_CreateSheet(ByVal vOldSheetName As String, ByVal vNewSheetName As String)
On Error GoTo ErrorHandler
Dim j
For j = 1 To ActiveWorkbook.Worksheets.Count
' do soemthing with Worksheets(N)
If ActiveWorkbook.Worksheets(j).Name = vNewSheetName Then
Exit Sub
End If
Next j
ActiveSheet.Cells(1, 1).Select
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = vNewSheetName
Worksheets(vNewSheetName).Move _
After:=Worksheets(vOldSheetName)
l_exit:
Exit Sub
ErrorHandler:
Call p_ErrorHandler(X, "p_CreateSheet " & Err.Number & Err.Description & Err.HelpContext & " vLineErr vLineErr vNewSheetName " & vNewSheetName & "vOldSheetName " & vOldSheetName)
End
End Sub
Sub p_CopySheet(vOldSheetName As String, vNewSheetName As String)
On Error GoTo ErrorHandler
ActiveSheet.Cells(1, 1).Select
Worksheets(vOldSheetName).UsedRange.Copy
Worksheets(vNewSheetName).Range("A1").PasteSpecial xlPasteValues
Worksheets(vNewSheetName).Range("A1").PasteSpecial xlPasteFormulas
' make white color
Worksheets(vNewSheetName).Cells.Select
With Selection.Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With
l_exit:
Exit Sub
ErrorHandler:
Call p_ErrorHandler(X, "p_CopySheet " & Err.Number & Err.Description & Err.HelpContext)
End
End Sub
Public Sub p_CopySheetMain(vOldSheetName As String, vNewSheetName As String)
On Error GoTo ErrorHandler
Call p_CreateSheet(vOldSheetName, vNewSheetName)
Call p_CopySheet(vOldSheetName, vNewSheetName)
l_exit:
Exit Sub
ErrorHandler:
Call p_ErrorHandler(X, "p_CopySheetMain " & Err.Number & Err.Description & Err.HelpContext)
End
End Sub
Public Sub p_RenewSheet()
If ActiveSheet Is Nothing Then
MsgBox "active sheet is not determinated "
End
End If
ActiveSheet.Cells(1, 1).Select
On Error GoTo ErrorHandler
Call p_setExcelCalcOff
Dim vOldSheetName As String
Dim vNewSheetName As String
vOldSheetName = ActiveSheet.Name
vNewSheetName = NewName()
Call p_CopySheetMain(vOldSheetName, vNewSheetName)
Application.DisplayAlerts = False
Worksheets(vOldSheetName).Delete
Application.DisplayAlerts = True
Worksheets(vNewSheetName).Activate
ActiveSheet.Name = vOldSheetName
Call p_setExcelCalcOn
l_exit:
Exit Sub
ErrorHandler:
Call p_ErrorHandler(X, "p_RenewSheet " & Err.Number & Err.Description & Err.HelpContext)
End
End Sub
Public Sub p_RepairRetrive(vIRibbonControl As IRibbonControl)
Call p_RenewSheet
l_exit:
Exit Sub
ErrorHandler:
Call p_ErrorHandler(X, "p_RepairRetrive " & Err.Number & Err.Description & Err.HelpContext)
End
End Sub
Public Sub p_in2plnCopySheetUI(vIRibbonControl As IRibbonControl)
If ActiveSheet Is Nothing Then
MsgBox "active sheet is not determinated "
End
End If
ActiveSheet.Cells(1, 1).Select
On Error GoTo ErrorHandler
Call p_setExcelCalcOff
Call p_CopySheetMain(ActiveSheet.Name, NewName())
Call p_setExcelCalcOn
ActiveSheet.Cells(1, 1).Select
l_exit:
Exit Sub
ErrorHandler:
Call p_ErrorHandler(X, "p_copySheetUI " & Err.Number & Err.Description & Err.HelpContext)
End
End Sub