-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule3.bas
More file actions
316 lines (165 loc) · 7.4 KB
/
Module3.bas
File metadata and controls
316 lines (165 loc) · 7.4 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
Attribute VB_Name = "Module3"
Option Explicit
Dim tab_rearList As ListObject
Public noRoutes() As String
Dim stopNo() As String
'It is very important to understand that the settings LookIn, LookAt and SearchOrder are saved each time the Find Method is used. For this reason one
'should always specify these settings explicitly each and every time you use the Find Method. If you don't, you run the risk of using the Find Method with settings you were not aware of.
Public Sub StopNames()
Call RouteNos
Call StopsSplit
Dim i As Integer, j As Integer, k As Integer, l As Integer, vCount As Integer
Dim rng As Range, rng2 As Range
Dim var1 As String, var2 As String, varEInput As String
Dim routesSplit() As String, stopSplit() As String, varE() As String
Worksheets(4).Select
Range("A1").Offset(0, 1).Select
Worksheets(5).Select
Range("A3").CurrentRegion.Select
'Debug.Print noRoutes(0)
'Debug.Print stopNo(0)
var1 = Range("A3").Address
var2 = Range("A1").Address
For i = 0 To UBound(noRoutes) - 1
Range("A3").CurrentRegion.Select
If noRoutes(i) = "" Then
GoTo NextIteration
ElseIf InStr(noRoutes(i), "-") Then 'Stops with "-"
routesSplit = Split(noRoutes(i), "-")
stopSplit = Split(stopNo(i), "-")
For j = 0 To UBound(stopSplit)
Worksheets(5).Select
Range("A3").CurrentRegion.Select
Set rng = Selection.Find("R" & routesSplit(j), After:=Range(var1), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlRows)
Debug.Print routesSplit(j)
Worksheets(4).Select
If i = 0 And j = 0 Then
Selection.End(xlDown).Offset(1, 0).Select
ElseIf j = 0 Then
Selection.End(xlDown).End(xlDown).Offset(1, 0).Select
End If
If rng Is Nothing Then
GoTo NextIt
Else
varEInput = Module5.KeyListOnly(routesSplit(j))
'Function
varE = Trial_Func(varEInput)
'New code to integrate into ticket
Worksheets(4).Select
Debug.Print i, j
For l = 0 To UBound(varE) - 1
Selection.Offset(l, 0).Value = "R" & routesSplit(j) & "- " & varE(l)
Next l
Selection.Offset(l, 0).Select
Worksheets(5).Select
End If
NextIt:
Next j
'New Code
Worksheets(4).Select
var2 = Selection.End(xlDown).End(xlDown).Address
Worksheets(5).Select
Else 'Stops with no "-"
Set rng = Selection.Find("R" & noRoutes(i), After:=Range(var1), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlRows)
Debug.Print rng
var1 = rng.Address
Worksheets(5).Select
rng.Select
Selection.Offset(0, 3).Select
Debug.Print stopNo(i)
varE = Trial_Func(stopNo(i))
'New Code
Worksheets(4).Select
Range(var2).Select
Selection.CurrentRegion.Select
'Debug.Print "Route " & "0" & noRoutes(i)
If CInt(noRoutes(i)) < 10 Then
Set rng2 = Selection.Find("Route " & "0" & noRoutes(i), After:=Range(var2), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlRows)
Else
Set rng2 = Selection.Find("Route " & noRoutes(i), After:=Range(var2), LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlRows)
End If
Debug.Print rng2.Address
Range(rng2.Address).Select
var2 = Selection.End(xlDown).Address
Selection.Offset(1, 0).Select
For l = 0 To UBound(varE) - 1
Selection.Offset(l, 0).Value = "R" & noRoutes(i) & "- " & varE(l)
Next l
Selection.Offset(l, 0).Select
Worksheets(5).Select
End If
NextIteration:
Next i
Application.CutCopyMode = False
Worksheets(4).Select
End Sub
Function Trial_Func(str_arr As String) As String()
Dim i As Integer, k As Integer, vCount As Integer
Dim varE() As String
vCount = 0
If CInt(str_arr) > 1 Then 'More than 1 stop
ReDim varE(CInt(str_arr)) As String
For k = 0 To CInt(str_arr) - 1
If Not IsEmpty(Selection.Offset(k + vCount, 0)) Then
varE(k) = Selection.Offset(k + vCount, 0).Value
Else
Do While IsEmpty(Selection.Offset(k + vCount, 0))
vCount = vCount + 1
Loop
varE(k) = Selection.Offset(k + vCount, 0).Value
End If
Next k
Trial_Func = varE
Else
ReDim varE(1)
varE(0) = Selection.Value
Trial_Func = varE
End If
End Function
Function SplitForSlash(col_or_row As ListObject, col_num As Integer) As String()
Dim n As Integer, x As Integer, i As Integer, sumStops As Integer
Dim noRoutes() As String, routeNo() As String
n = Int(col_or_row.ListRows.Count)
ReDim routeNo(n) As String
For x = 1 To col_or_row.ListRows.Count
If InStr(col_or_row.DataBodyRange(x, col_num), "/") Then
noRoutes() = Split(col_or_row.DataBodyRange(x, col_num), "/")
' Debug.Print noRoutes(0), noRoutes(1)
routeNo(x - 1) = noRoutes(0)
Else
routeNo(x - 1) = col_or_row.DataBodyRange(x, col_num)
End If
Next x
SplitForSlash = routeNo
End Function
Sub RouteNos()
Dim i As Integer, j As Integer
Dim routeNo_nozero() As String
Set tab_rearList = Worksheets(3).ListObjects("RearLoaderList")
noRoutes = SplitForSlash(tab_rearList, 5)
'Debug.Print UBound(noRoutes), noRoutes(20)
For i = 0 To UBound(noRoutes) - 1
If InStr(noRoutes(i), "-") Then
routeNo_nozero = Split(noRoutes(i), "-")
'Debug.Print UBound(routeNo_nozero)
For j = 0 To UBound(routeNo_nozero)
If Left(routeNo_nozero(j), 1) = "0" Then
routeNo_nozero(j) = Right(routeNo_nozero(j), 1)
End If
Next j
noRoutes(i) = Join(routeNo_nozero, "-")
ElseIf Not InStr(noRoutes(i), "-") And Left(noRoutes(i), 1) = "0" Then
noRoutes(i) = Right(noRoutes(i), 1)
End If
Next i
'Debug.Print noRoutes(2) ', noRoutes(8)
End Sub
Sub StopsSplit()
Set tab_rearList = Worksheets(3).ListObjects("RearLoaderList")
stopNo = SplitForSlash(tab_rearList, 7)
Debug.Print stopNo(2)
End Sub