Get sheet name from a named range's Name object - excel

I have:
Microsoft.Office.Interop.Excel.Workbook wb;
Microsoft.Office.Interop.Excel.Name name;
Is there any way to get the worksheet name that the named range is on in the given workbook, assuming I've gotten the named range's Name object and wb already?

Yes, use the Parent property to work your way up the object hierarchy:
ws = name.RefersToRange.Parent.name;

Range.Worksheet is a self-documenting alternative to Range.Parent:
string wsName = name.RefersToRange.Worksheet.Name;
(Or in 2 steps:
Microsoft.Office.Interop.Excel.Worksheet ws = name.RefersToRange.Worksheet;
string wsName = ws.Name;
)
Reference:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.name.referstorange.aspx
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.worksheet.aspx
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._worksheet.name(v=office.15).aspx

wb.Names(name).RefersToRange.Parent.Name

Related

VBA Using a variable to select a worksheet

I'm attempting to code a system where I Select data from a set of worksheets whose titles are generated by the user. The Titles of the created sheets are then stored as strings. Is there a way to open/refer to a worksheet using these strings?
My code is as below
Dim Title As String
Dim Sheet_title As Worksheet
Sheets("Config").Select
Sheets("config").range("C25").Select
Title = ActiveCell.Value
Debug.Print Title
Sheet_title = Title
Sheets("Results").range("B7") = Sheets(title).range("E8")
The "sheets(Title)" fails, as well as my attempt to fix it by setting Sheet_Title = Title (Object variable not set - Error 91).
Is there any way to select a worksheet using a string variable?
Referencing Objects (Set)
A Quick Fix
To reference a worksheet (any object), you need to use the Set keyword:
Set Sheet_title = Sheets(Title)
Sheets("Results").Range("B7").Value = Sheet_title.Range("E8").Value
A Recommendation
Option Explicit
Sub Test()
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim wsCon As Worksheet: Set wsCon = wb.Sheets("Config")
Dim Title As String: Title = wsCon.Range("C25").Value
Debug.Print Title
Dim wsTit As Worksheet: Set wsTit = wb.Sheets(Title)
Dim wsRes As Worksheet: Set wsRes = wb.Sheets("Results")
wsRes.Range("B7").Value = wsTit.Range("E8").Value
End Sub

How to get the name property of the active NamedSheetView class?

Excel now has the possibility to store personal filtering views to help collaboration in simultaniously used documents.
I could only find Microsoft documentation for an add-in, but the function is available in my Excel version of MS Excel for Microsoft 365 MSO (16.0.13127.20266) 32bit.
https://learn.microsoft.com/en-us/javascript/api/excel/excel.namedsheetview?view=excel-js-preview
I am trying to store the currently applied NamedSheetView name property (for later restoring option) but
this code fails:
Dim sh1 As Worksheet
Dim xViewName As String
Set sh1 = ThisWorkbook.Sheets(Sheet6.Name)
xViewName = sh1.NamedSheetView.Name
However this code works (with previously created "Test" view):
sh1.NamedSheetViews.GetItem("Test").Activate
If this NamedSheetViews is a collection, I should be able to get the item property, but these codes also fail:
strName = sh1.NamedSheetViews.GetItem(1).Name
strName = sh1.NamedSheetViews.Item(1).Name
Anyone has ever succeeded in getting the current NamedSheetView of a Worksheet?
Here is how I probe unknown Object properties:
I start with a reference to the Object. If I don't know what the Object is I use TypeName() to return it's class name (data type). I then declare a variable of that data type. Wash, rinse and repeat as I drill down the structure. Once the variable is declared, selecting the variable and pressing F1 with open the Microsoft Help document for that data type.
Module Code
Sub WhatIsThat()
Const TestName As String = "TestName"
Dim View As NamedSheetViewCollection
Set View = Sheet6.NamedSheetViews
On Error Resume Next
View.GetItem(TestName).Delete
On Error GoTo 0
View.Add TestName
Dim SheetView As NamedSheetView
Dim n As Long
For n = 0 To View.Count - 1
Debug.Print View.GetItemAt(n).Name
Set SheetView = View.GetItemAt(n)
Debug.Print SheetView.Name
Next
Stop
End Sub
Immediate Window Tests
?TypeName(Sheet6.NamedSheetViews)
?View.GetItemAt(0).Name
?TypeName( View.GetItemAt(0))
SOLUTION:
(Thanks for the great help from TinMan)
Dim SheetView As NamedSheetView
Dim sh1 As Worksheet
Dim ActiveSheetView as string
Set sh1 = ThisWorkbook.Sheets(Sheet6.Name)
Set SheetView = sh1.NamedSheetViews.GetActive
ActiveSheetView = SheetView.Name
Application:
sh1.NamedSheetViews.GetItem(ActiveSheetView).Activate

