+ Reply to Thread
Results 1 to 15 of 15

codename help

Hybrid View

  1. #1
    Gary Keramidas
    Guest

    codename help

    i want to loop through 12 sheets, codename sheet1 thru sheet12

    why doesn't this work?

    i = 1
    For i = 1 To 12
    cname = "Sheet" & i
    cname.Select
    Next

    --


    Gary




  2. #2
    Gary Keramidas
    Guest

    Re: codename help

    i ended up using index numbers instead
    Worksheets(i).Activate

    --


    Gary


    "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    news:%[email protected]...
    >i want to loop through 12 sheets, codename sheet1 thru sheet12
    >
    > why doesn't this work?
    >
    > i = 1
    > For i = 1 To 12
    > cname = "Sheet" & i
    > cname.Select
    > Next
    >
    > --
    >
    >
    > Gary
    >
    >
    >




  3. #3
    Carlos
    Guest

    Re: codename help

    Try

    dim cname as Worksheet
    For i = 1 To 12
    set cname = sheets("Sheet" & i)
    cname.Select
    Next



    "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    news:%[email protected]...
    >i want to loop through 12 sheets, codename sheet1 thru sheet12
    >
    > why doesn't this work?
    >
    > i = 1
    > For i = 1 To 12
    > cname = "Sheet" & i
    > cname.Select
    > Next
    >
    > --
    >
    >
    > Gary
    >
    >
    >




  4. #4
    Gary Keramidas
    Guest

    Re: codename help

    thanks carlos

    --


    Gary


    "Carlos" <[email protected]> wrote in message
    news:[email protected]...
    > Try
    >
    > dim cname as Worksheet
    > For i = 1 To 12
    > set cname = sheets("Sheet" & i)
    > cname.Select
    > Next
    >
    >
    >
    > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > news:%[email protected]...
    >>i want to loop through 12 sheets, codename sheet1 thru sheet12
    >>
    >> why doesn't this work?
    >>
    >> i = 1
    >> For i = 1 To 12
    >> cname = "Sheet" & i
    >> cname.Select
    >> Next
    >>
    >> --
    >>
    >>
    >> Gary
    >>
    >>
    >>

    >
    >




  5. #5
    Gary Keramidas
    Guest

    Re: codename help

    i think that only works for the sheet name, not the codename

    --


    Gary


    "Carlos" <[email protected]> wrote in message
    news:[email protected]...
    > Try
    >
    > dim cname as Worksheet
    > For i = 1 To 12
    > set cname = sheets("Sheet" & i)
    > cname.Select
    > Next
    >
    >
    >
    > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > news:%[email protected]...
    >>i want to loop through 12 sheets, codename sheet1 thru sheet12
    >>
    >> why doesn't this work?
    >>
    >> i = 1
    >> For i = 1 To 12
    >> cname = "Sheet" & i
    >> cname.Select
    >> Next
    >>
    >> --
    >>
    >>
    >> Gary
    >>
    >>
    >>

    >
    >




  6. #6
    Tom Ogilvy
    Guest

    Re: codename help

    That is correct Gary.

    Although changeable, the usage for codename assumes it isn't

    The alternative (to support variability or to get a list) is to loop through
    the objects in the project and pick up the codename property.

    See Chip Pearson's code on programming the VBE for insights
    http://www.cpearson.com/excel/vbe.htm

    and codenames specifically:
    http://www.cpearson.com/excel/codemods.htm

    although if you are populating a combobox with sheetnames

    for each sh in Worksheets
    .AddItem sh.name
    Next


    --
    Regards,
    Tom Ogilvy

    "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    news:[email protected]...
    > i think that only works for the sheet name, not the codename
    >
    > --
    >
    >
    > Gary
    >
    >
    > "Carlos" <[email protected]> wrote in message
    > news:[email protected]...
    > > Try
    > >
    > > dim cname as Worksheet
    > > For i = 1 To 12
    > > set cname = sheets("Sheet" & i)
    > > cname.Select
    > > Next
    > >
    > >
    > >
    > > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > > news:%[email protected]...
    > >>i want to loop through 12 sheets, codename sheet1 thru sheet12
    > >>
    > >> why doesn't this work?
    > >>
    > >> i = 1
    > >> For i = 1 To 12
    > >> cname = "Sheet" & i
    > >> cname.Select
    > >> Next
    > >>
    > >> --
    > >>
    > >>
    > >> Gary
    > >>
    > >>
    > >>

    > >
    > >

    >
    >




  7. #7
    Gary Keramidas
    Guest

    Re: codename help

    thanks for the tip.

    --


    Gary


    "Tom Ogilvy" <[email protected]> wrote in message
    news:[email protected]...
    > That is correct Gary.
    >
    > Although changeable, the usage for codename assumes it isn't
    >
    > The alternative (to support variability or to get a list) is to loop
    > through
    > the objects in the project and pick up the codename property.
    >
    > See Chip Pearson's code on programming the VBE for insights
    > http://www.cpearson.com/excel/vbe.htm
    >
    > and codenames specifically:
    > http://www.cpearson.com/excel/codemods.htm
    >
    > although if you are populating a combobox with sheetnames
    >
    > for each sh in Worksheets
    > .AddItem sh.name
    > Next
    >
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > news:[email protected]...
    >> i think that only works for the sheet name, not the codename
    >>
    >> --
    >>
    >>
    >> Gary
    >>
    >>
    >> "Carlos" <[email protected]> wrote in message
    >> news:[email protected]...
    >> > Try
    >> >
    >> > dim cname as Worksheet
    >> > For i = 1 To 12
    >> > set cname = sheets("Sheet" & i)
    >> > cname.Select
    >> > Next
    >> >
    >> >
    >> >
    >> > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    >> > news:%[email protected]...
    >> >>i want to loop through 12 sheets, codename sheet1 thru sheet12
    >> >>
    >> >> why doesn't this work?
    >> >>
    >> >> i = 1
    >> >> For i = 1 To 12
    >> >> cname = "Sheet" & i
    >> >> cname.Select
    >> >> Next
    >> >>
    >> >> --
    >> >>
    >> >>
    >> >> Gary
    >> >>
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>

    >
    >




  8. #8
    Bruno Campanini
    Guest

    Re: codename help

    "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    news:%[email protected]...
    >i want to loop through 12 sheets, codename sheet1 thru sheet12
    >
    > why doesn't this work?
    >
    > i = 1
    > For i = 1 To 12
    > cname = "Sheet" & i
    > cname.Select
    > Next
    >
    > --
    >
    >
    > Gary


    This works:

    ---------------------------
    Sub Button4_Click()
    Dim i As Integer
    For i = 1 To 4
    Sheets("Sheet" & i).Activate
    MsgBox ActiveSheet.Name
    Next
    End Sub
    -------------------------

    Ciao
    Bruno



  9. #9
    Bob Phillips
    Guest

    Re: codename help

    Sub RenameSheets()
    Dim sSheet As String
    Dim oVBMod As Object
    Dim i As Long

    With ActiveWorkbook.VBProject
    For Each oVBMod In .VBComponents
    Select Case oVBMod.Type
    Case 100:
    If oVBMod.Name <> "ThisWorkbook" Then
    sSheet =
    CStr(.VBComponents(oVBMod.Properties("Codename")).Properties("Name"))
    With Worksheets(sSheet)
    i = i + 1
    .Parent.VBProject.VBComponents(.CodeName) _
    .Properties("_CodeName") = "Sheet" & i
    'For i = 1 To oVBMod.Properties.Count
    ' Debug.Print oVBMod.Properties(i).Name
    'Next i
    End With
    End If
    End Select
    Next oVBMod
    End With

    End Sub


    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    news:%[email protected]...
    > i want to loop through 12 sheets, codename sheet1 thru sheet12
    >
    > why doesn't this work?
    >
    > i = 1
    > For i = 1 To 12
    > cname = "Sheet" & i
    > cname.Select
    > Next
    >
    > --
    >
    >
    > Gary
    >
    >
    >




  10. #10
    Gary Keramidas
    Guest

    Re: codename help

    thanks for your help on this, bob

    --


    Gary


    "Bob Phillips" <[email protected]> wrote in message
    news:[email protected]...
    > Sub RenameSheets()
    > Dim sSheet As String
    > Dim oVBMod As Object
    > Dim i As Long
    >
    > With ActiveWorkbook.VBProject
    > For Each oVBMod In .VBComponents
    > Select Case oVBMod.Type
    > Case 100:
    > If oVBMod.Name <> "ThisWorkbook" Then
    > sSheet =
    > CStr(.VBComponents(oVBMod.Properties("Codename")).Properties("Name"))
    > With Worksheets(sSheet)
    > i = i + 1
    > .Parent.VBProject.VBComponents(.CodeName) _
    > .Properties("_CodeName") = "Sheet" & i
    > 'For i = 1 To oVBMod.Properties.Count
    > ' Debug.Print oVBMod.Properties(i).Name
    > 'Next i
    > End With
    > End If
    > End Select
    > Next oVBMod
    > End With
    >
    > End Sub
    >
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > news:%[email protected]...
    >> i want to loop through 12 sheets, codename sheet1 thru sheet12
    >>
    >> why doesn't this work?
    >>
    >> i = 1
    >> For i = 1 To 12
    >> cname = "Sheet" & i
    >> cname.Select
    >> Next
    >>
    >> --
    >>
    >>
    >> Gary
    >>
    >>
    >>

    >
    >




  11. #11
    Tom Ogilvy
    Guest

    Re: codename help

    This code seems like your going in circles.

    If you already have a reference to the component (sheet), then why use it to
    find the sheet tab name to use to get a reference to the component which you
    already have?

    --
    Regards,
    Tom Ogilvy


    "Bob Phillips" <[email protected]> wrote in message
    news:[email protected]...
    > Sub RenameSheets()
    > Dim sSheet As String
    > Dim oVBMod As Object
    > Dim i As Long
    >
    > With ActiveWorkbook.VBProject
    > For Each oVBMod In .VBComponents
    > Select Case oVBMod.Type
    > Case 100:
    > If oVBMod.Name <> "ThisWorkbook" Then
    > sSheet =
    > CStr(.VBComponents(oVBMod.Properties("Codename")).Properties("Name"))
    > With Worksheets(sSheet)
    > i = i + 1
    > .Parent.VBProject.VBComponents(.CodeName) _
    > .Properties("_CodeName") = "Sheet" & i
    > 'For i = 1 To oVBMod.Properties.Count
    > ' Debug.Print oVBMod.Properties(i).Name
    > 'Next i
    > End With
    > End If
    > End Select
    > Next oVBMod
    > End With
    >
    > End Sub
    >
    >
    > --
    >
    > HTH
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > news:%[email protected]...
    > > i want to loop through 12 sheets, codename sheet1 thru sheet12
    > >
    > > why doesn't this work?
    > >
    > > i = 1
    > > For i = 1 To 12
    > > cname = "Sheet" & i
    > > cname.Select
    > > Next
    > >
    > > --
    > >
    > >
    > > Gary
    > >
    > >
    > >

    >
    >




  12. #12
    Chip Pearson
    Guest

    Re: codename help

    If you really need the CodeName, not the Sheet name, try
    something like

    Dim N As Integer
    For N = 1 To 3
    Debug.Print ThisWorkbook.VBProject.VBComponents("Sheet" &
    N).Name
    Next N




    "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    news:%[email protected]...
    >i want to loop through 12 sheets, codename sheet1 thru sheet12
    >
    > why doesn't this work?
    >
    > i = 1
    > For i = 1 To 12
    > cname = "Sheet" & i
    > cname.Select
    > Next
    >
    > --
    >
    >
    > Gary
    >
    >
    >




  13. #13
    Gary Keramidas
    Guest

    Re: codename help

    chip:

    what i was trying to do was loop all sheets based on the code name. the
    sheet names were all changed, but i knew the code names were sheet1 thru
    sheet 12. i was trying to concatenate the number onto the end of the word
    sheet so i could loop.
    i used index number instead, the index numbers are 2 thru 13. just wanted to
    use code names in case any sheets were inserted.

    i used the index (i) variable because i could not figure out how to select
    the sheet(i) by code name.

    For i = 2 To 13
    Worksheets(i).Activate
    ActiveSheet.Unprotect
    Range("b4:i56,k4:p56,r4:t56,v4:af56").ClearContents
    ActiveSheet.Protect
    Range("a1").Select
    Next

    --


    Gary


    "Chip Pearson" <[email protected]> wrote in message
    news:%[email protected]...
    > If you really need the CodeName, not the Sheet name, try something like
    >
    > Dim N As Integer
    > For N = 1 To 3
    > Debug.Print ThisWorkbook.VBProject.VBComponents("Sheet" & N).Name
    > Next N
    >
    >
    >
    >
    > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > news:%[email protected]...
    >>i want to loop through 12 sheets, codename sheet1 thru sheet12
    >>
    >> why doesn't this work?
    >>
    >> i = 1
    >> For i = 1 To 12
    >> cname = "Sheet" & i
    >> cname.Select
    >> Next
    >>
    >> --
    >>
    >>
    >> Gary
    >>
    >>
    >>

    >
    >




  14. #14
    Tom Ogilvy
    Guest

    Re: codename help

    If the code names are sheet2 to sheet13 then

    Sub ABC()
    Dim i As Long, sh As Object
    Dim sStr As String
    For i = 2 To 13
    sStr = ThisWorkbook.VBProject _
    .VBComponents("Sheet" & i) _
    .Properties("Name").Value
    MsgBox "Tab name of Sheet" & i & _
    " is " & sStr
    Next

    End Sub

    --
    Regards,
    Tom Ogilvy


    "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    news:[email protected]...
    > chip:
    >
    > what i was trying to do was loop all sheets based on the code name. the
    > sheet names were all changed, but i knew the code names were sheet1 thru
    > sheet 12. i was trying to concatenate the number onto the end of the word
    > sheet so i could loop.
    > i used index number instead, the index numbers are 2 thru 13. just wanted

    to
    > use code names in case any sheets were inserted.
    >
    > i used the index (i) variable because i could not figure out how to select
    > the sheet(i) by code name.
    >
    > For i = 2 To 13
    > Worksheets(i).Activate
    > ActiveSheet.Unprotect
    > Range("b4:i56,k4:p56,r4:t56,v4:af56").ClearContents
    > ActiveSheet.Protect
    > Range("a1").Select
    > Next
    >
    > --
    >
    >
    > Gary
    >
    >
    > "Chip Pearson" <[email protected]> wrote in message
    > news:%[email protected]...
    > > If you really need the CodeName, not the Sheet name, try something like
    > >
    > > Dim N As Integer
    > > For N = 1 To 3
    > > Debug.Print ThisWorkbook.VBProject.VBComponents("Sheet" & N).Name
    > > Next N
    > >
    > >
    > >
    > >
    > > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > > news:%[email protected]...
    > >>i want to loop through 12 sheets, codename sheet1 thru sheet12
    > >>
    > >> why doesn't this work?
    > >>
    > >> i = 1
    > >> For i = 1 To 12
    > >> cname = "Sheet" & i
    > >> cname.Select
    > >> Next
    > >>
    > >> --
    > >>
    > >>
    > >> Gary
    > >>
    > >>
    > >>

    > >
    > >

    >
    >




  15. #15
    Gary Keramidas
    Guest

    Re: codename help

    ok, thanks for that, tom. i'll file it away

    --


    Gary


    "Tom Ogilvy" <[email protected]> wrote in message
    news:%[email protected]...
    > If the code names are sheet2 to sheet13 then
    >
    > Sub ABC()
    > Dim i As Long, sh As Object
    > Dim sStr As String
    > For i = 2 To 13
    > sStr = ThisWorkbook.VBProject _
    > .VBComponents("Sheet" & i) _
    > .Properties("Name").Value
    > MsgBox "Tab name of Sheet" & i & _
    > " is " & sStr
    > Next
    >
    > End Sub
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    > news:[email protected]...
    >> chip:
    >>
    >> what i was trying to do was loop all sheets based on the code name. the
    >> sheet names were all changed, but i knew the code names were sheet1 thru
    >> sheet 12. i was trying to concatenate the number onto the end of the word
    >> sheet so i could loop.
    >> i used index number instead, the index numbers are 2 thru 13. just wanted

    > to
    >> use code names in case any sheets were inserted.
    >>
    >> i used the index (i) variable because i could not figure out how to
    >> select
    >> the sheet(i) by code name.
    >>
    >> For i = 2 To 13
    >> Worksheets(i).Activate
    >> ActiveSheet.Unprotect
    >> Range("b4:i56,k4:p56,r4:t56,v4:af56").ClearContents
    >> ActiveSheet.Protect
    >> Range("a1").Select
    >> Next
    >>
    >> --
    >>
    >>
    >> Gary
    >>
    >>
    >> "Chip Pearson" <[email protected]> wrote in message
    >> news:%[email protected]...
    >> > If you really need the CodeName, not the Sheet name, try something like
    >> >
    >> > Dim N As Integer
    >> > For N = 1 To 3
    >> > Debug.Print ThisWorkbook.VBProject.VBComponents("Sheet" & N).Name
    >> > Next N
    >> >
    >> >
    >> >
    >> >
    >> > "Gary Keramidas" <GKeramidasATmsn.com> wrote in message
    >> > news:%[email protected]...
    >> >>i want to loop through 12 sheets, codename sheet1 thru sheet12
    >> >>
    >> >> why doesn't this work?
    >> >>
    >> >> i = 1
    >> >> For i = 1 To 12
    >> >> cname = "Sheet" & i
    >> >> cname.Select
    >> >> Next
    >> >>
    >> >> --
    >> >>
    >> >>
    >> >> Gary
    >> >>
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1