Excel worksheet name - vba

I want to make a macro that copies data from one sheet to another.
No problem, but I named the sheet with an emoji.
How can I tell VBA which sheet he has to use if the name of the sheet is for example: 🏠
Thanks in advance

You can use the AscW function to find the appropriate unicode value for the item and use that to find the correct sheet

To find the name of any given worksheet, activate the worksheet, open the VBA Editor (Alt+F11), open the Immediate Window (Ctrl+G) and type the following:
? ActiveSheet.Name
If the name for some reason is nonsensical, and only consists of a single character, run ? Asc(ActiveSheet.Name) to get the ASCII value.
Referencing an ASCII value in VBA can be done by calling Chr(putValueHere), for example like this:
Worksheets(1).Name = Chr(50)
If you have a plethora of sheets, you can print all of them by running this code:
Sub SheetNamePrinter
For i = 1 To Worksheets.Count
Debug.Print Worksheets(i).Name
Next i
End Sub

Related

Find worksheet name mapping in Excel

I have inherited an Excel workbook with several worksheets, all with named tabs. I have some VBA code that runs depending on buttons and inputs. When I compare the VBAProject and the list of Excel Objects, the sheet's names match the tabs. All good so far.
However, within the VBA code and on the spreadsheets itself, it refers to other worksheets. They are still part of the workbook, and I think that the code was created, and then tabs were renamed. But I don't know how Excel keeps the connection, or, more importantly, how I am supposed to figure out the connections.
So, for instance, I have a VLOOKUP that refers to MiscDataRange
=IF((VLOOKUP(E4,MiscDataRange,4,0))="Y"...
I don't know what MiscDataRange is, but within the VBA code I find one reference. Right after specifying worksheet "Misc Interrupt", which is NOT any of the named tabs.
Worksheets("Misc Interrupt").Range("H2:H47") = "N" 'Reset to N at noon.
UpdateData
Range("MiscDataRange").ClearContents
I do have a sheet called MiscInt, and it appears to be the sheet that "Misc Interrupt" is using and MiscDataRange is referencing.
My problem is there is a hole in my knowledge - I can guess that MiscInt and Misc Interrupt are the same worksheet, that the VLookup is referring to data on the MiscInt sheet. But I am reduced to guessing. I cannot find anything in the file that maps those two as the same. Where would I look to find that?
This question seems related, except he simply has a VLookup, and isn't looking at the VBA code: Non-existent Excel Worksheet, but Formulas and Defined names still work?. In addition, from the VBA code, I can see hidden and visible worksheets.
You can open the Names manager by going to the Formula Tab and clicking Names Manager or pressing ctrl+F3 or you can paste a list of Names and what the names reference by pressing F3. It may be necessary to unhide the names first.
Sub ShowAllNames()
Dim n As Name
For Each n In ThisWorkbook.Names
n.Visible = True
Next
End Sub
If I understand your question, you're looking to find where those named ranges are.
You can use a sub like this:
Sub t()
Debug.Print "Sheet: " & Range("testNamedRange").Parent.Name
Debug.Print "Full Location: " & Range("testnamedrange").Name
Debug.Print "File path: " & Range("testnamedrange").Worksheet.Parent.Path
End Sub
Does that help?

Referencing sheet by name returns "subscript out of range" error

The setup:
Excel workbook which imports data from a database (VBA - SQL)
Excel workbook which I want to run a "UpdateAll" macro from the first Excel workbook and grab some of the updated data
I run this code.
Sub RunMacro()
Run "'E:\programs(x86)\Dropbox\work\excel\data.xlsm'!UpdateAll"
End Sub
The error returned:
Subscript out of range (Error 9)
"Debug" leads to:
Sub UpdateAll()
daysBack = Sheets("Update").Range("B1").Value '<--- Called out as the error
Call getDatabase11DATA
Call correctData
End Sub
Frequently asked questions:
"Do you really have a sheet called "Update""
"Do you know it's case sensitive?"
"What do you use the daysBack variable for"
"What is "B1"?"
My answers to all of these questions:
Yes
Yes
The daysBack variable is supposed to be a number between 0 and 365 as in; "How many days do I want to go back when retrieving data from the database?". (0 == today only)
The default value in the B1 cell is 0.
There's no Update sheet in the active workbook. This:
Sheets("Update")
Is implicitly:
ActiveWorkbook.Sheets("Update")
Either 1) ActiveWorkbook isn't the book you're expecting, or 2) the worksheet name is mispelled; look (and remove) for leading or trailing spaces.
Note that the Sheets collection can also contain charts; if you're looking for a worksheet, use the Worksheets collection.
Moved solution from question to answer:
EDIT: The solution
was to edit the 2nd line in the 2nd code to this format:
daysBack = Workbooks("name_of_workbook_containing_update_sheet").Sheets‌​("Update").Range("B1‌​").Value
Which in my case would be:
daysBack = Workbooks("data").Sheets‌​("Update").Range("B1‌​").Value

