0

I am pretty new to VBA coding and currently I am working with several files that I merge into one workbook. I want to apply the formulas to every worksheet all together since they also belong in the same cell. When I try to run the code I currently have it is giving me this problem: here

I am not sure why it is adding: < ' > to the cells I'm indicating and therefore not letting the formulas to run smoothly. This is the code I have at the moment:

Sub InstacartOld()
    Sheets.Select
    Range("AD805").Activate
    ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,(AD2:AD803))"

    Sheets.Select
    Range("AD806").Activate
    ActiveCell.FormulaR1C1 = "=SUMIF(AE2:AE803," + ",AD2:AD803)/100"

    Sheets.Select
    Range("AD807").Activate
    ActiveCell.FormulaR1C1 = "=SUMIF(AE2:AE803," - ",AD2:AD803)/100"

    Sheets.Select
    Range("AG805").Activate
    ActiveCell.FormulaR1C1 = "=SUM(AG2:AG803)"

    Sheets.Select
    Range("AH805").Activate
    ActiveCell.FormulaR1C1 = "=AG805/10000"

    Sheets.Select
    Range("AH808").Activate
    ActiveCell.FormulaR1C1 = "=AD806-AD807-AH805"

End Sub

Please any help is appreciated! hank you!

3
  • You're using A1-style cell references, so use .Formula not .FormulaR1C1 Commented Oct 29, 2024 at 22:05
  • 1
    Range("AD805").Formula = "=SUBTOTAL(9,(AD2:AD803))" only needs one line- skip the Activate Commented Oct 29, 2024 at 22:07
  • 1
    In this way, you can only enter the formula to one worksheet at a time. For many you need a loop. With ActiveCell you can fill all sheets immediately without loops. Only multiple sheet selection is not needed. Commented Oct 29, 2024 at 23:05

2 Answers 2

3

You need to use the Formula property instead of FormulaR1C1 since you use A1-style:

.Formula2 = "=SUBTOTAL(9,(AD2:AD803))"

The next epression is wrong because "-" is not supported operation for strings:

"=SUMIF(AE2:AE803," - ",AD2:AD803)/100"

"need to sum separately depending if there is a 'negative' sign or a 'positive' sign on column AD":

[AD806].Formula2 = "=SUM((AD2:AD803=""+"")*AE3:AE803)/100"
[AD807].Formula2 = "=SUM((AD2:AD803=""-"")*AE3:AE803)/100"

Credits to @MGonet: look at his comment below for another interpretation of requirements and corresponding formulas.

IMPORTANT Use Formula2 property.

Credits to @MGonet: If you have similar sheets and need to enter similar formulas, do the following:

Sheets(Array("Sheet1", "Sheet2")).Select
[AD806].Activate
ActiveCell.Formula2 = "=SUM((AD2:AD803=""+"")*AE3:AE803)/100"
[AD807].Activate
ActiveCell.Formula2 = "=SUM((AD2:AD803=""-"")*AE3:AE803)/100"

Explanation/instruction:

  1. Select all similar sheets including them in Array.
  2. Activate the required cell.
  3. Set formula to the active cell.

As result of the code above, AD806 on all selected sheets will get the same formula, AD807 as well.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for this information! i need to sum separately depending if there is a 'negative' sign or a 'positive' sign on column AD. how can i correct the second formula to sum the negative amounts?
I understood it differently. But want to sum an AD or AE column? If AD then "=SUMIF(AD2:AD803,"">0"")/100" for positive values and "=SUMIF(AD2:AD803,""<0"")/100" for negative values. If sum of AD in dependence of a sign in AE then "=SUMIF(AE2:AE803,"">0"", AD2:AD803)/100" or <0 accordingly.
@MGonet There are two columns mentioned in "=SUMIF(AE2:AE803," + ",AD2:AD803)/100". Since OP writes "'negative' sign or a 'positive' sign on column AD" the only column to sum from those of two is AE. Anyway, OP will correct or ask for correction if any. We can suppose what any we can having no data to look at.
@MGonet This trick with ActiveCell and multiple selected sheets is new for me. But I did it manually accidently by mistake, ha-ha.
Interesting: If you have charts in the workbook, one will assume that instead of Sheets.Select, Worksheets.Select must be used. But even that is not true: although Sheets.Select will select all the sheets (including charts), the operations will still work without an error.
|
2

You should try to avoid activating and/or selecting cells:

   Sub InstacartOld()
    
        Dim ws As Worksheet
        
        For Each ws In ThisWorkbook.Worksheets
            ws.Range("AD805").Formula = "=SUBTOTAL(9,AD2:AD803)"
            'the rest goes here
        Next ws

    End Sub

1 Comment

But in this particular case, using ActiveCell, you can enter formulas into all sheets at once without loop. I didn't check which is faster with a large number of sheets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.