vba formulaR1C1 concatenate from different columns with function - vba

I'm getting error for the following code: Application-defined or object-defined error.
How do I fix this?
p.s. lastRow is a number
Range("A2:" & "A" & lastRow).FormulaR1C1 = "=CONCATENATE(RC[6],RC[7],RC[9],RC[10],RC[11],ROUND(RC[12],0),IF(RC[13]="",0,RC[13]),ROUND(RC[14],2),YEAR(RC[15]),MONTH(RC[15]),DAY(RC[15]))"

Your "" need to be """"
Range("A2:" & "A" & 3).FormulaR1C1 = "=CONCATENATE(RC[6],RC[7],RC[9],RC[10],RC[11],ROUND(RC[12],0),IF(RC[13]="""",0,RC[13]),ROUND(RC[14],2),YEAR(RC[15]),MONTH(RC[15]),DAY(RC[15]))"

Related

VBA Run-time Error 1004 for Special Characters in Soft-coded Vlookup Function

When I was trying to throw a vertical bar "|" into my vlookup function, I got a 1004 error. I am not sure where it came from.
Initially the data table looks like this:
And my code was like this:
Sheets("Sheet 1").Cells(i, 2).Formula = "=VLOOKUP($A" & i & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Everything worked fine till the format of the data table got changed to:
So I had to throw the vertical bar "|" into my code. My new code was like this:
NewKey = Sheets("Sheet 1").Cells(i, 1).Value & "|" & Sheets("Data").Cells(i, 2).Value
Sheets("Sheet 1").Cells(i, 3).Formula = "=VLOOKUP(" & NewKey & ",Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"
Apparently Excel 2010 did not like my codes and threw me a 1004 error. I am wondering if anyone knows a solution to this. Thank you in advance for your help.
NewKey needs to be in quotes when the formula is placed on the sheet, so you need to include the quotes in the string:
NewKey = Sheets("Sheet1").Cells(i, 1).Value & "|" & Sheets("Sheet1").Cells(i, 2).Value
Sheets("Sheet1").Cells(i, 3).Formula = "=VLOOKUP(""" & NewKey & """,Data!$A$1:$B$" & LastRow & "," & ColmNum & ",0)"

"Type Mismatch"error when trying to use nested formula in vba

I am trying to calculate the value using below formula but getting an error message as "Type Mismatch"
Dim exp_comp As Integer
Sheets("Dashboard").Select
Range("M2").Select
Selection.Formula = "=DAYS(H2,TODAY())"
exp_comp = WorksheetFunction.Product(("H" & row / "L" & row), "M2")
Worksheets("Dashboard").Range("J" & row).Value = exp_comp
The division should be like this:
Range("H" & 1)/ Range("L" & 1)
1 is instead of the row, considering that it is defined somewhere.

Application-defined or object-defined error with formula VBA

I'm new to VBA and encounter the
application-defined or object-defined error
while doing it.
Set twb = ThisWorkbook
Set extwbk = Workbooks.Open("abc.xlsx")
Set x = extwbk.Worksheets("FILTERED").Range("$A:$A")
With twb.Sheets("FILTERED")
For rw = 9 To .Cells(Rows.Count, 1).End(xlUp).Row
.Cells(rw, 34).Formula = "=IF(ISERROR(VLOOKUP(.Cells(rw, 1).Value2, x, 1, False))=TRUE,""New"",""Old"")"
Next rw
End With
This can be done manually but I'm asked to do it by programming. It is simply apply the formula from AH9 to the end of the data. May I know is there any coding error in the for loop?
Thanks in advance.
Dont put VBA variables inside your string formula. Resolve them in VBA and inject the result into the formula:
.Formula = "=IF(ISERROR(VLOOKUP(" & .Cells(rw, 1).Value2 & "," & _
x.Address(External:=True) & ", 1, False))=TRUE,""New"",""Old"")"
p.s. dont use =TRUE to check for a boolean, things like If ISERROR(Something) = TRUE don't make sense. Just If ISERROR(Something).
.Formula = "=IF(ISERROR(VLOOKUP(" & .Cells(rw, 1).Value2 & "," & _
x.Address(External:=True) & ", 1, False)),""New"",""Old"")"
If you want the formula to be dynamic and recalculate accordingly when the cell at (rw, 1) changes in the future, use .Address instead of .Value2:
' vvvvvvvv
.Formula = "=IF(ISERROR(VLOOKUP(" & .Cells(rw, 1).Address & "," & _
x.Address(External:=True) & ", 1, False)),""New"",""Old"")"

VBA: Dims inside a Vlookup

I'm trying to use a Vlookup with the line:
wRange.FormulaR1C1 = "=VLOOKUP(RC[-" & (vCol - 1) & "],'" & vSheet & "'!C[15]:C[16],2,FALSE"
where vCol as Integer = 9 and vSheet as String = 082015
So basically, that line can be rewritten as:
wRange.FormulaR1C1 = "=VLOOKUP(RC[-8],'082015'!C[15]:C[16],2,FALSE)"
However, I'm getting an "Application-defined or object-defined error" during run-time.
Do you see what I'm doing wrong?
Thanks.
"=VLOOKUP(RC[-" & (vCol - 1) & "],'" & vSheet & "'!C[15]:C[16],2,FALSE)"
Note missing bracket at the end

Using string variables within a formula in VBA

I do not understand why this code doesn't work:
Cells(i, formula_col_index).Value = "=IF(" & time_location & "<>" & time_benchmark & ",""ERROR"",""OK"")"
where
time_location=" 17:00:00",
time_benchmark=" 17:30:00"
It keeps throwing application-defined (or object-defined) error.
Thanks in advance.
Since your variables time_location and time_benchmark contains string values, you should include them in double quotes when using formula:
Cells(i, formula_col_index).Value = "=IF(""" & time_location & """<>""" & time_benchmark & """,""ERROR"",""OK"")"