-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupsolver(for format only).vb
414 lines (271 loc) · 9.43 KB
/
setupsolver(for format only).vb
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
Sub setupsolver()
'NEED TO CHECK THE OPENSOLVER IN TOOLS AS A REFERENCE FOR THIS TO WORK
Dim MaxSpaces As Integer
Dim TotalSpaces As Integer
Dim TotalItems As Integer
Dim counter As Integer
Dim counter2 As Integer
Dim Rng As Range
Dim InputRng As Range, OutRng As Range
Dim Result As OpenSolverResult
Dim ObjFunction As Range
'Delete the Solver Sheet
Application.DisplayAlerts = False 'switching off the alert button
Sheets("Solver Sheet").Delete
Application.DisplayAlerts = True 'switching on the alert button
'Grab the good values
MaxSpaces = Cells(2, 8).Value
TotalSpaces = Cells(4, 8).Value
TotalItems = Cells(6, 8).Value
'Create the new columns
ActiveSheet.Cells(1, 4).Value = "# items/time period"
ActiveSheet.Cells(1, 5).Value = "# items/pallet"
ActiveSheet.Cells(1, 6).Value = "Flow"
'Generate the # items / 2 months column
'counter = 2
'
'For i = 2 To TotalItems + 1
'
' ActiveSheet.Cells(i, 13).Value = "=sum(D" & counter & ":L" & counter & ")"
' counter = counter + 1
'
'Next i
'Generate the # items / pallet
counter = 2
For i = 2 To TotalItems + 1
ActiveSheet.Cells(i, 5).Value = "=B" & counter & " * C" & counter
counter = counter + 1
Next i
'Generate the Flow
counter = 2
For i = 2 To TotalItems + 1
ActiveSheet.Cells(i, 6).Value = "=D" & counter & " / E" & counter
counter = counter + 1
Next i
'Label ranges in Pull from SAP
ActiveWorkbook.Names.Add Name:="MaxSpace", RefersToR1C1:= _
"='Sheet1'!R2C8"
ActiveWorkbook.Names.Add Name:="TotalSpace", RefersToR1C1:= _
"='Sheet1'!R4C8"
ActiveWorkbook.Names.Add Name:="SKUS", RefersToR1C1:= _
"='Sheet1'!R6C8"
ActiveWorkbook.Names.Add Name:="SKUS2USE", RefersToR1C1:= _
"='Sheet1'!R2C1:R1542C1"
'create new sheet
Sheets.Add
ActiveSheet.Name = "Solver Sheet"
'Label ranges in Solver Sheet
ActiveWorkbook.Names.Add Name:="Flow", RefersToR1C1:= _
"='Solver Sheet'!R2C2:R12329C2"
ActiveWorkbook.Names.Add Name:="Choice", RefersToR1C1:= _
"='Solver Sheet'!R2C4:R12329C4"
'Column labels
ActiveSheet.Cells(1, 1).Value = "SAP #"
ActiveSheet.Cells(1, 2).Value = "Flow"
ActiveSheet.Cells(1, 3).Value = "Spaces"
ActiveSheet.Cells(1, 4).Value = "Zij"
ActiveSheet.Cells(1, 5).Value = "Mult"
ActiveSheet.Cells(1, 6).Value = "Restocks"
ActiveSheet.Cells(1, 7).Value = "Total Restocks"
ActiveSheet.Cells(1, 8).Value = "Check"
ActiveSheet.Cells(2, 8).Value = "SAP #"
ActiveSheet.Cells(2, 9).Value = "Zij"
ActiveSheet.Cells(2, 10).Value = "Spaces"
ActiveSheet.Cells(2, 11).Value = "Total Items"
ActiveSheet.Cells(2, 12).Value = "Total Spaces"
'Duplicate the SKUs and Flow
Sheets("Sheet1").Select
ActiveSheet.Range("A2:A" & TotalItems + 1).Select
Selection.Copy
ActiveSheet.Range("T2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Range("A2:A" & TotalItems + 1).Copy Range("T2:T" & TotalItems + 1)
For i = 2 To TotalItems + 1
Cells(i, 21).Value = 8
Next i
Set InputRng = Application.Selection
Set InputRng = Range("T2:U" & TotalItems + 1)
Sheets("Solver Sheet").Select
Set OutRng = ActiveSheet.Range("A2:A" & TotalItems + 1)
Set OutRng = OutRng.Range("A1")
For Each Rng In InputRng.Rows
xValue = Rng.Range("A1").Value
xNum = Rng.Range("B1").Value
OutRng.Resize(xNum, 1).Value = xValue
Set OutRng = OutRng.Offset(xNum, 0)
Next
Sheets("Sheet1").Select
'Range("$O$2:$O$" & TotalItems + 1).Copy Range("$T$2:$T$" & TotalItems + 1)
ActiveSheet.Range("F2:F" & TotalItems + 1).Select
Selection.Copy
ActiveSheet.Range("T2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set InputRng = Application.Selection
Set InputRng = Range("T2:U" & TotalItems + 1)
Sheets("Solver Sheet").Select
Set OutRng = ActiveSheet.Range("B2:B" & TotalItems + 1)
Set OutRng = OutRng.Range("A1")
For Each Rng In InputRng.Rows
xValue = Rng.Range("A1").Value
xNum = Rng.Range("B1").Value
OutRng.Resize(xNum, 1).Value = xValue
Set OutRng = OutRng.Offset(xNum, 0)
Next
'Spaces setup
counter = 1 'counter is used to index to the next space
For i = 2 To TotalItems * MaxSpaces + 1
ActiveSheet.Cells(i, 3).Value = counter
counter = counter + 1
If counter = MaxSpaces + 1 Then
counter = 1
End If
Next i
'Mult
counter = 2
For i = 2 To TotalItems * MaxSpaces + 1
ActiveSheet.Cells(i, 5).Value = "=C" & counter & " * D" & counter
counter = counter + 1
Next i
'Setup restocks
counter = 2
For i = 2 To TotalItems * MaxSpaces + 1
ActiveSheet.Cells(i, 6).Value = "=B" & counter & "/C" & counter & "*D" & counter
counter = counter + 1
Next i
'Setup total restocks cell
ActiveSheet.Cells(2, 7).Value = "=sum(F2:F" & TotalItems * MaxSpaces + 1 & ")"
'Setup SAP # V2
Sheets("Sheet1").Select
ActiveSheet.Range("T2:U" & TotalItems + 1).Clear
Range("A2:A" & TotalItems + 1).Select
Worksheets("Sheet1").Range("A2:A" & TotalItems + 1).Copy Worksheets("Solver Sheet").Range("H3:H" & TotalItems + 1)
Sheets("Solver Sheet").Select
'Setup Zij V2
counter = 2
counter2 = MaxSpaces + 1
For i = 3 To TotalItems + 2
ActiveSheet.Cells(i, 9).Value = "=sum(D" & counter & ":D" & counter2 & ")"
counter = counter + MaxSpaces
counter2 = counter2 + MaxSpaces
Next i
'Setup Spaces V2
counter = 2
counter2 = MaxSpaces + 1
For i = 3 To TotalItems + 2
ActiveSheet.Cells(i, 10).Value = "=sum(E" & counter & ":E" & counter2 & ")"
counter = counter + MaxSpaces
counter2 = counter2 + MaxSpaces
Next i
'Setup Total items
ActiveSheet.Cells(3, 11).Value = "=count(J3:J" & TotalItems + 2 & ")"
'Setup Total spaces
ActiveSheet.Cells(3, 12).Value = "=sum(E2:E" & TotalItems * MaxSpaces + 1 & ")"
'Setting up the solver
SetChosenSolver ("CBC")
SolverOk SetCell:="$G$2", MaxMinVal:=2, ValueOf:=0, ByChange:="$D$2:$D$" & TotalItems * MaxSpaces + 1, _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$I$3:$I$" & TotalItems + 2, Relation:=2, FormulaText:="1"
SolverAdd CellRef:="$K$3", Relation:=2, FormulaText:="" & TotalItems
SolverAdd CellRef:="$L$3", Relation:=2, FormulaText:="" & TotalSpaces
SolverAdd CellRef:="$D$2:$D$" & TotalItems * MaxSpaces + 1, Relation:=1, FormulaText:="1"
Result = RunOpenSolver(False, True) ' do not relax IP, do hide dialogs
'Solver's solution to which SKU gets how many spaces PUT AFTER THE SOLVER CODE
counter = 3
For i = 2 To TotalItems * MaxSpaces + 1
If ActiveSheet.Cells(i, 5).Value > 0 Then
ActiveSheet.Cells(counter, 10).Value = ActiveSheet.Cells(i, 5).Value
counter = counter + 1
End If
Next i
End Sub
Sub epic()
Dim counter As Integer
Dim counter2 As Integer
counter = 2
counter2 = MaxSpaces + 1
For i = 3 To TotalItems + 2
ActiveSheet.Cells(i, 10).Value = "=sum(E" & counter & ":E" & counter2 & ")"
counter = counter + MaxSpaces
counter2 = counter2 + MaxSpaces
Next i
End Sub
Sub pizza()
SolverOk SetCell:="$G$2", MaxMinVal:=2, ValueOf:=0, ByChange:="$D$2:$D$" & 1541 * 8 + 1, _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$I$3:$I$" & 1541 + 2, Relation:=2, FormulaText:="1"
SolverAdd CellRef:="$K$3", Relation:=2, FormulaText:="" & 1541
SolverAdd CellRef:="$L$3", Relation:=2, FormulaText:="" & 2688
SolverAdd CellRef:="$D$2:$D$" & 1541 * 8 + 1, Relation:=1, FormulaText:="1"
End Sub
Sub CopyData()
'Updateby Extendoffice
Dim Rng As Range
Dim InputRng As Range, OutRng As Range
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set OutRng = OutRng.Range("A1")
For Each Rng In InputRng.Rows
xValue = Rng.Range("A1").Value
xNum = Rng.Range("B1").Value
OutRng.Resize(xNum, 1).Value = xValue
Set OutRng = OutRng.Offset(xNum, 0)
Next
End Sub
Sub Sum8()
Dim counter As Integer
Dim counter2 As Integer
counter = 2
counter2 = 11
For i = 2 To 12
ActiveSheet.Cells(i, 10).Value = "=sum(G" & counter & ":G" & counter2 & ")"
counter = counter + 10
counter2 = counter2 + 10
Next i
End Sub
Sub proof()
Dim Result As OpenSolverResult
Dim counter As Integer
Dim counter2 As Integer
Dim indexF As Integer
Dim columnNum As Integer
counter = 1
indexF = 16
columnNum = 11
counter2 = 21
For i = 11 To 110
SolverOk SetCell:="$M$2", MaxMinVal:=2, ValueOf:=0, ByChange:="$G$2:$G$111", _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$O$2", Relation:=2, FormulaText:="" & i
Result = RunOpenSolver(False, True) ' do not relax IP, do hide dialogs
ActiveSheet.Range("K1:M12").Select
Selection.Copy
ActiveSheet.Range("Q1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Range("S2").Select
Selection.Copy
ActiveSheet.Cells(2, counter2).Select
ActiveSheet.Paste
ActiveSheet.Range("Q1:S12").Select
Selection.Copy
If counter = 6 Then
indexF = indexF + 14
columnNum = 11
counter = 1
End If
If i = 27 Then
counter = counter
End If
ActiveSheet.Cells(indexF, columnNum).Select 'select the area to put the solution
ActiveSheet.Paste
ActiveSheet.Cells(indexF - 1, columnNum).Value = "Total space = " & i
SolverDelete CellRef:="$O$2", Relation:=2, FormulaText:="" & i
counter = counter + 1
counter2 = counter2 + 1
columnNum = columnNum + 4
Next i
End Sub