The code below is is supposed to set an hourly rate per client as well as go down the length of the spreadsheet and calculate the total amount that each client owes me and then return that value to the cell, as I work as a freelancer. The code that I have currently doesn't run since I added the "rate" feature into the program. In the excel spreadsheet the error that I'm getting looks like this #VALUE!
. I have no explanation for why it's that way.
Function clientCalc(client As String, rate As Integer)
Dim tbl As ListObject
Dim tableLen As Double
Dim sh As Worksheet
Dim tally As Double
Set sh = ThisWorkbook.Sheets("Hours")
Set tbl = sh.ListObjects("Main")
tableLen = tbl.Range.Rows.Count
tally = 0
For i = 2 To tableLen
If Range("H" & i).Value = client Then
sh.Range("B" & i).Formula = "=A" & i & "*" & rate & "*24"
tally = tally + Range("D" & i).Value
End If
Next i
clientCalc = Format(tally, "Currency")
End Function
In order for me to make this work in some way I have to make the program above and turn it into a Sub
rather than a Function
. In that case I've found that this works pretty well...
Sub setRate(client As String, rate As Double)
Dim tbl As ListObject
Dim tableLen As Double
Dim sh As Worksheet
Dim tally As Double
Set sh = ThisWorkbook.Sheets("Sheet1")
Set tbl = sh.ListObjects("Table3")
tableLen = tbl.Range.Rows.Count
tally = 0
For i = 2 To tableLen
If Range("C" & i).Value = client Then
sh.Range("B" & i).Formula = "=A" & i & "*" & rate & "*24"
End If
Next i
End Sub
Sub main()
Call setRate("Client_One", 30)
Call setRate("Client_Two", 40)
End Sub
Function clientCalc(client As String)
Dim tbl As ListObject
Dim tableLen As Double
Dim sh As Worksheet
Dim tally As Double
Set sh = ThisWorkbook.Sheets("Hours")
Set tbl = sh.ListObjects("Main")
tableLen = tbl.Range.Rows.Count
tally = 0
For i = 2 To tableLen
If Range("H" & i).Value = client Then
tally = tally + Range("D" & i).Value
End If
Next i
clientCalc = Format(tally, "Currency")
End Function