Using a set variable in a Vlookup - excel

I'm trying to use the following formula in a cell where Name is a defined cell. When I run 'MsgBox Name.Address' it gives me the correct cell, but when I run the formula I get
run-time error '1004'
Worksheets("E'ee Details").Range("N1").FormulaR1C1 = "=VLOOKUP(" & Name.Address & ",EmployeeBank!C[-13]:C[-10],4,FALSE)"
The code also correctly places in the value of the variable if I remove .address
Worksheets("E'ee Details").Range("N1").FormulaR1C1 = "=VLOOKUP(" & Name & ",EmployeeBank!C[-13]:C[-10],4,FALSE)"
This gives me =VLOOKUP(J Bob,EmployeeBank!A:D,4,FALSE) in the cell. This would work but I can't put quotation marks around the name as these are already in place in the vba code. I tried doing multiple quotation marks and also char(34) but couldn't get it to work.

You need to use an additional double quotation.

Sorted it, I was putting in the wrong number of quotation marks!!!!
Worksheets("EeeDetails").Range("N1").FormulaR1C1 = "=vlookup(""" & NewName & """,EmployeeBank!C[-13]:C[-10],4,FALSE)"

Related

VBA code is adding unwanted ' in my formula and returning wrong values [duplicate]

I am trying to write this formula using VBA:
ActiveCell.Value = "=f(R[-1]C[0],Sheet1!" & ColumnLetter & i & ")"
Where ColumnLetter is some variable letter which my macro computes earlier, and f is some function, and i is some number.
The problem is that when I run this, the cell is given this instead: (if ColumnLetter = F, i = 16):
=f(R[-1]C[0],Sheet1!'F16')
but I want:
=f(R[-1]C[0],Sheet1!F16)
Why is VBA or Excel putting those single quotation marks around F16? It does not insert these extra quotation marks if I do not include R[-1][0] as an argument in my formula, but I need to include this.
Help much appreciated!
Its the combination of R1C1 and A1 addressing. You need to pick one method and use it for both parts.
Note that if you type =f(R[-1]C[0],Sheet1!F16) into a cell you will get an error for the same reason.
You say you need to use R1C1 style for the first address, but (assuming this is because you don't want
absolute address) you can use .Offset instead
ActiveCell.Value = "=f(" & ActiveCell.Offset(-1, 0).Address(False, False) _
& ",Sheet1!" & ColumnLetter & i & ")"
The Apostrophe means to Excel that it should interpret it as text.
Write it to ActiveCell.Formula. That way it is recognized as Formula.

Trying to add a Hyperlink Formula using VBA and a Variable

I know how to add a formula to a worksheet that my macro is looping through
ThisWorkbook.Sheets("Sheet1").Range("A" & i).Formula = "=1+1"
and I know how to set up a Hyperlink Formula if I know the sheet name
=HYPERLINK("#Sheet2!A1","Sheet2")
but im having some trouble combining the two,
I have the Sheet name stored in a Variable (Sheet_Name) and im trying to set up the Hyperlink Formula to contain that (So instead of #Sheet2 I want it to use the Sheet_Name variable instead)
I cant seem to get the syntax to work
.Formula = "=HYPERLINK(""#'" & sheet_Name & "'!A1"",""" & sheet_Name & """)"
If you need double qutoes within the result string, you need to double them.
It's sometimes tricky to find out, what's the right number of quotes ...
Furthermore you should put single quotes around the sheetname variable - as you need them if there is e.g.
a space in the name ...

Syntax for formula =SUM(range(cells(41,10),cells(49,10)))

The "formula" =SUM(range(cells(41,10), cells(49,10))) returns "#NAME"?
I need to create the formula with quotation and & and place it in certain cells.
Also the values in the Cells() function are variables in the code.
The example is a result of one placement of the formula at a particular cell. I've tried a : also to separate the cells(), also tried not using Range().
with Worksheets("Sheet1")
.Range("A1").Formula = "=SUM(" & .range(.cells(41,10),.cells(49,10)).Address & ")"
End with

Runtime Error 1004 On formula R1C1 vba

I have some trouble applying a FORMULAR1C1 to a range.
This is the code i been using:
Range("AE6:AE" & conter).FormulaR1C1 = "=VLOOKUP(RC[-28],'[" & namebook & "]" & namesheet & "'!C1:C4,4,FALSE)"
where Conter is the number of rows that will applying the formula, namebook is the name of the woorkbook (without path) and namesheet is the name of the sheet that are looking the info for the vlookup.
the reason that i used a variable is that the file update daily and change the name because the date.
When i run this i get the runtime error 1004 what can i solve this? thanks
It's better of you stick with one style of Range.
You want to use FormulaR1C1, and your first VLOOKUP parameter is RC[-28], but the second one you are using C1:C4.
Also, you have a few "extra" ' that needs to be removed.
Modify your code to the line below:
Range("AE6:AE" & conter).FormulaR1C1 = "=VLOOKUP(RC[-28],[" & namebook & "]" & namesheet & "!R1C3:R4C3,4,FALSE)"

VBA setting the formula for a cell

I'm trying to set the formula for a cell using a (dynamically created) sheet name and a fixed cell address. I'm using the following line but can't seem to get it working:
"=" & strProjectName & "!" & Cells(2, 7).Address
Any advice on why this isn't working or a prod in the right direction would be greatly appreciated.
Not sure what isn't working in your case, but the following code will put a formula into cell A1 that will retrieve the value in the cell G2.
strProjectName = "Sheet1"
Cells(1, 1).Formula = "=" & strProjectName & "!" & Cells(2, 7).Address
The workbook and worksheet that strProjectName references must exist at the time that this formula is placed. Excel will immediately try to evaluate the formula. You might be able to stop that from happening by turning off automatic recalculation until the workbook does exist.
Try:
.Formula = "='" & strProjectName & "'!" & Cells(2, 7).Address
If your worksheet name (strProjectName) has spaces, you need to include the single quotes in the formula string.
If this does not resolve it, please provide more information about the specific error or failure.
Update
In comments you indicate you're replacing spaces with underscores. Perhaps you are doing something like:
strProjectName = Replace(strProjectName," ", "_")
But if you're not also pushing that change to the Worksheet.Name property, you can expect these to happen:
The file browse dialog appears
The formula returns #REF error
The reason for both is that you are passing a reference to a worksheet that doesn't exist, which is why you get the #REF error. The file dialog is an attempt to let you correct that reference, by pointing to a file wherein that sheet name does exist. When you cancel out, the #REF error is expected.
So you need to do:
Worksheets(strProjectName).Name = Replace(strProjectName," ", "_")
strProjectName = Replace(strProjectName," ", "_")
Then, your formula should work.
If Cells(1, 1).Formula gives a 1004 error, like in my case, changes it to:
Cells(1, 1).FormulaLocal
If you want to make address directly, the worksheet must exist.
Turning off automatic recalculation want help you :)
But... you can get value indirectly...
.FormulaR1C1 = "=INDIRECT(ADDRESS(2,7,1,0,""" & strProjectName & """),FALSE)"
At the time formula is inserted it will return #REF error, because strProjectName sheet does not exist.
But after this worksheet appear Excel will calculate formula again and proper value will be shown.
Disadvantage: there will be no tracking, so if you move the cell or change worksheet name, the formula will not adjust to the changes as in the direct addressing.

Resources