I really want to be a programmer….no REALLY!!!!
Years ago, at least as far back as kindergarten, I knew I wanted to grow up and be a programmer. Life took small detours along the way and I never followed through with my dream of writing code full time. Today I am an ERP administrator, which means my main function is to understand the system that nobody else can. Every now and then it does allow me to flex my mad SQL skillz to get some quick answers. But most of the heavy coding is done by a different team of developers who are usually responding to my requests that essentially say “I need a report that looks like this…”.
Last weekend though I had to come up with a short term solution to a problem. The problem is that a “highly critical” report was needed daily and the developer team wasn’t going to be able to get it out for at least another two to three weeks.
Now, I won’t get in to the debate over the merits of this particular report, or the determination that it needs to be deemed highly critical. But the condescending tone of the previous sentence will give you the gist of my personal take on the situation.
Anyway, my solution to the short term problem is to copy a report in to Excel and have a VBA macro run around and create a stand in report.
Now I will be the first one to admit I am no Excel wizard. I mainly use it to do simple math, and to hold large sets of data that I’m waiting to import in to a SQL database. Beyond that I tend to avoid Excel like the plague.1
Anyway, so here’s the basic problem, look in to the future2 and try to figure out what day we are going to run out of material based on what our usage is going to be.
Now some of you may be thinking to yourself “Wait a second, I just read that Wikipedia about ERP and isn’t there something called MRP3 that is supposed to do exactly that?” At this point I will point you to four paragraphs back, and lets just keep going.
So, we’ve got this mega report dumped in to Excel, need to parse through hundreds if not thousands of lines, do some math, and then spit out a completely different report that basically says exactly the same thing as the first report.4
So lets take a look at the VBA code I used to put all the right formulas in to the Excel sheet.
' Past Due Value ActiveCell.Offset(0, 3).Select ActiveCell.Formula = "=If(" & ActiveCell.Offset(-1, 0).Address & ">;0,0,ABS(" & ActiveCell.Offset(-1, 0).Address & "))" ' Move over one cell and put one huge mega formula in place For i = 1 To 6 ActiveCell.Offset(0, 1).Select ActiveCell.Formula = "=If(" & ActiveCell.Offset(-3, 0).Address & ">0,If(" & ActiveCell.Offset(-1, -1).Address & ">0, IF(" & ActiveCell.Offset(-1, -1).Address & "-" & ActiveCell.Offset(-3, 0).Address & ">0,0,ABS(" & ActiveCell.Offset(-1, -1).Address & "-" & ActiveCell.Offset(-3, 0).Address & "+" & ActiveCell.Offset(0, -1).Address & "))," & ActiveCell.Offset(-3, 0).Address & "),0)" Next i
Take a look at the gigantic huge formula I’m dropping in there. Even though I wrote it, I am still completely surprised that it actually worked. Also, I’m doing the math for 7 columns, but I’ve got one set of logic for the first column and a different set for the other six. At least I was smart enough to use a for loop and keep myself from writing the same chunk of code six times in a row.
Well about two days later people realized that a couple of the rows that are part of the data set shouldn’t be included in the formulas. What complicated matters is that because I was starting from a report that was already doing some of the math, I needed to back out two of the lines from the main results, and then do my math. This all had to be done recursively as well. For an entire afternoon I tried to figure out how to make a stand alone Excel formula do the math. I wasn’t having much luck.
Then it dawned on me, I was using VBA to run the macro, why the FUCK am I using Excel to do the math?
So, this morning I re-wrote that section. Now take a look at my new code
Dim inventory As Integer Dim demand As Integer 'Set current inventory and demand values inventory = ActiveCell.Offset(-5, 2).Value For i = 1 To 7 ActiveCell.Offset(0, 1).Select demand = ActiveCell.Offset(-3) If (inventory >= demand) Then ActiveCell.Value = 0 inventory = inventory - demand ElseIf (inventory < demand) Then ActiveCell.Value = Abs(demand - inventory) inventory = 0 End If Call RemoveFormatsAndRestoreFont Next i
Looks a hell of a lot better. Plus you can actually tell what is going on. Obviously my coding chops have a lot to be desired.
But I was pleased with the sense of accomplishment I had solving this particular problem.5
- One of my pet peeves are co-workers who think Excel is a word processor. I mean COME ON people. ↩
- This is some real crystal ball shit here. ↩
- Yep ↩
- Because the first report doesn’t look like what someone else is used to seeing we need the second report. ↩
- Even though the requirement seemed stupid. ↩
