Excel VBA reference to other workbook through "set" with generic variables, keeping that reference in formulas - vba

Here's my problem. I'm running a central KPI file that has to be updated each week. It is refering to files that have a specific name + the current week that they're in. For example, the KPI refers to a DT1P and MPO file, both ending with the current week.
So, for week 2016-28, the central KPI file should look up the values in MPO2016-28.xlsx and DT1P2016-28.xlsx.
For week 2016-29, the central KPI file should look up the values in MPO2016-29.xlsx and DT1P2016-29.xlsx.
However, when I try to run formulas to do some calculations from those sheets, I have no clue how I can make the week generic (the week is being made in Range("E1") in the KPI file), without just refering to the entire location the file is. How can I solve this?
I have:
Set wb_MPO = Workbooks.Open("\\S007v\MPO" & Range("e1").Value & ".xlsx")
wb_KPI.Activate
Set wb_DT1P = Workbooks.Open("\\S007v\DT1P" & Range("e1").Value & ".xlsx")
wb_KPI.Activate
To define the files that need to be opened.
However, my formulas look like this (there's many more of them, but the principle stays the same, so I won't bother you with them)
Range("E4").FormulaR1C1 = _
"=INDEX('[MPO2016-28.xlsx]CSR MPO'!C10,MATCH(RC[-4],'[MPO2016-28.xlsx]CSR MPO'!C1,0),0)"
Could it be something like:
Range("E4").FormulaR1C1 = _
"=INDEX('[wb_MPO]CSR MPO'!C10,MATCH(RC[-4],'[wb_MPO]CSR MPO'!C1,0),0)"
Or am I totally off?
Thanks in advance for your support :)

You have the right idea, but it would be:
Range("E4").FormulaR1C1 = _
"=INDEX('[" & wb_MPO.Name & "]CSR MPO'!C10,MATCH(RC[-4],'[" & wb_MPO.Name & "]CSR MPO'!C1,0),0)"
This will only work if the workbook you are trying to reference is open though. I feel like there are probably better ways to extract this information than opening the workbook and writing a formula to a cell.

Related

VBA to reference new sheet daily

I was looking to be able to reference a new worksheet daily within a VLookup Code
Currently my code looks as below :
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[133],'[Unavista UTI Lookup November2019.xlsm]Nov 28'!C4:C7,4,0)"
However each day the date changes within the code. Is there a way I can amend this so each day the Macro will just look for the latest Worksheet with the latest date?
In this case, you can string in a couple of Format(Now(), statements in your code as per below:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[133],'[Unavista UTI Lookup " & Format(Now(), "mmmmyy") & ".xlsm]" & Format(Now(), "mmm dd") & "'!C4:C7,4,0)"
Result:
=VLOOKUP(EH13,'[Unavista UTI Lookup November19.xlsm]Nov 28'!$D:$G,4,0)
Doesnt seem to be working...
I've hardcoded the file being referenced as MyLookupfile
Rather than looking for a specific date within the worksheet name, is it possible to just look for the latest created tab, so it looks like something along the lines of the below? (Note - i've used latestcreatedtab as a sub for now).
Formula still needs work but just trying to show what i am aiming to get too
Application.WorksheetFunction.VLookup(Range("EH2"),[MyLookupFile & LatestCreatedTab], C4:C7, 4, 0)

Cannot use Application.Evaluate() for CUBEVALUE function

Dear MS Excel wizards =)
Recently I'm developing a UserForm for Budget file, to bring all basic controls to one place and ease the life of users. My initial idea was calculating all primary values (starting num of Students, finishing num of Students...) in this form and feed to Budget file formulas as values. For some calculations, I have to address ThisWorkbookDataModel, therefore I was planning to use Application.Evaluate()
Yet it fails with case like this:
m1 = Application.Evaluate("=CUBEVALUE(""ThisWorkbookDataModel"",CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimDate].[1.Year].&[" & mYear & "]"",""[DimDate].[3.MonthCalendarTrue].&[" & mMonth & "]""}),CUBEMEMBER(""ThisWorkbookDataModel"",""[Measures].[mQofStudents]""),CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimCampus].[Burleigh]""}))")
As a workaround, I evaluate this formula on the worksheet first:
Range("AvgStudStartValBH").Formula = "=CUBEVALUE(""ThisWorkbookDataModel"",CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimDate].[1.Year].&[" & mYear & "]"",""[DimDate].[3.MonthCalendarTrue].&[" & mMonth & "]""}),CUBEMEMBER(""ThisWorkbookDataModel"",""[Measures].[mQofStudents]""),CUBEMEMBER(""ThisWorkbookDataModel"",{""[DimCampus].[Burleigh]""}))"
Yet this really creates inconveniences in refreshing of the result in UserForm.
What I found while debugging this Application.Evaluate() issue is that if I cut away last CUBEMEMBER(), it works! So maybe Application.Evaluate() has a limit in symbols?
If so, is there any other workaround except pasting this into Excel worksheet to evaluate there before getting this to the form?

What is the macro formula for copy data sheets from monthly name file?

