Consulting

Results 1 to 16 of 16

Thread: what type of sheet

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    what type of sheet

    hello
    it all began when i wanted to create udf that would tell me wheter a worksheet exists.
    [VBA]Function sheetme(shtname) As Boolean
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    If ws.name = shtname Then
    sheetme = True
    End If
    Next
    End Function
    [/VBA]
    then i found that my founction could not deal with charts.so i added another udf to tell me wheter i look at a worksheet or a chart sheet
    [VBA]
    Function prototype(shtname) As String
    prototype = TypeName(shtname)
    End Function
    [/VBA]
    can any one tell me what wrong with my functions?.
    moshe

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    I would guess you want:
    [VBA]Function prototype(shtname) As String
    prototype = TypeName(Sheets(shtname))
    End Function [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    With the original code you'll only be looking at worksheets.

    Charts aren't included in the Worksheets collection.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by Norie
    With the original code you'll only be looking at worksheets.

    Charts aren't included in the Worksheets collection.
    ...but are included in the Sheets collection. So if you change your line:
    [vba]For Each ws In ActiveWorkbook.Worksheets[/vba] to
    [vba]For Each ws In ActiveWorkbook.Sheets[/vba] it might do what you want?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can avoid looping
    [VBA]Sub test()
    MsgBox SheetExists("Chart1")
    End Sub

    Function SheetExists(sh) As Boolean
    Dim i
    On Error Resume Next
    Set i = Sheets(sh)
    If Not IsEmpty(i) Then SheetExists = True
    End Function

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This function should do what you want.
    Function sheetExists(sheetName As String) As Boolean
    On Error Resume Next
    sheetExists = (ThisWorkbook.Sheets(sheetName).Name = sheetName)
    On Error GoTo 0
    End Function

  7. #7
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    what is wrong with
    [VBA]
    Sub sheetlocator()
    On Error GoTo err
    Dim sheetname As Object
    sheetname = InputBox("enter vsheet name")
    If sheetme(sheetname) = True Then
    sheetname.Activate
    End If
    Exit Sub
    err:
    MsgBox " sheet do not exists", vbOKOnly + vbInformation, "wrong"
    End Sub

    [/VBA]
    moshe

  8. #8
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    InputBox is returning a string, not an object. You should declare sheetname as a string and use Sheets(sheetname).Activate, not sheetname.Activate
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    [VBA]Private Sub CommandButton1_Click()
    Dim i As Integer, sht As String
    For i = 0 To ListBox1.ListCount - 1
    If ListBox1.selected(i) = True Then
    sht = ListBox1.List(i)
    End If
    Next i
    Sheets(sht).Activate
    End
    End Sub
    Private Sub CommandButton2_Click()
    UserForm10.hide
    End Sub
    Private Sub UserForm_Activate()
    ListBox1.Value = ""
    UserForm10.Caption = "navigator" & " - " & ActiveWorkbook.FullName
    ListBox1.Value = ActiveSheet.name
    End Sub
    Private Sub UserForm_Deactivate()
    UserForm10.Caption = ""
    End Sub
    Private Sub UserForm_Initialize()
    ListBox1.Value = ""
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    ListBox1.AddItem (ws.name)
    Next ws
    End Sub
    Private Sub UserForm_QueryClose(cancel As Integer, CloseMode As Integer)
    cancel = True
    UserForm10.Caption = "The Close box won't work! Click cancel!"
    End Sub
    [/VBA]
    the code above is a userform for a list of all sheets in a workbook.how can i get the userform open on the activesheet being highlight in the lostbox?
    where do all chart sheet diaappeared?
    thanks
    moshe

  10. #10
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    As has been mentioned before, this only looks at worksheets:
    [VBA] For Each ws In ActiveWorkbook.Worksheets
    ListBox1.AddItem (ws.name)
    Next ws[/VBA]
    you need to declare ws as Object and look at the Sheets collection.

    Is your listbox a multiselect one? (I am guessing it is from your code)
    Regards,
    Rory

    Microsoft MVP - Excel

  11. #11
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You
    Regards,
    Rory

    Microsoft MVP - Excel

  12. #12
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You can use
    Regards,
    Rory

    Microsoft MVP - Excel

  13. #13
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You can use something like:
    [VBA] Dim n As Integer
    For n = 1 To Sheets.Count
    With ListBox1
    .AddItem Sheets(n).Name, n - 1
    If Sheets(n) Is ActiveSheet Then .Selected(n - 1) = True
    End With
    Next n
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  14. #14
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello rory
    i change my ws to object.i keep getting error 380.ithappen everytime i open another wb and trying to get ws list .may be my activate event is wrong?
    [VBA]
    Private Sub UserForm_Activate()
    ListBox1.Value = ""
    UserForm10.Caption = "navigator" & " - " & ActiveWorkbook.FullName
    ListBox1.Value = ActiveSheet.name
    End Sub

    [/VBA]
    thanks
    moshe

  15. #15
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You can't use the Value property if your listbox is Multiselect; that's why I had this line in my code:
    [VBA]If Sheets(n) Is ActiveSheet Then .Selected(n - 1) = True [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  16. #16
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why do you need/want to activate multiple sheets?

    What are youy actually trying to do?

Posting Permissions

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