+ Reply to Thread
Results 1 to 7 of 7

Extracting a avialble Tab Name from a workbook

  1. #1
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Extracting a avialble Tab Name from a workbook

    Hi All,

    From this sites I learn lots of things and thanks Excel Forum by this site v r learning lots of new thing.

    I have an issue in my below coding.
    My question is -- I make below code to extract available TAB names from a workbook and its working nicely but printing all the TAB Names from a workbook its showing "Script out of range, Run time error - 9" and I am Zero in understanding the Run time error and please explain me what is this run time error - 9 why this number showing as a run time error.

    Sub GetShtNam()
    Dim ShtNam As String
    Dim wsindex As Integer
    mrow = 2
    wsindex = 1
    Range("A2:A" & ActiveSheet.UsedRange.Rows.Count).Select
    Selection.ClearContents
    Range("A1").Select
    For i = 1 To 254
    If Worksheets(wsindex).Name = "" Then
    Range("A1").Select
    Exit Sub
    Else
    ShtNam = Worksheets(wsindex).Name
    Cells(mrow, 1).Value = ShtNam
    mrow = mrow + 1
    wsindex = wsindex + 1
    End If
    Next
    End Sub


    Can anyone please help me out and please explain with single quotation in script page so that i can understand easily.

    Thanks in Advance !

    Thanks,
    Naveed.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,099

    Re: Extracting a avialble Tab Name from a workbook

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,099

    Re: Extracting a avialble Tab Name from a workbook

    Or:

    Please Login or Register  to view this content.

    Regards, TMS

  4. #4
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Extracting a avialble Tab Name from a workbook

    Thanks alot sir ,thnks for ur reply.

    I like ur way of making code and please have small review on my below after spending 30mins i successfully figure out the code.

    Please have review and please let me know is it 100% accurately work or not ?

    Sub GetTabNam()
    Dim TabNam As String
    Dim wsindex As Integer
    mrow = 2
    wsindex = 1
    wsindex2 = ActiveWorkbook.Worksheets.Count
    Range("A2:A" & ActiveSheet.UsedRange.Rows.Count).Select
    Selection.ClearContents
    Range("A1").Select
    For i = wsindex To wsindex2
    TabNam = Worksheets(wsindex).Name
    Cells(mrow, 1).Value = TabNam
    mrow = mrow + 1
    wsindex = wsindex + 1
    Next
    MsgBox "All Available Tab Name Extract ! "
    End Sub

    Thanks in Advance !

    Thanks,
    MNR.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,099

    Re: Extracting a avialble Tab Name from a workbook

    I have, effectively, already reviewed your code and provided two shorter and more efficient versions of code to achieve the required objective.

    If your code works, and it does what you want it to do, I'm not sure what you want me to say.

    How can I break it gently? You have variables that you don't really need as you set them and only use them once in the next line, eg, Tabnam. You have separate counters for the worksheet index, the row where you store the Tab Name and the loop counter; you could use just one. And you change the base value of the loop within the loop, that is, wsindex, which is not recommended. Finally, you use ".Select ... .Selection." which is to be avoided.


    Please note that, to comply with forum rules, you should add code tags to your code examples.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  6. #6
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Extracting a avialble Tab Name from a workbook

    I am extremely SORRY sir,

    As i am not that much good in VBA Macro and in which way I learn Macros i tried.

    Sorry sir and thanks for pointing out the differents in defining variables and in future i will not make this type mistakes in defining variables.

    Once again Thanks for this explanation and Sorry as well.

    Thanks - Naveed.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,099

    Re: Extracting a avialble Tab Name from a workbook

    There's no need to apologise. We all have to learn, and often we learn best by making mistakes. I have to be honest and say I don't like/enjoy learning by my mistakes but, generally, you remember and don't do it again.

    I've been a programmer fo 40 years, I still make mistakes and I learn something new pretty much every day.

    And I prefer to see someone come to the forum with code they've tried to write and ask for help, rather than just say "I want/need this, can you write it for me?"

    So, good luck with your coding ... and don't be put off if it's not right first time

    Regards, TMS


    PS: please don't forget the code tags and marking the thread as solved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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