Sorry my English not very good, also still newbie in macro.
I'm able to formula grabbing data doing copy paste other sheets on other files. but the problem is the files is on monthly and year.. So i need to change the macro formula every year.
Example:
Workbooks.Open Filename:= _
"C:\REPORT\2016\february\STOCK REPORT_JANUARI_*2016*.xls"
Is there any formula or something so i didn't need to change the year every times?
You can use the Year Function.
Using Year(Date) equals the year of the currebt date. For today, Year("28/02/2017") equals 2017.
Replace your line of code with the line below:
Workbooks.Open Filename:= _
"C:\REPORT\2016\february\STOCK REPORT_JANUARI_*" & Year(Date) & "*.xls"

Microsoft Excel 2007 - How do I write a dynamic Table_Array for Vlookup

I'm not sure if I need to post this question in a specific place, but here goes.
WARNING: Wall of text. I need to explain my situation fully as it appears to be a unique problem with not many solutions.
TL;DR - Master spreadsheet grabs data from "weekly" spreadsheet via vlookups. A new spreadsheet is released every week. How do I automate the vlookup to grab data from a new spreadsheet, every week?
Now here is the long version...
I have a master spreadsheet that uses vlookups pointing to another spreadsheet. Example code below:
=VLOOKUP(A2,'...OTC\2016\Reports Sent\[SALES BY SKU STORE wk 3 (retail) (2).xls]SKU'!$D$1:$G$65536,4,FALSE)
Great. Easy Peasy.
The master spreadsheet is currently pointing to data for "Week 3". Hence, [SALES BY SKU STORE wk3 (retail)(2).xls]. Currently in the file directory, where the spreadsheet lives, week 3 spreadsheet is the most up-to-date. Every week, a new spreadsheet will be saved in the same folder but called [SALES BY SKU STORE wk4 (retail)(2).xls]. Getting the idea now..?
I want the master spreadsheet to be able to point to the new weekly data, everytime a new spreadsheet exists. So one would think to somehow make the vlookup Table_Array dynamic.
How do I accomplish this?
Additional note:- I have written a PowerShell script to output all the filenames & file extensions into a .csv file. Then I have created a macro to import the .csv data into a separate sheet in the master file. I did this as a potential lookup table. Example data below, we'll call it "Filename List":
[SALES BY SKU STORE wk1 (retail)(2).xls]
[SALES BY SKU STORE wk2 (retail)(2).xls]
[SALES BY SKU STORE wk3 (retail)(2).xls]
Solutions attempted but failed because my excel/macro/VBA knowledge is very limited:-
The file destination does not change. The only part of the filename that changes is the week number. I have considered the idea of + 1 to the week number everytime a new file has been added to the Filename List. I don't know how to do any of this.
I have tried the idea of CONTCATENATE with the Table_Array and the Filename List. Again, I don't know how to pull this off. Browsing the web has led me to believe INDIRECT is the way forward.
The idea of writing IF statements via VBA code has come to mind. Potentially hardcoding the filenames in the vlookup, somehow. Again, I don't know how to do this.
Please can anyone shed some light as to how I do this? If you can answer with an idea, I will try my best to emulate it. I will appreciate anything at this point.
Thank you.
Something like this - I've assumed you will never have more than 53 financial weeks:
Sub updateWorkbook()
Dim n As Long
Const csFOLDER_PATH As String = "C:\your path\"
For n = 53 To 1 Step -1
If Dir(csFOLDER_PATH & "SALES BY SKU STORE wk" & n & " (retail)(2).xls") <> vbNullString Then
FileCopy csFOLDER_PATH & "SALES BY SKU STORE wk" & n & " (retail)(2).xls", csFOLDER_PATH & "SALES BY SKU STORE CURRENT (retail)(2).xls"
Exit For
End If
Next n
End Sub
You can call this from the Open event of the master workbook. In the ThisWorkbook module, add:
Private Sub workbook_open
updateWorkbook
End Sub
Can you use
=VLOOKUP(A2,INDIRECT("'...OTC\2016\Reports Sent\[SALES BY SKU STORE wk " & A1 & " (retail) (2).xls]SKU'!$D$1:$G$65536"),4,FALSE)
where A1 is the week number you want to import the data from?

Can I link to workbooks that haven't been created yet if I know what they will be named?

We use an excel sheet (called an "Assign") to store info for a particular day. A new Assign is created each day and placed in the same folder, the previous day's is removed. I know what the name of the new Assigns will be before they're created, it goes by date ('May 19 Assign', 'May 20 Assign', etc...)
I have a sheet that I want to link to the daily Assign. It's a straight forward VLOOKUP that I need to do to get data from the daily Assign workbook, but since it's a new file each day, I'm just wondering if there's a way to have the links update automatically to the new file?
Thanks
For the easiest solution, see Tim's comment.
You COULD write a UDF that returns the latest fileName. This one below would return "C:\MyPath\myFile_20140519.xls" for today.
Function GetLatestFileName() As String
GetLatestFileName = "C:\MyPath\myFile_" & Format(Date, "yyyymmdd") & ".xls"
End Function
Then you could enter it in a cell etc and it would change dynamically.