FormulaR1C1 from another WorkBook - excel

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

Related

Concatenate date, text and cell value

I am trying to use VBA to create a description that includes the Month/Year, text, and the value of a cell. I have tried two methods and neither one is working for me.
First i tried this:
ActiveCell.FormulaR1C1 = _
"=CONCATENATE(Format(DateAdd("m", -1, now), "mmmyy"), " My Text ",RC[-19])"
Which gives a
run-time error 1004.
Next I tried
Range("X10").Value = Format(DateAdd("m",-1,now), "mmmyy") & " My Text " & E10
Which output the date and the text but not the value of the cell.
I'm not sure where I'm going wrong with this. Searching on here got me to this point, but I'm stuck.
Thanks for any help.
'Let: X be your Workbook index, Y be your sheet index, YY be your row index, XX be your column index
Sub macro2()
MsgBox Month(Date) & "/" & Year(Date) & " " & Workbooks(X).Sheets(Y).Cells(YY, XX).Value
End Sub
All of these may be stored into a string variable for portability across your project.
Good luck!.

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

Can't get VLookup to work with another Excel file

I am trying to place data from one excel file to another, using VBA's VLookup, but without success.
No error is shown and nothing happens. My cells are not being populated although after verification, they should. I cannot debug more than I have, as everything seems to be okay concerning the path, sheet name, and cell rows. I am trying to fetch a value from the source file (SourceFile.xlsx) after a key match on Column B (which contains a unique number).
I have tried the following code with and without the source file being opened, with exact same results:
If Application.WorksheetFunction.VLookup(.Range("B" & cell.Row), Workbooks("C:\Users\halpsb\Desktop\projet_macro\SourceFile.xlsx").Sheets("Projects_2015").Range("B" & cell.Row), 9, False) = "Yes" Then
.Range("D" & cell.Row) = Application.WorksheetFunction.VLookup(.Range("B" & cell.Row), Workbooks("C:\Users\halpsb\Desktop\projet_macro\SourceFile.xlsx").Sheets("Projects_2015").Range("B" & cell.Row), 7, False)
Else
.Range("D" & cell.Row) = ""
End If
Thank you in advance for any kind of help.
Edit: As precised in a comment below, the 7th column is used for my test and contains either "Yes" or "No" strings in the source file. Depending on this value, the 9th column of the source file is used for fetching data and placing it in my original file.
Your Vlookup formulas contain a "7" and a "9" as they are trying to retrieve data from the 7th and 9th column in the range specified by the second parameter.
BUT, you 2nd parameter only contains one column, which will cause an error
you could add error handlin similar to the example below
Sub aa()
On Error Resume Next
Debug.Print Application.WorksheetFunction.VLookup(2, Sheet1.Range("E4:E10"), 3, False)
if err.number <> 0 then
Debug.Print Err.Number, Err.Description
' an error occurred!
end if
End Sub
Consider this. The 2nd parameter is a single column! The 9 is trying to get the 9th column!
If Application.WorksheetFunction.VLookup(
.Range("B" & cell.Row)
, Workbooks("C:\Users\halpsb\Desktop\projet_macro\SourceFile.xlsx").Sheets("Projects_2015").Range("B" & cell.Row)
, 9
, False) = "Yes"

Find last filled row without opening file and with conditions

What I wish to do :
i. Locate an excel document using path and doc name √
ii. Obtain the last filled row’s number with a spreadsheet name without opening the file X
iii. Obtain the last filled row's number BEFORE a certain row number, X
ex: last filled row before row 40. There may be filled cells between 45-52,
but I would like the function to return say "32" and not "52" like it does for me
What I have:
The path to the file is a classic directory + file name, which are written in two separate cells in the spreadsheet that has the macro. It's irrelevant, just to show that File_Path = the path ...
File_Path = Chr(34) & Server & "\" & Range(Workbook_Loc).Value 'That's step one
In the file located at File_Path, I wish to go in the spreadsheet named MONTH YEAR (ex: File_Path("July 2015")) and find the last filled row's number. In the 3rd line, I try to fill a cell with the last row's value in B. I know there is a lot online for this, but I can't get it to work somehow...:
Set wb = Workbooks.Open(File_Path) ' This should open my workbook
lastRow = wb(Chr(34) & Month & " " & Year & Chr(34)).Cells(1000, col).End(xlUp).Row
Range(CurrentValue_Loc).Formula = "=INDEX('" & Server & "\" & Workbook & Month & " " & Year & "'!B" & lastRow & ",1)"
The previous code stops at the second line ; it opens the workbook located at File_Path and gives me the following error:
Run-time error '438'
Object doesn't support this property method
What am I doing wrong?
Finally, for my iii. point (see top example), I tried to do this:
lastRow = wb(Chr(34) & Month & " " & Year & Chr(34)).Cells(40, col).End(xlUp).Row
but it still gives me the last filled row, even if it is after row40
Thank you!
You do not have to type the name of the workbook again, when attempting to find the last rows. Just use the workbook variable, which you have already set.
Sub lastRow()
Dim lastRow As Long
lastRow = wb.Sheets(1).Cells(Rows.Count, 2).End(xlUp).Row
lastRow = wb.Sheets(1).Cells(40, 2).End(xlUp).Row
End Sub
Just use the variable you have already declared, and then the Sheets() method to define the sheet you're finding the last row on. In the example above we are finding the last row on the first worksheet in the workbook.

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