Using the contents of cell's as part of a file location in a macro

This is what I have:
Option Explicit
Sub EditWorksheet()
Workbooks.Open "Y:\Laurence\contents(J2)"
Sheets("contents(N2)").Select
End Sub
So what I want to do is have VBA call on the values of cells J2 and N2 in order to decide which workbook and worksheet to open. For your information J2 could contain for example "Forecast2016.xlsx" and N2 may contain "MASTER". Just to be clear, for every possible content these cells can have, I do indeed have a spreadsheet in the relevant file location (Y:\Laurence).
Thanks in advance!
The problem is you are not passing a string, but a range with that code. This causes the run time error 13. Instead of
Workbooks.Open "Y:\Laurence\contents(J2)"
Sheets("contents(N2)").Select
try
Workbooks.Open "Y:\Laurence\" & Sheets("Contents").Range("N2").Value
Sheets(Sheets("Contents").Range("N2").Value).Select
This forces the value of the cell to be passed, not a reference to the range. Also notice I qualified your worksheet that you were referencing as well. As you had it before, it would have pulled Range("N2") from whatever sheet happened to be open at that moment.

Error in passing named range as argument in VBA

I'm having a problem while passing ranges across two subprocedures. Can you suggest the possible error ?
I have defined two subprocedures in VBA. In the first subprocedure, I have a selection of cells (3X3 MATRIX), named under "ABC" which I'm copying to another selection of cells "PQR". This is working :)
I wanted to enable "all borders" when the values are copied on PQR from ABC. For this I recorded a macro.
But, whenever I run this subprocedure, it is giving an error that object doesn't exist.
I called the macro in the following manner :
All_border_test PQR
The code for subprocedure is as given below :
Sub All_borders_test(d As Range)
Range(d).Select
' Recorded Macro to enable all borders (its working)
End Sub()
1- Take a look at: What's the RIGHT way to reference named cells in Excel 2013 VBA? (I know I'm messing this up) and VBA Reference Named Range ActiveSheet
2- fix your code to be like:
Sub All_borders_test(d As Range)
d.Select ' old code was: Range(d).Select
End Sub 'no parenthesis here
3- you can call All_borders_test using one of the following calls:
Call All_borders_test([PQR])
Call All_borders_test(Range("PQR"))
Call All_borders_test(Sheet1.Range("PQR")) ' assuming that PQR range exists in Sheet1

how to create relative path in hyperlink excel ? (Word.Document.12)

I have two documents, one which has all the info and it is a word document, and another that is an excel document, that have just some highlights from the word document.
I want to create some links between some selected text in word and excel cells, so far the special past is doing a great job, and create link in this format
=Word.Document.12|'C:\Users\...\xxx.docx'!'!OLE_LINK9'
Now i want to copy both documents in my usb and past them in other computers, this where the problem is, i would have to do the special past all over again since the path is different now, what i though as a solution was to put the path to the word document in cell let say A1 and concatenate the formula above, something like
=Word.Document.12|A1!'!OLE_LINK9'
but it doesnt work, it throws an error message, can you please help me?
PS : I would like to avoid vba if possible
PS : I would like to avoid vba if possible
I have included both ways to do it since the question is tagged with Excel-VBA as well :)
Take your pick.
VBA Way
Is this what you are trying?
Sub Sample()
Dim objOle As OLEObject
'~~> Change this to the respective Sheet name
With ThisWorkbook.Sheets("Sheet1")
'~~> This is your embedded word object
Set objOle = .OLEObjects("Object 1")
'~~> Cell A1 has a path like C:\Temp\
objOle.SourceName = "Word.Document.12|" & .Range("A1").Value & "xxx.docx!'"
End With
End Sub
Non VBA Way
Create a named range and call it say Filepath. Set the formula to
="Word.Document.12|'" & Sheet1!$A$1 & "xxx.docx'!'"
Where Cell A1 will have the file path.
Next Select your word document and in the formula bar, type =Filepath and you are done.