Need help on code to copy formula from one cell to other cells in the row on a dynamic table

roosechua

New Member
Joined
Sep 25, 2019
Messages
1
Hi All,

I am still learning VBA and currently, I am stuck on how I can copy formula from cell(1,1) on a table to new rows when the table expands (or new columns are added). Here's an example of my setup:

Table1: PricesPerFruit
FruitPrice
apple1
pineapple2
grapes3

<tbody>
</tbody>

Table2: ItemsSoldperWeek (Initially 1 column)
Wk1
5
10
4

<tbody>
</tbody>

Table3: SalesPerWeek (Initially 1 column)
Wk1
$5
$20
$12

<tbody>
</tbody>

The formula in cell(1,1) on the SalesPerWeek table is
Code:
=PricesPerFruit[[Price]:[Price]]*ItemsSoldPerWeek[@Wk1]
.

However, when I try to add new column for the new week, the formula is not copied/autofilled on the next columns. I am trying to populate these new columns and my code so far:

Code:
Sub CopyFormulaToNewColumns()    Dim ws As Worksheet
    Dim tblFruits As ListObject
    Dim tblQuantity As ListObject
    Dim tblSales As ListObject
    Dim lc As Long
        
    Set ws = Worksheets("Sheet3")
    Set tblFruits = ws.ListObjects("PricesPerFruit")
    Set tblQuantity = ws.ListObjects("ItemsSoldperWeek")
    Set tblSales = ws.ListObjects("SalesPerWeek")


    lc = tblSales.Range.Columns.Count
    
    For counter = 2 To lc
        tblSales.DataBodyRange.Cells(1, 1).Select
        Selection.AutoFill Destination:=tblSales.DataBodyRange.Cells(1, counter), Type:=xlFillDefault
    Next
    
End Sub

Unfortunately, when I run this code, it gives me the error message "Run-time error '1004': AutoFill method of Range class failed".
Adding rows has no problem, as somehow, the new row is autofilled.

Anyone whom can help to give me a hint on how to do this? My full code actually creates a number of new columns at once, and I need to make sure that the formulas are set on these new columns. Thanks in advance for the help!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
use the Formula property of a range:
Code:
[COLOR=#333333]tblSales.DataBodyRange.Cells(1, counter).formula = [/COLOR][COLOR=#333333]tblSales.DataBodyRange.Cells(1, 1).formula[/COLOR][COLOR=#333333]
[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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