I am currently creating a PTO schedule for my workplace. We have a small company, yet I am attempting to make it easy for us to handle by using buttons and macros in excel.
I have created a button in my worksheet that subtracts 8 hours (a workday) from a persons hours, yet I would like it to ask which person (row) I would like to adjust and which PTO to pull from (column, being either Personal time or Vacation time).
My code right now can only subtract 8 hours from one cell of an employee being his Personal time. I believe I am close, but I need help.
Private Sub cbPlusTimeJR_Click()
Range("f2").Value = Range("f2").Value - 8
End Sub
Above is the code I had used, now I need help beefing it up.
You can use InputBox()
to prompt the user for an answer. Then use If/ElseIF/Else
to test what they inputted and act accordingly.
Something like:
Private Sub cbPlusTimeJR_Click()
'Declare a variable to hold column number
Dim columnNum As Integer
'Declare a variable to hold user response to prompt
Dim vacOrPers As String
'Get user response using an inputBox
vacOrPers = InputBox("Do you wish to subtract from Vacation or Personal", "Hour Changes")
'Test what their response was and act accordingly
If InStr(1, LCase(vacOrPers), "vac") Then
columnNum = 6 'Column F
ElseIf InStr(1, LCase(vacOrPers), "per") Then
columnNum = 7 'Column G
Else
MsgBox ("Please enter Vacation or Personal")
Exit Sub
End If
'Subtract the hours
Cells(5, columnNum).Value = Cells(5, columnNum).Value - 8
End Sub
The tests in the If
portion of the code like InStr(1, LCase(vacOrPers), "vac")
are saying "Does the lowercase version of what the user typed contain the string vac
?