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)"
Related
I have a formula in I2:
= "=" & "'" & $H2 & $I2 & E2 & "'!" & "$C$2"
It combines the directory, filename, sheet name, and cell location.
Now I'd like to evaluate the result of that formula in J2. Is it possible to do so without VBA or with just a bit of VBA? I need to be able to fill down I2 to I1000 and I personally prefer a non-VBA approach.
For example, let's say the result of I2 is:
='R:\20180220\[Test.xlsb]'!$C$2
Is there a way to actually show the value of that cell in J2? Note that workbook Test.xlsb is closed and won't be opened (because I have thousands of workbooks and cannot open all of them).
I tried EVALUATE(Range.Formula) and ExecuteExcel4Macro() in VBA, and tried to wrap them with a UDF, but both failed.
Here's a kludge that may work for you.
Create the formulas as in your current post. Note that the result in your post lacks a reference to a tab/worksheet --- you likely need to update your formula to include a worksheet. The formula I used in my tests is = "=" & "'" & $PATH & $FILE & $TAB & "'!" & "$C$2" (the same as yours but with a field added for the $TAB).
Copy/paste special values the results. This replaces = "=" & "'" & $PATH & $FILE & $TAB & "'!" & "$C$2" with ='C:\PATH\WORKBOOK\TAB!$C$2 to give the correct formulas. Annoyingly they do not calculate automatically.
To force an update of the calcs, F9 doesn't seem to work, so select the results and do a mock find/replace, say replace $C$2 with $C$2. This doesn't change the formulas but gets the recalc going. You might also be able to just save, close and re-open the workbook.
Hope this helps.
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)"
I am inserting the following hyperlink as a formula using vba:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\" & Range("C" & ActiveCell.Row).Value & "\log.txt"",""View Log"")"
This works fine, however if my value in cell C was to change then my hyperlink becomes invalid and won't work because it has retained the value of the cell at the time in which the formula was entered.
I need a way of making a dynamic reference to my cell C in the event that the value should change then my link will also change.
Can someone please show me how to do this? Thanks in advance
Your code is taking the value from column C and building a string using that value that looks like this:
"S:\Tasks\FolderName\log.txt"
Instead, what you want to do is build the following string:
"S:\Tasks\" & C2 & "\log.txt"
To do that, use this VBA code:
ActiveSheet.Range("AD" & ActiveCell.Row).Formula = "=HYPERLINK(""S:\Tasks\"" & C" & ActiveCell.Row() & " & ""\log.txt"",""View Log"")"
I've been looking at this code for a while and can't seem to find what I'm doing wrong. It must be one of those obvious solutions, but I'm stumped.
The line of code that's causing me trouble is this:
Range("M2:M5759).FormulaR1C1 = "=if(isna(vlookup(RC[-12], " & Path & "[Budget " & Month & ".xlsx]Budget_" & Month & "'!RC[-12]:R[LastR]C[-12], 1, false)), ""NO"", ""YES"")"
The variable Path contains the location of the file I'm looking at.
The name of the file is Budget 0615.xlsx, and the Tab is named Budget_0615, but the date has to be changeable, so it is assigned to the variable Month.
The variable LastR contains the number of rows in the file.
My code is trying to return the value "NO" if the value in column A can't be found in the file Budget 0615.xlsx, and return "YES" if it can.
When I run the Macro, I get the Run-time error '1004': Application-defined or object-defined error.
Can someone help? If you need more info or if I'm not being clear, just ask.
Thanks!
You appear to be missing a ' before the Path in the formula, and LastR needs to be concatenated into the formula string as an absolute, not relative, number:
Range("M2:M5759).FormulaR1C1 = "=if(isna(vlookup(RC[-12],'" & Path & "[Budget " & Month & ".xlsx]Budget_" & Month & "'!RC[-12]:R" & LastR & "C[-12], 1, false)), ""NO"", ""YES"")"
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.