0

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
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Alex Lowe
  • 783
  • 3
  • 20
  • 43
  • 2
    You can't set a formula from inside a UDF. – Comintern Feb 27 '19 at 20:22
  • @Comintern When I take this out...`sh.Range("B" & i).Formula = "=A" & i & "*" & rate & "*24"` from the function at the very top of my question it works fine. Why's that? – Alex Lowe Feb 27 '19 at 20:25
  • 2
    A UDF can only return a value to the calling cell (or cells if the UDF returns an array). See here for an overview: https://support.microsoft.com/en-us/help/170787/description-of-limitations-of-custom-functions-in-excel – Tim Williams Feb 27 '19 at 20:27
  • 1
    Well, when you take out that line, you're no longer setting a formula from inside a UDF. – Comintern Feb 27 '19 at 20:28
  • OK, I very much appreciate the help. I've been banging my head against the wall for some time wondering what I've been doing wrong. – Alex Lowe Feb 27 '19 at 20:30

0 Answers0