VBA error Not calculating If then statement

M

Marcie

See VBA below. I cannot figure out how to get this to work so that the after
entering in the Name, hours and pay rate for it to calculate the pay and
insert in my worksheet.

Private Sub CommandButtonOk_Click()
If TextBoxEmployeeName.Text = "" Then
MsgBox "You must enter a name."
Exit Sub
End If
NextBlankRow = Application.WorksheetFunction.CountA(Range("ColA")) + 1
'MsgBox (NextBlankRow)
Cells(NextBlankRow, 1) = TextBoxEmployeeName.Text
Cells(NextBlankRow, 2) = TextBoxHours.Value
Cells(NextBlankRow, 3) = TextBoxPayRate.Value

If (Hours > 40) Then
OverTime = Hours - 40
Pay = 40 * PayRate + OverTime * PayRate * 1.5
Else
Pay = 40 * PayRate
End If

TextBoxEmployeeName.Text = ""
TextBoxHours.Value = ""
TextBoxPayRate.Value = ""


TextBoxEmployeeName.SetFocus

End Sub
 
T

Tom Ogilvy

You haven't included any command that would do that. You also haven't
assigned any values to your variables.

Private Sub CommandButtonOk_Click()
Dim pay as Double, OverTime as Double
Dim PayRate as Double, Hours as Double
If TextBoxEmployeeName.Text = "" Then
MsgBox "You must enter a name."
Exit Sub
End If
NextBlankRow = Application.WorksheetFunction.CountA(Range("ColA")) + 1
'MsgBox (NextBlankRow)
Cells(NextBlankRow, 1) = TextBoxEmployeeName.Text
Cells(NextBlankRow, 2) = TextBoxHours.Value
Cells(NextBlankRow, 3) = TextBoxPayRate.Value

PayRate = Cells(NextBlankRow,3).Value
Hours = Cells(NextBlankRow,2).Value

If (Hours > 40) Then
OverTime = Hours - 40
Pay = 40 * PayRate + OverTime * PayRate * 1.5
Else
Pay = 40 * PayRate
End If

Cells(NextBlankRow,4).Value = Pay

TextBoxEmployeeName.Text = ""
TextBoxHours.Value = ""
TextBoxPayRate.Value = ""


TextBoxEmployeeName.SetFocus

End Sub
 
G

Greg Wilson

Tom beat me to it. Further to Tom's code, I suggest setting the focus to
TextBoxEmployeeName if no name is selected:
If TextBoxEmployeeName.Text = "" Then
MsgBox "You must enter a name."
TextBoxEmployeeName.SetFocus
Exit Sub
End If

Greg
 
M

Marcie

Tom,
Thank you for your help. It worked. I'm new at this and new I needed to
assign something somewhere but wasn't sure. Thanks again.
Marcie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top