Pasting formulas with VBA not working

excel12345u

New Member
Joined
Apr 17, 2023
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hi there,

I have VBA that needs to copy formulas when a new row was added

in VBA code that pasting the formula is like this

VBA Code:
        For i = lastRow + 1 To lastRow + count2 - count1
            criteriaSheet1.Range("C" & i).Formula = "=IFERROR(INDEX('Sheet2'!$F$3:$F$" & lastRow & ",MATCH(0,IF('Sheet 2'!$C$3:$C$" & lastRow & "<>3,1,IF('Sheet 2'!$A$3:$A$" & lastRow & "<>B" & i & ",1,COUNTIF($C$12:C" & i - 1 & ",'Sheet 2'!$F$3:$F$" & lastRow & ")))),0),"""")"
        Next i

Pasted formula is not working because in the cell when I click I can see that there are additional @ signs, like this

Excel Formula:
=IFERROR(@INDEX('Sheet 2'!$F$3:$F$84,MATCH(0,IF(@'Sheet 2'!$C$3:$C$84<>3,1,IF(@'Sheet 2'!$A$3:$A$84<>B86,1,COUNTIF($C$12:C85,@'Sheet 2'!$F$3:$F$84)))),0),"")

Why is this happening? How do I stop this from happening?

Thanks in advance!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi @excel12345u. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.


In the formula you have in the VBA code you are missing a space in the first sheet name:
1682200426626.png



Also I think it should be an array formula:
Rich (BB code):
  For i = lastRow + 1 To lastRow + count2 - count1
    criteriaSheet1.Range("C" & i).FormulaArray = _
      "=IFERROR(INDEX('Sheet 2'!$F$3:$F$" & lastRow & "," & _
          "MATCH(0,IF('Sheet 2'!$C$3:$C$" & lastRow & "<>3,1," & _
                  "IF('Sheet 2'!$A$3:$A$" & lastRow & "<>B" & i & ",1," & "COUNTIF($C$12:C" & i - 1 & "," & _
                     "'Sheet 2'!$F$3:$F$" & lastRow & ")))),0),"""")"
  Next i

I purposely arranged the lines like this to make it easier to read the formula and not the entire formula on one line.

Also, that way you can see which parts of the formula are similar and you can simplify them, like this:
VBA Code:
  Dim colF As String, colC As String, colA As String
  
  For i = lastRow + 1 To lastRow + count2 - count1
    With Sheets("Sheet 2").Range("F3:F" & lastRow)
      colF = .Address(, , xlA1, True)
      colC = .Offset(0, -3).Address(, , xlA1, True)
      colA = .Offset(0, -5).Address(, , xlA1, True)
    End With
  
    criteriaSheet1.Range("C" & i).FormulaArray = _
      "=IFERROR(INDEX(" & colF & "," & _
          "MATCH(0,IF(" & colC & "<>3,1," & _
                  "IF(" & colA & "<>B" & i & ",1," & "COUNTIF($C$12:C" & i - 1 & "," & _
                     "" & colF & ")))),0),"""")"
  Next i

Or this way:
VBA Code:
  For i = lastRow + 1 To lastRow + count2 - count1
    criteriaSheet1.Range("C" & i).FormulaArray = _
      Replace("=IFERROR(INDEX('Sheet 2'!$F$3:$F$#," & _
          "MATCH(0,IF('Sheet 2'!$C$3:$C$#<>3,1," & _
                  "IF('Sheet 2'!$A$3:$A$#<>B" & i & ",1," & "COUNTIF($C$12:C" & i - 1 & "," & _
                     "'Sheet 2'!$F$3:$F$#)))),0),"""")", "#", "" & lastRow & "")
  Next i


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
@excel12345u Try using
VBA Code:
criteriaSheet1.Range("C" & i).Formula2
rather than
VBA Code:
criteriaSheet1.Range("C" & i).Formula
You do need to look at your sheet names though as @DanteAmor has suggested
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top