I have this code for work that has a very simple purpose of filling in the hours of regular(A2), overtime(B2), and double OT(C2) work based on the value I input into hours(E2).
I created my macro, attempted to scramble together the correct code (I can correct that later as needed) and now I am receiving a "Mismatch Error" in the highlighted line of code. I have searched several different resources online and just can't seem to figure out what I am missing with this. I have the value of cell E2 defined as a double, and I have a test value (15.7) input into the proper cell (E2), yet it still shows cell E2 as having no value.
Any help is appreciated.
Calculations
is a Worksheet object.
Worksheets(value)
can be passed a string value, ie the name of the worksheet (eg Worksheets("Calculations")
), or an index (eg Worksheets(1)
), but it cannot be passed a worksheet object.
Change all your uses of Worksheets(Calculations)
to Worksheets("Calculations")
(eg Worksheets("Calculations").Range("E2").Value
), or bypass the Worksheets
method entirely and just use Calculations
(eg Calculations.Range("E2").Value
).
Also note that the ElseIf
portions of your If
statement won't be processed if an earlier portion of the statement has been evaluated as True
. The first part of your statement will execute if Hours > 8
, so none of the other steps will evaluate unless Hours <= 8.
You also have one statement where you use an And
operator incorrectly. An And
operator checks whether the left operand and the right operand are both True
and, if so, returns True
, otherwise it returns False
. (Eg a statement such as If x > 5 And x < 20 Then
would be a sensible way to use it.) I believe you are trying to use it in order to perform two statements. The correct syntax to do that would be something like x = 5 : y = 2
, or just put the two statements on separate lines.
I believe the following will do what you are trying to achieve:
Sub Calculate
With Calculations
hours = .Range("E2").Value
If hours <= 8 Then
.Range("A2").Value = hours
.Range("B2").Value = 0
.Range("C2").Value = 0
ElseIf hours <= 12 Then
.Range("A2").Value = 8
.Range("B2").Value = hours - 8
.Range("C2").Value = 0
Else
.Range("A2").Value = 8
.Range("B2").Value = 4
.Range("C2").Value = hours - 12
End If
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.