Search code examples
excelvbaformulamismatch

Type Mismatch: Range Formula


I am completely at a loss as to why I am getting the "type mismatch" error.

The error appears on the Range(RelAddy).Formula line.

Sub CallingUserformAssignJC()

BeforeLastRow = Sheets("Information").Cells(Rows.Count, 19).End(xlUp).Row
Sheets("Information").Range("U2:U" & BeforeLastRow).ClearContents

LastRowAC = Sheets("Information").Cells(Rows.Count, 19).End(xlUp).Row
LastCol = Sheets("Today").Cells(1, Columns.Count).End(xlToLeft).Column

For j = 1 To LastCol
    CurrLastRow = Sheets("Today").Cells(Rows.Count, j).End(xlUp).Row
    startAddy = Sheets("Today").Cells(2, j + 1).Address
    endAddy = Sheets("Today").Cells(CurrLastRow, j + 1).Address
    RelAddy = Sheets("Today").Cells(CurrLastRow + 1, j + 1).Address
    Range(RelAddy).Formula = "=COUNTA(" & Sheets("Today").Range(startAddy, endAddy) & ")"
    unassignedMilestone = Sheets("Today").Cells(CurrLastRow + 1, j + 1).Value
    Do While unassignedMilestone <> 0
        frmAssignJC.Show
        frmAssignJC.Hide
    Loop
    j = j + 3
Next j

End Sub

I have looked at the locals window, and startAddy, endAddy, and RelAddy are all type Variant/String. Any help would be appreciated thank you.


Solution

  • The problem is in Range("B2").Formula = "=COUNTA(" & Sheets("Today").Range(startAddy, endAddy) & ")"

    Try Range("B2").Formula = "=COUNTA(" & Sheets("Today").Range(startAddy, endAddy).Address & ")"

    In order to use a formula you need to get the Address of the range instead of just the range