Error 1004 on formula - vba

When I run this code, I repeatedly am getting error 1004
with activecell
.Formula = "=CONCATENATE(" & ActiveCell.Offset(0, -2).Address & "," _
& ":" & "," & ActiveCell.Offset(0, -1).Address & ")"
end with
Does anyone have an idea of where the issue is presenting itself?

You want
"=CONCATENATE(" & ActiveCell.Offset(0, -2).Address & ","":""," & ActiveCell.Offset(0, -1).Address & ")"
Because your current formula evaluates to (with F4 selected)
=CONCATENATE($D$4,:,$E$4)
Which is wrong hence the error.
Try
Option Explicit
Public Sub test()
With ActiveCell
.Formula = "=CONCATENATE(" & .Offset(0, -2).Address & ","":""," & .Offset(0, -1).Address & ")"
End With
End Sub
That has a formula which evaluates to
=CONCATENATE($D$4,":",$E$4)
This adds the missing "" surrounding your :

Related

VBA Adding a button that adds a row that copies the formulas in a certain from above the activecell, not working

I want to make a button that copies a certain formulas within a range, and inserts it below the activecell.
It works, but now the formula reference keeps linking to the formula above it, I need it to act like an AutoFill,
Sub Rijen_Toevoegen()
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown
Range("B" & ActiveCell.Row + 1 & ":H" & ActiveCell.Row + 1).Formula = Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row).Formula
End Sub
You can use autofill like this:
Sub Rijen_Toevoegen()
ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown
Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row).AutoFill Destination:=Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row + 1)
End Sub
Look that the destination range must include the ActiveCell's row "B" & ActiveCell.Row and also the row you are filling ":H" & ActiveCell.Row + 1 (plus 1)
Let me know if it works
Offset should be used like this:
currentSheet.yourRange.Offset(offsetRow, offsetColumn)
With that, you should change your code to:
Sub Rijen_Toevoegen()
ActiveCell.Offset(1, 0).EntireRow.Insert Shift:=xlDown
Range("B" & ActiveCell.Row + 1 & ":H" & ActiveCell.Row + 1).Formula = Range("B" & ActiveCell.Row & ":H" & ActiveCell.Row).Formula
End Sub
Also, it is recommended to avoid using .Select, .Active, ActiveCell and the likes.

Inserting formula using VBA in Excel does not work

