VBA - add formula to cell when created

Golaidron

Board Regular
Joined
Jan 23, 2013
Messages
83
Office Version
  1. 365
Platform
  1. Windows
Hi guy's/guru's

I got have
- 1 Template sheet
- 1 Datasheet
- Named ranges x 12:
- product_January to product_December
- Tables x 12
- tblJanuary to tblDecember
- 1 Helpsheet
- Named ranges:
- Companies
- Contactperson
- Type_Change
- Tables
- Customers
- 1 Productsheet
- Named ranges:
- Ctrl_Product
Range = A3:A57
- Ctrl_Type
Range = L2:Q2
- 1 Macro

This macro
VBA Code:
    Dim monthNames As Variant
    Dim templateSheet As Worksheet
    Dim newSheet As Worksheet
    Dim i As Integer
    Dim j AS Integer
   
    ' Legg til månedsnavn i array
    monthNames = Array("January", "February", "March", "April", "May", "June", _
                    "July", "August", "September", "October", "November", "December")
   
    ' Referer til templateSheetet
    Set templateSheet = Sheets("Template")
   
    ' Loop gjennom each month and create sheet
    For i = LBound(monthNames) To UBound(monthNames)
        'Copies Template to a new sheet
        templateSheet.Copy After:=Sheets(Sheets.Count)
        ' Set the reference to the new sheet
        Set newSheet = ActiveSheet
        ' Rename the new sheet with the month name
        newSheet.Name = monthNames(i)
       
        ' Set Datavalidation in B2:B1000 to named range product_monthNames
        With newSheet.Range("B2:B1000").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=product_" & monthNames(i)
        End With
       
        ' Set Datavalidation i C2:C1000 list to named range type_change
        With newSheet.Range("C2:C1000").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=type_Change"
        End With
       
        ' Set Datavalidation i E2:E1000 list to named range Companies
        With newSheet.Range("E2:E1000").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=Companies"
        End With
Next I
End Sub

I also want to add a formula in D2:D1000, F2:F1000 and G2:G1000, and here is where i really need you guys.

D2:D1000 - Formula
In sheet January!D2
=IFError(Index(tblJanuary[[Import]:[Provision]],Match(January!B2,tblJanuary[Product - January],0),Match(January!C2,tblJanuary[[#Headline],[Import]:[Provision]],0)),"")
In January!D3
=IFError(Index(tblJanuary[[Import]:[Provision]],Match(January!B3,tblJanuary[Product - January],0),Match(January!C3,tblJanuary[[#Headline],[Import]:[Provision]],0)),"")
and so on
I would this forumal to be added to D2:D1000 in the new sheet when created.
Is it possible to do something like this?
"=IFError(Index(tbl"&monthName&"[[Import]:[Provision]],Match("&monthName&"!B"&j&",tbl"&monthName&"[Product - &"monthName"&],0),Match("&monthName&"!C"&j&",tbl"&monthName&"[[#Headline],[Import]:[Provision]],0)),"")"

F2:F1000
In January!F2
=IFError(Index(tblCustomer[Contactperson],Match(Januar!E2,tblCustomer[Company],0)),"")
I would this forumal to be added to F2:F1000 in the new sheet when created.
Is it possible to do something like this?
"=IFError(Index(tblCustomer[Contactperson],Match("&monthName&"!E"&j&",tblCustomer[Company],0)),"")"

G2:G1000
In January!G2
=IFError(Index(Product!$L$3:$Q$57,Match(January!B2,Ctrl_Product,0),Match(January!C2,Ctrl_Type,0)),"")
I would this forumal to be added to F2:F1000 in the new sheet when created.
Is it possible to do something like this?
"=IFError(Index(Product!$L$3:$Q$57,Match(&monthName&"!B"&j&",Ctrl_Product,0),Match("&monthName&"!C"&j&",Ctrl_Type,0)),"")"

Hope to hear from you soon.

Best regards
Golaidron
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time.
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time.
Thank you Peter.
 
Upvote 0

Forum statistics

Threads
1,223,713
Messages
6,174,043
Members
452,542
Latest member
Bricklin

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