Consulting

Results 1 to 5 of 5

Thread: Pass Sheet Codename as argument to Subroutine

  1. #1

    Pass Sheet Codename as argument to Subroutine

    Hey Y'all,

    I'm struggling with passing a worksheet codename through the a subroutine. Below is an example of what I have been trying to do. The intent is to have a single function that works on the called sheet.

    I'd like to be able to send the sheet codename instead of the name itself as that might get changed.

    When I pass the name, the function seems to work properly. However, when I try to use the codename....nope.


    Sub tryPassSheetName(wName$)
        Dim ws As Worksheet
        Set ws = ActiveWorkbook.Sheets(wName)
        Debug.Print ws.CodeName
    End Sub
    
    
    Sub testy()
        tryPassSheetName ("Sheet29") 'codename of the sheet (Does NOT work)
        tryPassSheetName ("TL List of Indicators") 'actual name of the sheet (Works only until user changes sheet name)
    End Sub
    Thoughts?

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    'Note that you must add trust for vbcomponent access:
    'File -> Options -> Trust Center -> Trust Center Setttings -> Macro Settings -> Trust Access to the VBA Project object model.
    Sub Test_SheetFromCodeName()
      Dim ws As Worksheet
      Set ws = SheetFromCodeName("Sheet1", ActiveWorkbook)
      MsgBox ws.Name
    End Sub
    
    
    'http://www.excelforum.com/excel-programming-vba-macros/968869-how-to-reference-a-sheet-with-codenames-and-variables.html
    'http://www.ozgrid.com/forum/showthread.php?t=188760
    Function SheetFromCodeName(aName As String, Optional wb As Workbook) As Worksheet
      If wb Is Nothing Then Set wb = ThisWorkbook
      With wb
        Set SheetFromCodeName = .Sheets(.VBProject.VBComponents(aName).Properties("Index"))
      End With
    End Function

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub Examples()
    Dim X As String
    X = "Test Sheet" 'The "Tab" name of the first sheet in my workbook
    
    MsgBox Sheets(1).Name
    MsgBox Sheets(X).Name
    MsgBox Sheet1.Name     'Sheet1 is the name of that Sheet Object and needs no other qualifiers.
    
    MsgBox Sheets(1).CodeName
    MsgBox Sheets(X).CodeName
    MsgBox Sheet1.CodeName
    
    End Sub
    Sub Example2()
    Dim Sht As Object
    
    For Each Sht In Sheets
    If Sht Is Sheet1 Then
    MsgBox Sht.Name
    MsgBox Sht.CodeName
    End If
    Next
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Have your Sub accept a sheet object OR a string - if it's a string assume it's the tab name of the sheet, if it's a sheet object treat it accordingly:
    Sub tryPassSheetName(wName)
    Dim ws As Worksheet
    If TypeName(wName) = "String" Then
      Set ws = Sheets(wName)
    Else
      Set ws = wName
    End If
    Debug.Print ws.CodeName, ws.Name
    End Sub
    
    Sub testy()
    tryPassSheetName Sheet3  'codename of the sheet (Does work)
    tryPassSheetName "Sheet2"  'name on the sheet's tab
    End Sub
    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
    p45cal, thanks.

Posting Permissions

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