First of all, I have tried to look at all the other examples of adding a formula using VBA, and I think I have tried to apply all the answers in this code
Sub AddFormulas(SheetName As String)
Dim i As Integer
'Switch worksheet
Set Wksht = ThisWorkbook.Worksheets(SheetName)
Wksht.Activate
Application.Calculation = xlCalculationManual
i = 2
While Not IsEmpty(Cells(i, 1))
Wksht.Cells(i, 18).Formula = "=IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")"
i = i + 1
Wend
Application.Calculation = xlCalculationAutomatic
End Sub
But still, it gives me this anoying error, that I can't interpret
If I change my line to
Wksht.Cells(i, 18) = "'IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")"
Then I get no error, and the correct formula is added, although as a text string
What is wrong, since it would not add what to me looks like a valid formula?
//V
The Formula property requires formulas to be written in English, i.e. English function names (not an issue here) and commas as separators rather than semi-colons.
So your statement should be:
Wksht.Cells(i, 18).Formula = "=IFERROR(VLOOKUP(A" & i & "," & Wksht.Cells(1, 18) & "!$A:$A,1,FALSE)," & Chr(34) & "MISSING" & Chr(34) & ")"
If you don't mind having "portability" issues, you could also use the FormulaLocal property, i.e.
Wksht.Cells(i, 18).FormulaLocal = "=IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")"
Write the formula as it should be in Excel, select it and run the following code:
Public Sub PrintMeUsefulFormula()
Dim strFormula As String
Dim strParenth As String
strParenth = """"
strFormula = Selection.Formula
strFormula = Replace(strFormula, """", """""")
strFormula = strParenth & strFormula & strParenth
Debug.Print strFormula
End Sub
It should print it as it should be.
I believe your code is giving error becouse of this Wksht.Cells(1, 18) part of line in this row Wksht.Cells(i, 18).Formula = "=IFERROR(VLOOKUP(A" & i & ";" & Wksht.Cells(1, 18) & "!$A:$A;1;FALSE);" & Chr(34) & "MISSING" & Chr(34) & ")". Make sure that Wksht.Cells(1, 18) really contains name of worksheet your are trying to address. If this cell is blank, you will receive previously mentioned error.

application.worksheetfunction.vlookup error 1004?

i have a debug in my code, but i cannot figure out why it is happening, could you please review the code and see where i messed up? Note the error debug is happening on the ActiveCell.FormulaR1C1 line.
'ENRICHMENT CODE FOR VARIOUS TITLES
For Each wbtitle In wbrange
sThisWorkTitle = wbtitle
sThisWorkColumnNum = wbtitle.Column
sThisWorkColumnNam = Split(Cells(, sThisWorkColumnNum).Address, "$")(1)
'identifying CASH RADICAL COLUMN LETTER
If sThisWorkTitle = "Account Cash Radical" Then
scashradicalcolumnnam = Split(Cells(, sThisWorkColumnNum).Address, "$")(1)
Else
'do nothing
End If
''' CASH RELATED?
If sThisWorkTitle = "Cash Related?" Then
wbtitle.Select
Range(sThisWorkColumnNam + gspstart).Select
ActiveCell.FormulaR1C1 = Application.WorksheetFunction.VLookup(Range(scashradicalcolumnnam & ActiveCell.Row), Range(scashradicalcolumnnam & immsstart & ":" & scashradicalcolumnnam & immsfinal), 1, False)
ActiveCell.AutoFill Range(ActiveCell.Address, Cells(gspfinal))
Columns(sThisWorkTitle).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Else
'do nothing
End If
Scott has highlighted the problem line.
You could try amending so an actual formula is inserted into the cell.
So instead of the following:
ActiveCell.FormulaR1C1 = _
Application.WorksheetFunction.VLookup( & _
Range(scashradicalcolumnnam & ActiveCell.Row) & _
, Range(scashradicalcolumnnam & immsstart & ":" & scashradicalcolumnnam & immsfinal), 1, False)
Something like:
ActiveCell = "=vlookup(" & scashradicalcolumnnam & ActiveCell.Row & _
"," & scashradicalcolumnnam & immsstart & ":" & _
scashradicalcolumnnam & immsfinal & _
", 1, False)"

Add sum to do a negative based on cell.offset vba

I am trying to add a formula to a selected cell if column A matches a selected criteria.
The only part I cannot get to work is the following bit of code.
cell.Offset(0, 7).Value = "=SUM(" & cell.Offset(0, 6) & ",-(" & cell.Offset(0, 8) & "," & cell.Offset(0, 5) & "))"
If I was to type the formula manually it would appear like this
=Sum(G2-I2)-F2
Its easy to add just that code but it wouldnt update the row number to the current row.
Any help would be apprecaited.
Thanks
Al
Is this what you are trying?
cell.Offset(0, 7).Formula = "=SUM(" & _
cell.Offset(0, 6).Address & _
"-" & _
cell.Offset(0, 8).Address & _
")-" & _
cell.Offset(0, 5).Address
Or this perhaps?
cell.Offset(0, 7).Formula = "=SUM(" & _
cell.Offset(0, 6).Address(RowAbsolute:=False, ColumnAbsolute:=False) & _
"-" & _
cell.Offset(0, 8).Address(RowAbsolute:=False, ColumnAbsolute:=False) & _
")-" & _
cell.Offset(0, 5).Address(RowAbsolute:=False, ColumnAbsolute:=False)

Insert VBA into formula formatting?

Really quick question on how to format VBA in excel formulas. When you are inserting a formula into excel and you want to insert a variable from vba for example if b is a string you would use " & b & " is that the correct formatting? To illustrate the problem I have the code below and tried to use that formatting and well... I don't know why it wont work, I get a (Compile error: Expected: End of statement). Can anyone tell me where I am going wrong?
Dim HrsSTD As String
Dim HrsSAT As String
Dim HrsSUN As String
Dim HrsSTWN As String
Dim sdFormula
HrsSTD = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSAT = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSUN = ActiveCell.Address
Selection.Offset(0, 1).Select
HrsSTWN = ActiveCell.Address
sdFormula = "=IF((" & Range(NamedRange).Cells(2, 1).Address & _
"=""Please add a title"",0,VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSTD & "+VLOOKUP(" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSAT & "*1.25+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*" & HrsSUN & "* _
1.5+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*" & HrsSTWN & "*0.75)"
The code I would type into excel would be: But I want to change the A13's and the I16 (i.e. all the relative references) into variables in VBA
=IF(A13="Please add a title",0,VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*F16+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*G16*1.25+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*H16*1.5+VLOOKUP(A13,'Tables (H)'!$H$2:$J$6,2,FALSE)*I16*0.75)
Is this what you are trying? Also I see that you haven't taken my advice from the previous answer.
One more tip. Break you code in simple parts. It is easier to understand.
The problem with your code is in the line
",'Tables (H)'!$H$2:$J$6,2,FALSE)* _
" & HrsSAT & "*1.25+VLOOKUP((" & Range(NamedRange).Cells(2, 1).Address & _
You can't write it like that. The first line doesn't have the ending ". You cannot carry it forward to the next line like that.
is this what you are trying?
Dim sFormula As String
Dim sAddr As String
sAddr = Range(NamedRange).Cells(2, 1).Address
sFormula = "=IF(" & sAddr & _
"=""Please add a title"",0,VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*F16+VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*G16*1.25+VLOOKUP(" & _
",'Tables (H)'!$H$2:$J$6,2,FALSE)*H16*1.5+VLOOKUP(" & _
sAddr & ",'Tables (H)'!$H$2:$J$6,2,FALSE)*I16*0.75)"