I am attempting to create my first user defined function in the Excel Visual Basic Editor. I am using Excel 2011 on a Macbook Pro 2011 with OS X El Capitan Version 10.11.1
I am working on creating a workbook that will use my functions to figure results for a barrel race in a 4 Division format. The divisions are as follows:
The first division or "1D" are the fastest times up to 0.499 seconds off of the fastest time.
The second division or "2D" are the next fastest times between 0.5 and 0.999 seconds off of the fastest time.
The third division or "3D" times are between 1 second and 1.499 seconds off of the fastest time.
Lastly, the fourth division or "4D" times are 1.5 or more seconds off of the fastest time.
This is the current code I have to figure these divisions:
Function DIV(pVal)
If pVal = "Z" Then
DIV = ""
ElseIf pVal = 999 Then
DIV = "DQ"
ElseIf pVal > 100 Then
DIV = "NT"
ElseIf pVal < (Range("B2") + 0.5) Then
DIV = "X1D"
ElseIf ActiveCell.Offset(-1, 0) = "X1D" And pVal < (Range("B2") + 1) Then
DIV = "2D"
ElseIf pVal < (Range("B2") + 1) Then
DIV = "X2D"
ElseIf pVal < (Range("B2") + 1.5) Then
DIV = "X3D"
ElseIf pVal > (Range("B2") + 1.499) Then
DIV = "X4D"
Else
DIV = "Incorrect"
End If
End Function
Now, I want only the first time in each division to result in "2D", "3D" & "4D" and the rest of the times in each division to result in "X1D", "X2D", "X3D" & "X4D" because I will be applying a conditional formatting so that anything beginning with "X" will appear in white text and therefore be invisible when looking at it, however, I will still be able to use it for future functions when I move on to the placings in each division.
So for example, I'm currently working on the 2D and I want all times that are between 0.5 and 0.999 seconds off of the fastest time to return a result of "X2D" except for the first one which would mean that the cell above it would say "X1D".
Everything in the code above is working except for the ElseIf that should be returning the "2D". It is coming back as false and moving on to the next ElseIf where it then returns "X2D".
I have removed parts of the code one at a time and tested them. I have narrowed the problem down to the ActiveCell.Offset section.
I've searched all over the internet for the past three days trying to figure out what I'm missing and I just can't find it. The following are all the ways I've tried typing it based on what I've found online and so far nothing has worked.
ElseIf ActiveCell.Offset(rowOffset:=-1, columnOffset:=0) = "X1D" And pVal < (Range("B2") + 1) Then DIV = "2D"
ElseIf pVal < (Range("B2") + 1) And ActiveCell.Offset(-1, 0) = "X1D" Then DIV = "2D"
ElseIf pVal < (Range("B2") + 1) And ActiveCell.Offset(-1, 0).Select = "X1D" Then DIV = "2D"
ElseIf pVal < (Range("B2") + 1) And ActiveCell.Offset(-1, 0).Range = "X1D" Then DIV = "2D"
ElseIf pVal < (Range("B2") + 1) And ActiveCell.Offset(-1, 0).Activate = "X1D" Then DIV = "2D"
I have tried one other way of coding it based on what someone suggested on another site and that was this :
Function DIV(pVal)
If pVal = "Z" Then DIV = ""
If pVal = 999 Then DIV = "DQ"
If pVal > 100 Then DIV = "NT"
If pVal < (Range("B2") + 0.5) Then DIV = "X1D"
If ActiveCell.Offset(-1, 0) = "X1D" And pVal < (Range("B2") + 1) Then DIV = "2D"
If pVal < 100 Then DIV = "Correct"
If DIV = "" Then DIV = "Incorrect"
End Function
When I tried this one it made the entire function stop working and all results came back as "Correct".
I am at a complete loss as to what I should do to fix this and if anyone has any suggestions I would be very appreciative.
Thank you!
EDIT** This function will only be used in all of Column C except for C1 & C2 because C1 is a header cell and C2 will always be 1D.
Specific example of how the function will be used:
Let's say there are 12 times, 3 in each division.
The fast time which will always be in B2 is 15.000.
The time in B5 is 15.5 which is the first 2D time.
The function is in C5 and should first ask if B5 is = "Z", if true it should leave the cell blank, if not true it should then ask if B5 is = 999, if true it should return "DQ", if not true it should then ask if B5 is > 100, if true it should return "NT", if not true it should then ask if B5 is < B2 + 0.5, if true it should return "X1D", if not true it should then ask if B5 is < B2 + 0.999 AND C4 = "X1D", if true it should return "2D", if not true it should then ask if B5 < B2 + 0.999, if true it should return "X2D", if not true it should go on to ask about the next divisions. If I can figure out how to do this with the 2nd division I can learn from that and be able to finish the other divisions and hopefully create new functions for the other things I want the workbook to do.
I hope this helps!