Consulting

Results 1 to 9 of 9

Thread: Solved: activesheet.type always 3?

  1. #1
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location

    Solved: activesheet.type always 3?

    debug.print activesheet.type always gives 3, which means xlExcel4MacroSheet (the xlSheetType values are xlChart, xlDialogSheet, xlExcel4IntlMacroSheet, xlExcel4MacroSheet, xlWorksheet).

    I'm trying to identify whether the current sheet is a regular "Sheet1" worksheet, as opposed to a "Chart1" chart (which you get from hitting the F11 key). What am I doing wrong, or should I do instead?

    (P.S. - I thought that this was an unfortunate but logical outcome from being in the code window as I observed this (i.e., ?activesheet.type in the Immediate window). But it also gives 3 when running the macro "full speed" from alt-F8.)
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  2. #2
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Well... I may be missing something here but I bound a macro to Ctrl + Q that runs the code:
    [VBA]Debug.Print ActiveSheet.Type[/VBA]

    Whenever I'm on a chart it does indeed return '3'. When I am on a regular worksheet it returns '-4167'

    So I'm getting two different values. Does this match what you're getting? Am I missing the point?
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  3. #3
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    Those are exactly the results I'm getting. CORRECTION TO ORIGINAL POST: it gets result 3 on all CHARTS, and worksheets give -4167 (which is xlWorksheet).

    It would seem that the correct result for charts should not be 3 but should be xlChart or -4109. I'll assume for now I'll assume that regular worksheets work as expected giving xlWorksheet; and that, like it or not, charts give xlExcel4MacroSheet (3) instead of xlChart. But this is still a mystery to me.
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  4. #4
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Well... if that's the result you want you can always use:

    [VBA]Dim result

    result = ActiveSheet.Type
    Select Case result
    Case 3
    Debug.Print "xlChart"
    Case -4167
    Debug.Print "xlWorksheet"
    End Select[/VBA]

    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This is a known bug. Typename is more reliable

    [vba]

    Select Case TypeName(ActiveSheet)

    Case "Chart": MsgBox "Chartsheet"

    Case "Worksheet": MsgBox "Worksheet"
    End Select
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    Thanks Randy - that's what I've resigned to, excepting further explanation. I still prefer to used the named constants for classic reasons; in this case it means coding xlExcel4MacroSheet which seems absurd.

    Either there's something else to this or maybe it's simply a bug.
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  7. #7
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    Okay xld, just seeing your reply. FYI this is XL 2003. On further research, I've seen others suggest to use Typename. I also saw another way at http://tinyurl.com/54ya6v but it is very awkward, doing

    For Each oChart In Charts
    If oChart.Name = Sheets(iCount).Name

    which I assume is unnecessary if you just use TypeName. Way to go, remembering that and sharing it!
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That does seem a tad convoluted doesn't it. I'll stick to Typename :-)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location
    The following shows TypeOf to give superior speed over typename on my machine. I ran this once with a chart as activesheet and once without
    Sub Test_typename_speed_vs_typeof()
    Dim tStart As Single, i As Long, bBool As Boolean
        tStart = Timer()
        For i = 1 To 5000000#: bBool = TypeName$(ActiveSheet) = "Chart": Next
        Debug.Print "typename " & Timer() - tStart
        
        tStart = Timer()
        For i = 1 To 5000000#: bBool = TypeName$(ActiveSheet) = "Worksheet": Next
        Debug.Print "typename2 " & Timer() - tStart
        
        tStart = Timer()
        For i = 1 To 5000000#: bBool = TypeOf ActiveSheet Is Chart: Next
        Debug.Print "typeof " & Timer() - tStart
        
        tStart = Timer()
        For i = 1 To 5000000#: bBool = TypeOf ActiveSheet Is Worksheet: Next
        Debug.Print "typeof2 " & Timer() - tStart
    End Sub
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •