Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
x2010
I named a cell 'homecell' (Formulas-Define Name-Name = "homecell") That cell is selected. The homecell name appears in Name box. Shouldn't the following vba line then give me "homecell"? -- Application.Selection.Name It gives me "=Sheet3!$A$1" Thank you |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
oldyork90 presented the following explanation :
x2010 I named a cell 'homecell' (Formulas-Define Name-Name = "homecell") That cell is selected. The homecell name appears in Name box. Shouldn't the following vba line then give me "homecell"? -- Application.Selection.Name It gives me "=Sheet3!$A$1" Thank you Selection returns a 'fully absolute' address of the selected cells. If you want to get the defined name you'll need to loop the names collection for a matching absolute address. (This won't work if the address has any 'relative' refs) Example1: (uses Intersect function) If Intersect(Selection, Range("homecell") Then "True" Else "False" Example2: (compare address) Select Case Selection.Address Case Range("homecell").Address: "True" Case Else: "False" End Select 'Case Selection.Address -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try: Application.Selection.Name.Name
-- Jim Cone Portland, Oregon USA http://www.mediafire.com/PrimitiveSoftware (Data Options add-in: rows/dates/random stuff) "oldyork90" wrote in message ... x2010 I named a cell 'homecell' (Formulas-Define Name-Name = "homecell") That cell is selected. The homecell name appears in Name box. Shouldn't the following vba line then give me "homecell"? -- Application.Selection.Name It gives me "=Sheet3!$A$1" Thank you |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Oops.., missing a closing parenthesis...
Example1: (uses Intersect function) If Intersect(Selection, Range("homecell")) Then "True" Else "False" ...sorry about that! Also, I mentioned looping the names collection but omitted an example and so... Dim nam As Variant For Each nam In ActiveSheet.Names '//assumes local scope If Selection.Name.Name = nam Then '//do stuff Exit For '//quit looking End If Next 'nam -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() Selection returns a 'fully absolute' address of the selected cells. If you want to get the defined name you'll need to loop the names collection for a matching absolute address. (This won't work if the address has any 'relative' refs) Thank you Garry Ok, then shouldn't I be able to iterate here? (Didn't try to access a member yet... didn't get past the debugger) ============= dim n as Name For Each n In Application.Selection.Names Debug.Print "Here we are" Next =============== The error message says not supported. It's probably something dumb I'm doing. The name is assigned to an absolute address. I also tried selecting a multi cell range hoping that maybe the collection of names would then populate. I may not understand what I see in the object browser. This looks doable. Set me straight. Thanks again. |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
oldyork90 was thinking very hard :
Selection returns a 'fully absolute' address of the selected cells. If you want to get the defined name you'll need to loop the names collection for a matching absolute address. (This won't work if the address has any 'relative' refs) Thank you Garry Ok, then shouldn't I be able to iterate here? (Didn't try to access a member yet... didn't get past the debugger) ============= dim n as Name For Each n In Application.Selection.Names Debug.Print "Here we are" Next =============== The error message says not supported. It's probably something dumb I'm doing. The name is assigned to an absolute address. I also tried selecting a multi cell range hoping that maybe the collection of names would then populate. I may not understand what I see in the object browser. This looks doable. Set me straight. Thanks again. Well, it doesn't look anything like the sample I posted and so it doesn't surprise me that you're not getting different results than your original post. VBA is extremely syntax sensitive, and so you can't just make stuff up and expect it to work. For example, what leads you to believe that 'Application.Selection.Names' is proper VBA syntax for referencing a 'Names' collection? 'Names' (note it's plural) is a collection that belongs to a Workbook (if it has global scope), or a Worksheet (if it has local scope). That means you must loop the collection in reference to a workbook object OR a worksheet object. Selection is a VBA reference to a cell or group of cells. So if try to access 'Names' in reference to 'Selection' you get an error because 'Selection' doesn't support 'Names'. It does support a 'Name' property (note it's NOT plural here) *if it returns an absolute address assigned to a DefinedName*, otherwise it also will throw an error. The value returned for "Selection.Name" (as already stated in my original reply) returns an absolute address. If this address has a defined name then it can be returned by using "Selection.Name.Name" (as noted in Jim Cone's reply as well as how used in my code sample for looping the Names collection) The sample code in my reply shows the correct way to iterate the names collection of the active sheet IF the names defined have local (worksheet-level) scope. This *should be the default scope* unless there's some highly extenuating reason to define names with global (workbook-level) scope. I suspect your defined name "homecell" actually does have global scope and so it would be a member of the workbooks object. In this caes you'll need to swap out *ActiveSheet* for *ActiveWorkbook* in the For..Each loop. Also, (no big deal!) I chose to use type Variant for the iterator because this is a habit I got into for using For..Each loops so I don't have to worry about what type the collection item is in reusable loop code. IOW, I can use the same code to loop the Worksheets collection (or any other objects) without having to explicitly modify the code. As I said, this is no big deal but I just prefer not to proliferate code with unecessary loops when I can reuse utility code designed for that purpose.<g -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Mon, 19 Mar 2012 09:19:48 -0700, "Jim Cone" wrote:
Try: Application.Selection.Name.Name -- That will work if there is only one name assigned to the cell; or if there are multiple names and the one you want to return is the first in an alphabetically ordered list. |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Garry
Well my question wasn't directly about your answer so I'm not surprised it doesn't look anything like it. I was trying to understand something about Selection via restatement. Your code is fine. Bottom line is I don't understand what I'm seeing (or not seeing) in the object browser. I need to experiment, and think real hard, for awhile. Thanks again for your help. Thanks again. |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
oldyork90 pretended :
Garry Well my question wasn't directly about your answer so I'm not surprised it doesn't look anything like it. I was trying to understand something about Selection via restatement. Your code is fine. Bottom line is I don't understand what I'm seeing (or not seeing) in the object browser. I need to experiment, and think real hard, for awhile. Thanks again for your help. Thanks again. For the most part, using the ObjectBrowser is a good way to get to know about properties/methods. I find it very useful for straight forward stuff, and F1 is always ready to display extended info on whatever happens to be selected. Unfortunately, you won't see anything that resembles "Selection.Name.Name" because the OB and Help don't offer much info. 'Selection' is listed under 'Application' as a method that returns a 'Range' object, but it doesn't explain that it does this via its absolute address. In this case you could see that there are various possible ways to work with 'Selection'... Selection.Address Selection.Name ...return the same location as what you see in the NameBox as an absolute value ("$" sign prefix to col/row label). At this point you have a fully qualified ref to a 'Range' and so you can get its 'Name' if one was assigned to the selected range by extending it like this... Selection.Name.Name ...the problem with using the above is that it only returns a value IF a name has been assigned, otherwise it throws an error. This is why I omitted an example of it in my 1st reply. Also, Ron's contribution applies here too, and so is why I explicitly ref the named ranges in my 1st reply. Since using the above syntax leaves lots of room for error and/or ambiguity I wouldn't use it in general coding. The 2 methods I showed are going to give you reliable results because they ref specific ranges of interest that you can code for without guessing what is selected. Notice that there's no reason for either method to throw an error, AND allows no chance of ambiguity. You could also assign 'Selection' to a variable typed as Variant, Object, or Range... Dim rng As Range Set rng = Selection Dim oRng As Object Set oRng = Selection ...to get a fully qualified ref to the cells. In this usage you can test for a name as follows: Dim sRngName As String On Error Resume Next '//handle error if name doesn't exist sRngName = rng.Name (OR oRng.Name) If sRngName = "" Then MsgBox "The selection doesn't have a name!" -OR- Dim sRngName As String On Error GoTo errexit '//handle error if name doesn't exist sRngName = rng.Name (OR oRng.Name) NormalExit: Exit Sub errexit: MsgBox "The selection doesn't have a name!" End Sub You can use the Variant type if you want to step through each cell. This is faster than working directly in the worksheet... Dim vRng As Variant, i As Long, j As Long Set vRng = Selection '//load cell contents into a 1-based 2D array If Not IsArray(vRng) Then Exit Sub 'Get the number of Rows/Cols Dim lRows As Long, lCols As Long lRows = UBound(vRng): lCols = UBound(vRng, 2) 'Step through each cell col-by-col for each row For i = LBound(vRng) To UBound(vRng) 'For i = 1 To lRows For j = LBound(vRng, 2) To UBound(vRng, 2) 'For j = 1 To lCols '//do stuff to cell in each col of Rows(i) Next 'j Next 'i -OR- 'Step through each cell row-by-row for each col For i = LBound(vRng, 2) To UBound(vRng, 2) For j = LBound(vRng) To UBound(vRng) '//do stuff to cell in each row of Columns(i) Next 'j Next 'i -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Mar 20, 11:50*am, GS wrote:
oldyork90 pretended : Garry Well my question wasn't directly about your answer so I'm not surprised it doesn't look anything like it. *I was trying to understand something about Selection via restatement. *Your code is fine. *Bottom line is I don't understand what I'm seeing (or not seeing) in the object browser. *I need to experiment, and think real hard, for awhile. *Thanks again for your help. Thanks again. For the most part, using the ObjectBrowser is a good way to get to know about properties/methods. I find it very useful for straight forward stuff, and F1 is always ready to display extended info on whatever happens to be selected. Unfortunately, you won't see anything that resembles "Selection.Name.Name" because the OB and Help don't offer much info. 'Selection' is listed under 'Application' as a method that returns a 'Range' object, but it doesn't explain that it does this via its absolute address. In this case you could see that there are various possible ways to work with 'Selection'... * Selection.Address * *Selection.Name ..return the same location as what you see in the NameBox as an absolute value ("$" sign prefix to col/row label). At this point you have a fully qualified ref to a 'Range' and so you can get its 'Name' if one was assigned to the selected range by extending it like this... * Selection.Name.Name ..the problem with using the above is that it only returns a value IF a name has been assigned, otherwise it throws an error. This is why I omitted an example of it in my 1st reply. Also, Ron's contribution applies here too, and so is why I explicitly ref the named ranges in my 1st reply. Since using the above syntax leaves lots of room for error and/or ambiguity I wouldn't use it in general coding. The 2 methods I showed are going to give you reliable results because they ref specific ranges of interest that you can code for without guessing what is selected. Notice that there's no reason for either method to throw an error, AND allows no chance of ambiguity. You could also assign 'Selection' to a variable typed as Variant, Object, or Range... * Dim rng As Range * Set rng = Selection * Dim oRng As Object * Set oRng = Selection ..to get a fully qualified ref to the cells. In this usage you can test for a name as follows: * Dim sRngName As String * On Error Resume Next '//handle error if name doesn't exist * sRngName = rng.Name *(OR *oRng.Name) * If sRngName = "" Then MsgBox "The selection doesn't have a name!" -OR- * Dim sRngName As String * On Error GoTo errexit '//handle error if name doesn't exist * sRngName = rng.Name *(OR *oRng.Name) * NormalExit: * Exit Sub * errexit: * MsgBox "The selection doesn't have a name!" * End Sub You can use the Variant type if you want to step through each cell. This is faster than working directly in the worksheet... * Dim vRng As Variant, i As Long, j As Long * Set vRng = Selection '//load cell contents into a 1-based 2D array * If Not IsArray(vRng) Then Exit Sub * 'Get the number of Rows/Cols * Dim lRows As Long, lCols As Long * lRows = UBound(vRng): lCols = UBound(vRng, 2) * 'Step through each cell col-by-col for each row * For i = LBound(vRng) To UBound(vRng) *'For i = 1 To lRows * * For j = LBound(vRng, 2) To UBound(vRng, 2) *'For j = 1 To lCols * * * '//do stuff to cell in each col of Rows(i) * * Next 'j * Next 'i -OR- * 'Step through each cell row-by-row for each col * For i = LBound(vRng, 2) To UBound(vRng, 2) * * For j = LBound(vRng) To UBound(vRng) * * * '//do stuff to cell in each row of Columns(i) * * Next 'j * Next 'i -- Garry Free usenet access athttp://www.eternal-september.org ClassicVB Users Regroup! * * comp.lang.basic.visual.misc * * microsoft.public.vb.general.discussion Damn Garry... I feel like I should be paying for this. Thank you so much. It helps. Thank you. |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You're welcome! I hope it helps also...
-- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
"Center across selection" rather than "Merge cells" | Excel Discussion (Misc queries) | |||
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell | Excel Discussion (Misc queries) | |||
Splitting up one's highlighted selection as if you had used to"control" button for each selection | Excel Programming | |||
Icon for "Wrap" and for "Centre Across Selection" | Excel Worksheet Functions | |||
Help! Range("startsum").Value = Application.Sum(Selection) | Excel Programming |