vba: copy sheet with named destination

I want to create a copy of a sheet with data.
I managed to create a copy with this code, but want to create it with a defined name
Dim outsheet As String
outsheet = "SAP Import"
Dim wsCopy As Worksheet
Set wsCopy = ThisWorkbook.Worksheets(outsheet)
wsCopy.Copy After:=ThisWorkbook.Worksheets(outsheet)
Now I would like to change the name. The obvious solution is however not implemented in VBA (Parameter unknown)
Dim strSheetTemp As String
strSheetTemp = outsheet + "-temp"
wsCopy.Copy Destination:=ThisWorkbook.Worksheets(strSheetTemp)
How can I create a named copy?
Or how can I get the name of the copy to rename it ?
I believe the sheet can only be named (or renamed in this case) after being copied.
Dim outsheet As String
outsheet = "SAP Import"
Sheets(outsheet).Copy After:=Sheets(outsheet)
Sheets(Sheets(outsheet).Index + 1).Name = outsheet & "-temp"
Try this one:
Dim outsheet As String
outsheet = "SAP Import"
Dim wsCopy As Worksheet
Set wsCopy = ThisWorkbook.Worksheets(outsheet)
wsCopy.Copy After:=wsCopy
ActiveSheet.Name = outsheet & "-temp"
Following your trial philosophy...
In order to change the name you should use the property .Name.
That one will work in your case.
Hope it helps

Using a variable for the title argument in InputBox

I've created an InputBox which asks the user for the letter of the last column containing entries. I'd like the title to be sheet name, according to an index. Here's the code I've written:
Dim LastEntryCol As String
Dim ShtName As Variant
Set ShtName = ThisWorkbook.Sheets(2) '****The number 2 will need to be changed to an index. For now it's hard-entered.
LastEntryCol = Application.InputBox("What is the letter of the last column containing entry labels?", ShtName)`
When I run it, I get error 1004 "Method 'InputBox' of object '_Application' failed". I suspect it has something to do with how I set ShtName, as the code runs properly if I replace the title argument with something like "hooray coding!"
Can anyone explain to me what's going wrong and how I can fix it? Thanks!
You are passing the whole WorkSheet object, instead of just its name. Try this:
Dim LastEntryCol As String
Dim ShtName As String
ShtName = ThisWorkbook.Sheets(2).Name '****The number 2 will need to be changed to an index. For now it's hard-entered.
LastEntryCol = Application.InputBox("What is the letter of the last column containing entry labels?", ShtName)
Sheets is a Collection so to get the name you need to use
ShtName = ThisWorkbook.Sheets.Item(2).Name
Also pretty sure this will always be a string so you could use Dim ShtName As String and avoid Set.

Getting Name of Worksheet C++/CLI

How do I get the name of a worksheet in an Excel workbook using C++/cli?
I understand when you use C# you can do this:
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
string strWorksheetName = worksheet.Name;
Although I don't know how to do the:
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
in C++.
This would translate to the following C++/CLI:
Excel::Worksheet^ worksheet = dynamic_cast<Excel::Worksheet^>(sheets->get_Item(1));
String^ strWorksheetName = worksheet->Name;

Resources