Refer to Excel workbook by path, based on cell value data - excel

I have an Excel sheet that draws data from other, closed Excel workbooks. Currently it works fine when I list out the closed workbook's entire path, but I'd like to use a variable, stored in a separate cell, as part of the path name.
For example, I am trying to reference a workbook called
workbook12.10.12.xls
In a separate workbook (we'll say the "active" workbook), I have a cell with formula
=INDEX('C:\Path[workbook12.10.12.xls]SHEET1'!$B$1:$B$5, MATCH("match text", 'C:\Path[workbook12.10.12.xls]SHEET1'!$A$1:$A$5, 0))
which finds the value in workbook12.10.12's B column corresponding to the cell in the A column that contains "match text." This works fine; however, I have a cell in the active workbook with the value
12.10.12
and would like to somehow reference this value in the INDEX function.
I can't have the other workbooks open, so the INDIRECT function won't help. Googling seems to suggest that Excel doesn't have a simple one-stop solution for this kind of thing... can someone help please? Thanks!

From Frank Kabel's 2004 post at Dicks Blog you could
Use Laurent Longre has developed the free add-in MOREFUNC.XLL which includes the function INDIRECT.EXT
Use SQL.REQUEST as described here *does not appear to be supported anymore and I am not clear if this could handle your INDEX\MATCH request
Use Harlan Grove’s PULL function
In addition you could:
Create a "dirty link" directly via code that enters a formula referring to the workbook you need
For pulling values - but not for working with ranges - you could use Walkenbach's ExecuteExcel4Macro XLM method

I think what you what to do is to find the specific record in the specific file (date named).
You may do it by a simple VBA code.
Suppose you are going to search for a record# say REC001 in A1, date file 12.10.12 at cell C1, and have the result to be display at cell A7
On the worksheet you want to enter input and get output, rightclick the sheet tab and select 'View code' and paste the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C1")) Is Nothing Then Exit Sub
Range("A7").Formula = "=INDEX('C:\TEMP\[workbook" & Range("C5").Value & ".xls]SHEET1'!$B$1:$B$5, MATCH(" & Range("A1").Value & ", 'C:\TEMP\[workbook" & Range("C5").Value & ".xls]SHEET1'!$A$1:$A$5, 0))"
End Sub
Then every time you edit C1, the formula will be updated.
Actually I don't think you should use INDEX function in your case. It is more simple to use a VLOOKUP. E.g.:
Range("A8").Formula = "=vlookup(" & Range("A1").Value & ",'C:\TEMP\[workbook" & Range("C5").Value & ".xls]SHEET1'!$A$1:$B$5,2,false)"
You will have to note on a few points:
1. you paste the code on the Sheet1 object (or the sheet name) but not to insert a new module
2. your path and filename for the target file is correct, including the .xls and .xlsx
3. your original file only cover to $B$5
4. on VBA, recommend you to save the file as .xlsm format

You can store a full reference including the file path to a range in a closed file in a name in excel (either directly or via VBA based on selections in different cells and using the Worksheet_Change procedure as above) and then refer to the file using the name in a formula as normal. This gets over the limitation in the INDIRECT function.
The VBA is very simple:
New_Ref = Sheets("Wells").Range("K6")
ActiveWorkbook.Names("MyWorkbook").RefersTo = "=" & New_Ref
The only trick is to be sure to include "=" in the name.
Names have a huge number of uses once you spot this. I have used this to get data from a closed file on a remote sharepoint site without any difficulty - I assume sharepoint deals with all the permissions.

Related

Read One Cell and Copy to Another Only Upon Opening Spreadsheet

I need to copy the data in cell c3 to cell i11 when I open my spreadsheet. I do not want i11 to change after this action until I reopen the spreadsheet.
You can set the code in the ThisWorkbook part of your projects. Select Workbook and Open or use the following code.
The working formula is the value you see me do below, but this can be achieved a number of way such as using Cells(3,3).Value. Additionally the way the sheet is referenced can vary. I put both the Activesheet reference and a explicit sheet reference, depending how your sheet is structured you may have to use one over the other, but choose one below and see how your sheet handles it and if adjustments are needed.
Private Sub Workbook_Open()
'Use one of these depending on how your sheet opens
ActiveSheet.Range("I11").Value = ActiveSheet.Range("C3").Value
Worksheet("YourWorksheetHere").Range("I11").Value = Worksheet("YourWorksheetHere").Range("C3").Value
End Sub

Excel VBA refer to worksheet that uses icon/emoji as part of name

I'm using Excel for Office 365 MSO 64-bit.
I want to write a VBA macro that selects different worksheets in a workbook based on the worksheet's name.
For example, I have two lines of VBA code that activate a workbook and then select a specific sheet in the workbook by the sheet's name.
Windows("myworkbook").Activate
Sheets("mysheet").Select
However, I have to work with some sheets that contain icons or emojis in them. For example, there is a worksheet that has this name: "🚑 Patient".
If I try to paste the icon/emoji into VBA like this: Sheets("🚑 Patient").Select, the icon does not show up in the VBA editor. Instead, I get Sheets("????? Patient").select.
I have also tried to use ChrW() to encode? the ambulance character (see here: https://www.compart.com/en/unicode/U+1F691)
When I run this macro below), I get an invalid procedure call or argument as noted below.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(128657) ' get invalid procedure call or argument here
Sheets(x & " Patient").Activate
End Sub
I also tried code for ambulance... also tried ChrW(&H1F691), but I get the same error.
My suspicion is that I am using the wrong argument for ChrW(), but I'm lost.
edit: So, the docs say that my argument for ChrW() is out of range. That helps explain the error, but I'm still missing a work-around.
Question: Is there a way to refer to use VBA to select worksheets that have an icon/emoji as part of their name?
I know you can also refer to worksheets by index number like this Sheets(3).Select.
However, there will be instances where I don't know the index of the sheet ahead of time, but I will know the name of the sheet, so it is preferable for me to call the worksheets by name.
Thank you.
In addition to the self-answered response, when working in a single workbook, the coder can assign a CodeName to the sheet in the VBA IDE, and then use that CodeName directly. This is really only valid if the Sheet is not re-created (i.e. is a permanent sheet in the book) at any stage, because a new/copied sheet will be automatically given a new CodeName by Excel.
For example, if given the CodeName shtPatient (see picture bellow), the code could be:
Sub SelectWeirdSheet()
' Windows("MYWorkbook.xlsx").Activate '<-- this approach has limitations
shtPatient.Activate ' See my comment below about the limitation - this will not work as expected in this example.
End Sub
Note: https://stackoverflow.com/a/10718179/9101981 explains why not to use Activate, but I have left the code as-is for the purposes of this answer. Also look at Using Worksheet CodeName and Avoiding .Select & .Activate. Another limitation noted is that the CodeName is only valid for the workbook that the code is in - so may not be applicable in this case.
I have highlighted the CodeName parts of the IDE in the image below, see how "Test Patient" is not called "Sheet7", but instead has a meaningful name that I gave it in the properties window below.
In order to properly address the emoji, it should be split into two separate unicode characters.
In this case, it would be x = ChrW(&HD83D) & ChrW(&HDE91)
Those two unicode characters make up the ambulance emoji.
So, this Macro now works.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(&HD83D) & ChrW(&HDE91)
Sheets(x & " Patient").Activate
End Sub
Found the solution on reddit of all places https://www.reddit.com/r/excel/comments/6pq1r1/vba_how_can_i_write_emojis_using_chrw/

VBA or FORMULA to update file path name with most recent working date

I am producing a file everyday with updated market data (the layout is identical each day, just different numbers in the tables). I have a couple of columns on each worksheet that take data from a separate file. This file has a date as part of its name.
One formula is:
=K10-'O:\Daily Vols\PDFsourcefiles\[Daily PDF 2018.05.18..xlsm]POWERPDF'!$K$10
This equation gives me the data in K10 today minus the data in K10 from yesterday's document.
I would like to find a way to automatically update this file path each day with the previous date on which I ran the file (NOT just date-1 as weekends can't be included!)
I am currently using 'find and replace' function but this is very time consuming and prone to human error if I forget one or two cells.
I do have today's date in cell A1 of the 'Comments' worksheet if this would be useful to use.
I hope someone out there with more experience of excel will be able to help with this - many thanks in advance!
This should be doable in VBA. I think the Workbook_Open event would be best. The link will be made when you open the file. Assuming you don't know how to get to the VBA editor press Alt+F11 and double-click "ThisWorkbook". Then type the following code:
Private Sub Workbook_Open()
Sheets("[YourWorksheetName]").Cells([RowNum],[ColNum]) = "=K10-'O:\Daily Vols\PDFsourcefiles\[Daily PDF " & Format(Date - 1, "yyyy.mm.dd") & "..xlsm]POWERPDF'!$K$10"
End Sub
Where [YourWorksheetName] is the name of the worksheet of today's file (maybe POWERPDF?), [RowNum] and [ColNum] are the row/column that you want this formula. So if this formula goes into B1 then it would be .Cells(1, 2) because B is the second column.
Hopefully that works better.

How to create hyperlinks with VBA to the same open workbook

The setup
I have an Excel workbook stored on my hard drive.
The structure is such that on the first sheet I have a list of the names of the other sheets in the same workbook (...which can be created or deleted).
All the names on the list, on the first sheet, are supposed to be hyperlinks to the corresponding sheet in the workbook. So, by clicking the name on the first sheet you jump to the corresponding sheet.
When a new sheet is created a macro creates also the new name on the list on the first sheet and makes a hypelink of it. This works.
...BUT...
The links point to the stored version of the file, not to the open workbook! Clicking the links opens the stored file and not the one which is under work.
QUESTION: How to create a hyperlink that always points to the same open workbook and not to the stored copy of it?
Try this:
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"Sheet3!", TextToDisplay:="Link to sheet #3"
Address is the URL and SubAddress is a localtion of the page (or a sheet or a range in excel workbooks).
You may try creating the hyperlink as a formula (via VBA, as you need).
I am using the parameters you posted, this may need a little adjustment.
Dim rngsrc as Range, rngtrgs as String
Set rngsrc = Worksheets("Summary").Cells(Cell.Row, 5)
Set rngtrg = "'" & sSheetName & "'!B5"
rngsrc.FormulaR1C1 = "=HYPERLINK(" & rngtrgs & "," & sSheetName & ")"
See
Official documentation
Example 1
Example 2
This (hopefully) answers your question. As a separate note, it still remains to be clarified why you see the behavior you see.
It seems that the problem comes from the following fact: My file is in a SharePoint folder. If I open it just for reading, the hyperlinks work fine. If I open the file for editing, a copy of the SharePoint file is placed on my hard disk, on a specified location. So, the path to the file is not the same as it would be if I open it read-only. Should I use hyperlink.follow to solve this?
So, this all comes down to the question: In VBA/Excel, can I create a hyperlink which always points to a location in the same opened file so that the hyperlink ignores the storage path of the corresponding file? Using empty string (or BLANK) doesn't help as the address parameter in hypelinks.add as Excel seems to automatically fill in the whole storage path.
I upgraded to Office 2013 and "PADAM": The problem vanished! It seems that there was a bug in Excel/VBA in this in the 2007 version.

Linking cells to each other; having cells update linked cells and vice versa

I have two ideas that could lead to more or less the same result.
I am trying to have similar cells or tables update themselves to whatever the most recent entry was in the linked system. For example, cell A1 is linked to cell B2 (in this system that I am trying to create). I would enter something like "1" or "Text" or whatever in A1 and the system would update cell B2 to whatever I entered in cell A1. However the opposite must work in the same manner. If I changed whatever in cell B2 to, say, "5", cell A1 would also display "5". Also, note that I have Excel 2013.
I need this to work with a cell or a table. So, getting to the possible solutions...
A subroutine in VBA that automatically updates all the linked cells or tables.
Some sort of mechanic unknown to me that uses VBA or another Excel aspect to do this, like a function or tool.
In your answer or solution, please be mindful to my inexperience with VBA. Thanks in advance.
You can try a worksheet change function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address then
ActiveSheet.Range("B1").Value = ActiveSheet.Range("A1").Value
ElseIf Target.Address = Range("B1").Address then
ActiveSheet.Range("A1").Value = ActiveSheet.Range("B1").Value
End If
End Sub
Although this seems like it might create an infinite loop (the update from the change causes another change) it DOES work for me in Excel 2010..
There are other Worksheet functions you can try as well (e.g. Worksheet_SelectionChange)
This macro needs to be placed/entered as a WORKSHEET macro on the sheet where you desire to use it..it will not work in a Module.
To install:
1) Save your workbook as a macro-enabled file.
2) Close Excel, reopen file, and enable macro security
3) Type Alt-F11
4) In the project explorer view on the left, look for your sheet name. Double click it
5) In the code entry area on right (big window) paste the example code above
6) Return to your worksheet and try it.

Resources