Search code examples
excelvbaexcel-formulacell

Inserting Formula into cells VBA


I am trying to "print" formulas into an excel sheet with VBA. The code works without the "=" sign in front of the COUNTIF, but when I Insert it I get a Run-Time error 1004.

Can someone help me?

Here is the code that gets the error

Sub Looksie()

    Dim Tru As Range
    Dim i As Integer
    i = 2
    
    For Each Tru In Range("A2:A300")
        
            Sheets("Resultat").Select
            Cells(i, 2).Formula = "=COUNTIF(Input!" & Cells(19, i + 1).Address & ":" & Cells(5000, i + 1).Address & ";" & """" & "A" & """" & ")"
     
            i = i + 1
        
    Next Tru
 End Sub

Thank you


Solution

  • Could you use next code?

    I'm edited only ":" to ",".

    Sub Looksie()
    
        Dim Tru As Range
        Dim i As Integer
        i = 2
        
        For Each Tru In Range("A2:A300")
            
                Sheets("Resultat").Select
                Cells(i, 2).Formula = "=COUNTIF(Input!" & Cells(19, i + 1).Address & ":" & Cells(5000, i + 1).Address & ",""A"")"
         
                i = i + 1
            
        Next Tru
     End Sub