application.worksheetfunction.vlookup error 1004? - vba

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)"

Related

Error 1004 on formula

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 :

Creating a loop with data from other sheets

I'm trying to create a macro for a Personal Budget worksheet, where there is a cell with tedious work.
Using the "Record Macro" button I traced the coding behind the operation I made in the cell, which is the following:
Sub calculo_otherex()
' calculo_otherex Macro
' Cálculo de otros gastos con tarjeta.
'
ActiveCell.FormulaR1C1 = _
"=Extractos!R[-89]C[-2]-('Home Expenses'!R[-105]C+'Home Expenses'!R[-60]C+'Home Expenses'!R[-16]C+Health!R[-105]C+Health!R[-67]C+Health!R[-29]C+Gifts!R[-99]C+'Daily Living'!R[-87]C+'Daily Living'!R[-29]C+'Daily Living'!R[20]C+'Daily Living'!R[59]C+'Daily Living'!R[102]C+Entertainment!R[-105]C+Entertainment!R[-67]C+Entertainment!R[-29]C+Entertainment!R[9]C+Transportation!R[-105]C)"
Range("H129").Select
End Sub
As you can see, in the formula I refer to other sheets, and to specific cells which reflect a result of a certain month (in this case July) and a specific subset (charges to a credit card).
What I want to do is insert a loop so this procedure repeats for all the months, but the problem is that the data in the sheet "Extractos" appears every 7th cell to the right from "Extractos!R[-89]C[-2]", as the other data in the formula appears in the next column.
How could I solve this??
Thank you very much.
Give the following a try... It's hard to read R1C1, so I changed it up a bit and you'll need to replace all my "A1" references to the first month's cell reference. Also, the for loop will only loop 12 times - 12 months in a year. You can manually change this if you'd like. Lastly, the formula will be written in the activecell then the following formulas will be written in the next cell/column to the right.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'instead of using R1C1, use ranges with offsets - it's much easier to read :) ''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'I used "A1" as a placeholder. Replace this with the actual cell reference for the first month
'The first loop will use the "A1" range. Then, the 2nd and following loops will move over 1 or 7 columns
'i = move over 1 column
'x = move over 7 columns
For i = 0 To 11 'this will loop through 12 times 0-11
ActiveCell.Offset(0, i).Formula = _
"=Extractos!" & Range("A1").Offset(0, x).Address _
& "-('Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+'Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+'Home Expenses'!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Health!" & Range("A1").Offset(0, i).Address _
& "+Gifts!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+'Daily Living'!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Entertainment!" & Range("A1").Offset(0, i).Address _
& "+Transportation!" & Range("A1").Offset(0, i).Address & ")"
'add 7 to x variable
x = x + 7
'loop and add 1 to i
Next i
'macro done, select H129
Range("H129").Select

VBA to create formula based on another variable

I am trying to create the formula below using vba in excel
=SUM(COUNTIF(E7,"Vac")+ COUNTIF(E7,"LWOP")+COUNTIF(R7,"Vac")+ COUNTIF(R7,"LWOP"))
But E7 and R7 will change based on another variable called rCell.address
Below is the code that I have within the macro and it is giving an error:
Range("a8").Formula = "=SUMIF(countif(" & rCell.Address & ", "VAC"" & "))"
The current macro is:
Sub Find()
Dim strdate As String
Dim rCell As Range
Dim lReply As Long
With Worksheets("Sheet1")
strdate = .Range("a1").Value
End With
If strdate = "False" Then Exit Sub
strdate = Format(strdate, "Short Date")
On Error Resume Next
Set rCell = Cells.Find(What:=CDate(strdate), After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
On Error GoTo 0
If rCell Is Nothing Then
lReply = MsgBox("Date cannot be found. Try Again", vbYesNo)
If lReply = vbYes Then Run "FindDate":
End If
Range("a8").Formula = "=SUMIF(countif(" & rCell.Address & ",VAC" & "))"
End Sub
I am assuming that your error appears on this line of code: Range("a8").Formula = "=SUMIF(countif(" & rCell.Address & ",VAC" & "))".
You should change that line to
Range("a8").Formula = "=SUM(COUNTIF(" & rCell.Address & "," & Chr(34) & "Vac" & Chr(34) & ")+ COUNTIF(" & rCell.Address & "," & Chr(34) & "LWOP" & Chr(34) & ")+COUNTIF(" & rCell.Offset(0, 13).Address & "," & Chr(34) & "Vac" & Chr(34) & ")+ COUNTIF(" & rCell.Offset(0, 13).Address & "," & Chr(34) & "LWOP" & Chr(34) & "))"
Updated with better answer thanks to KS Sheon
Range("a8").Formula = "=SUM(COUNTIF(" & rCell.Address & ",""Vac"")+ COUNTIF(" & rCell.Address & ",""LWOP"")+COUNTIF(" & rCell.Offset(0, 13).Address & ",""Vac"")+ COUNTIF(" & rCell.Offset(0, 13).Address & ",""LWOP""))"
SUMIF is wrong in this context.
Range("a8").Formula = "=SUMIF(countif(" & rCell.Address & ",VAC" & "))"
you should use SUMIF like this
Range("a8").Formula =SUMIF(B2:B25,">5")

VBA - Boolean value is converted to a string

I'm trying to add the following formula dynamical to a cell in Excel.
=HVIS(ER.FEJL(LOPSLAG(B" & ActiveCell.row & ",VareKategorier,2,FALSK)),"""",LOPSLAG(B" & ActiveCell.row & ",VareKategorier,2,FALSK))
becomes
=HVIS(ER.FEJL(LOPSLAG(B3;VareKategorier;2;'FALSK'));"";LOPSLAG(B3;VareKategorier;2;'FALSK'))
but as you can see the boolean value FALSK (danish word for FALSE) is converted to a string 'FALSK'.
Why is it doing this and how can I fix my formula to get it working?
Complete code
For Each raekke In .Range("A3", "E" & celle).Rows
raekke.Select
Dim formulaKat As String
Dim formulaUKat As String
If ekstra = True Then
formulaKat = "=HVIS(ER.FEJL(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierIgloMolok,2,FALSK)),"""",HVIS(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierGenbrug,2,FALSK)=0,"""",LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierIgloMolok,2,FALSK)))"
Range("D" & ActiveCell.row).Formula = formulaKat
formulaUKat = "=HVIS(ER.FEJL(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierIgloMolok,3,FALSK)),"""",HVIS(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierIgloMolok,3,FALSK)=0,"""",LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierIgloMolok,3,FALSK)))"
Range("E" & ActiveCell.row).Formula = formulaUKat
Else
formulaKat = "=HVIS(ER.FEJL(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierGenbrug,2,FALSK)),"""",HVIS(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierGenbrug,2,FALSK)=0,"""",LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierGenbrug,2,FALSK)))"
Range("D" & ActiveCell.row).Formula = formulaKat
formulaUKat = "=HVIS(ER.FEJL(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierGenbrug,3,FALSK)),"""",HVIS(LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierGenbrug,3,FALSK)=0,"""",LOPSLAG(B" & _
ActiveCell.row & _
",VareKategorierGenbrug,3,FALSK)))"
Range("E" & ActiveCell.row).Formula = formulaUKat
End If
Next
This works for me
sformula = "=HVIS(ER.FEJL(LOPSLAG(B" & _
ActiveCell.Row & _
",VareKategorier,2,FALSK)),"""",LOPSLAG(B" & _
ActiveCell.Row & _
",VareKategorier,2,FALSK))"
Range("A1").Formula = sformula

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)"