Insert formula from VBA - vba

I'm attempting to have the following code insert the formula into the changed cell. I'm not receiving any errors but the code is not populating on the worksheet. Any insight into what I'm not doing?
formula_p1 = _
"=GETPIVOTDATA(""Max of " & [value_column_header] & ",Database!R1C14, & _
""Key"",& Key &)"
Debug.Print Cell.Address
Sheets("Cost Sheet").Cell.Formula = formula_p1
Thank you in advance.
Update
My current code is below I'm stuck on the concatenation in the final part. I'm not sure how to accomplish this in VBA. Also I need this to be a formula that will re evaluate on changes to the "Family" part that can be done on the worksheet. The change in "Family" is accomplished from a combo box selection.
formula_1 = _
"=GETPIVOTDATA(""Max of " & value_column_header & """,Database!$N$1,""Key""," & Concatenate(CurrentHFMFamily, G5, Left(B12, 4)) & ")"

This is the final result of what i was trying accomplish. It's not pretty but it's quick and stable.
=IF(INDIRECT(CONCATENATE("B",ROW()))=""," ",IFERROR(GETPIVOTDATA("Max of & _
"&INDIRECT((ADDRESS(11,COLUMN())))&"_ADJ",Database!R1C14,"Key",& _
LEFT(INDIRECT(CONCATENATE("B",SUM(ROW()-1))),4)&CurrentHFMFamily& & _
(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(5))),- & _
(MOD(COLUMN()+1,4))))),0))
Thanks to everyone for your ideas they helped in pointing me in the right direction.

Related

VBA sort function not working

The following is an excerpt from a larger section of VBA code. I'm having a problem sorting a list of values after I paste them from another sheet. The list of values are successfully pasted and are selected before the sorting should be happening. Also the sorting code which I'm using works fine if it's used seperately in a macro by itself. Any suggestions as to what may be going wrong here? Thanks
Dim ProductType As String
Dim ValueDate As String
ProductType = Range("I19").value
ValueDate = Range("I18").value
ActiveSheet.name = ProductType & " " & ValueDate
Sheets("SensitivityResults").Cells(6, 1).Resize(653).Copy
Sheets(ProductType & " " & ValueDate).Range("A3").PasteSpecial Paste:=xlValues
Selection.Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlGuess
selection might not be set properly, try with
Sheets(ProductType & " " & ValueDate).Range("A3:A656").Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlGuess

SUMIF formula workbook name is unknown

I have to read to use SUMIF to check and compare the supplier number from different workbooks and if it is same then copy the Prices(using SUMIF). Everytime time the workbook can be different from which I take the prices but the sheet names and their layout will be same.So how can I write the formula in SUMIF? Can anyone help me please?
I'm stuck with this code since 2 days but couldn't figure out whats wrong.
Windows(wb_name).Activate
Range("AW18", Range("AW18").Offset(0, -44).End(xlDown).Offset(0, 44)).Formula = _
"=SUMIF('[" & dest_name & "]" & "!" & "Cu Part PVO L",$M$10:$M$2000,C19, _
"[" & dest_name & "]" & "Cu Part PVO L" & "'" & "!",$AD$10:$AD$2000)"
It looks like you have the exclamation mark in the wrong place, as well as also having too many commas.
Range("AW18", Range("AW18").Offset(0, -44).End(xlDown).Offset(0, 44)).Formula = _
"=SUMIF('[" & dest_name & "]Cu Part PVO L'!$M$10:$M$2000,C19," & _
"'[" & dest_name & "]Cu Part PVO L'!$AD$10:$AD$2000)"

Insert formula with reference to active cell row

I am trying to insert a formula into a cell and trying to use a dynamic cell reference by using ActiveCell.Row.
Range("Q" & ActiveCell.Row).Formula = "=IF(Range(""P"" & ActiveCell.Row).Address ="""",""DD/MM/YYY"",CONCATENATE(NETWORKDAYS(O21,P21),"" Working Days""))"
But I am getting an application defined error.
Please can someone show me where I am going wrong? Thanks
Range("Q" & ActiveCell.Row).Formula = "=IF(" & Range("P" & ActiveCell.Row).Address & " ="""",""DD/MM/YYY"",CONCATENATE(NETWORKDAYS(O21,P21),"" Working Days""))"
You have to comment out the VBA code in the string.

VBA Set Cell value to text containing ' not working

I'm trying to get a macro to type out a heap of formulae for me. The formula goes something like this:
=COUNTIF('other_sheet'!A:A,"hello*")
The user can specify A and other_sheet, so I select the cell and then go
ActiveCell.Value = "=COUNTIF('" & othercell & "'!" & column & ":" & _
column & """,hello*"")"
But it keeps giving me errors like:
1004: Object Defined Error
I have tried using ActiveCell.Text, ActiveCell.Formula etc. and they all don't work.
It needs to be ActiveCell.Formula and ", is the wrong way around next to Hello. It should be:
ActiveCell.Formula= "=COUNTIF('" & othercell & "'!" & column _
& ":" & column & ",""hello*"")"
Apparently you can only put proper, completed answers into a cell.
i.e. if the output turns out to be "=sum(A:A" and a bracket isn't closed, then the application throws an error just as if you typed it in manually.

Run-time error '1004' when applying formula to Range.FormulaR1C1 in VBA

I'm trying to apply a big nested formula to a range using the code below. Basically, if the value in cell A of the active row exists in the column A of another workbook and if the cell in column E of the active row is not empty, I want the active cell to display the cells to display the value of the equivalent cell in a separate workbook.
This needs to be applied to several worksheets so I'm using the variables lrow (which is an int with the last row of the active worksheet in workbook#1) and tlrow (which is an int equal to the last row of the active worksheet in workbook#2). When I step through the sub, these variables both return the numbers I would expect them to.
Likewise, this is inside of a for loop so I also use Worksheets(i).Name where I is an int.
When I run the code, I get the run-time error "'1004': Application-defined or object-defined error".
I'm assuming it's a syntax issue.
Code:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & _
"'!A15:D" & tlrow & ",3,FALSE)),""0"",VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & "'!A15:D" & tlrow & ",3,FALSE))))"
Try using this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1," & _
Worksheets(i).Range("A1:D" & lrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)),""0"",VLOOKUP(RC1," & _
Worksheets(i)..Range("A1:D" & tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)))"
What version of Excel are you running? In more recent versions you can use the Iferror function in this formula to really chop down the size.
It would be something like this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IFERROR(VLOOKUP(RC1," & " & Worksheets(i).Range("A1:D" & _
tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & ",3,0),""0"")"
Thanks for your help. I was able to resolve the problem by defining my vlookup range in a Range variable and then inputting the variable name in L42's equation in place of
worksheets(i).Range("A1:D" & lrow)
Really apprecaite the responses! Thanks again.