sheet list to return exact name without any trim

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kkshansid
    New Member
    • Oct 2008
    • 232

    #1

    sheet list to return exact name without any trim

    Code:
    Sub SheetNames()
        Columns(1).Insert
        For i = 1 To Sheets.Count
            Cells(i, 1) = Sheets(i).Name
        Next i
    End Sub
    This function returns sheet names very well but i want exact name without any trim for example for sheet name 0085 it returns 85 but i need same name to be returned i used cstr function but that still returns the same 85
    kindly help
    access 2007
    Thanx in advance
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Hi

    I thing this may solve the proble
    Code:
    Sub SheetNames() 
        Columns(1).Insert
        For i = 1 To Sheets.Count
            Cells(i, 1).NumberFormat = "@"
            Cells(i, 1) = Sheets(i).Name
        Next iEnd Sub
    In short, the string is returned to the spreadsheet but the sheet then assumes it is a number, so you need to change the format to the Cell, in this example it formats it as text.

    Alternatively you could format the cell with 4 leading zeros ie
    Code:
    Columns(1).Insert
        For i = 1 To Sheets.Count
            Cells(i, 1).NumberFormat = "0000"
            Cells(i, 1) = Sheets(i).Name
        Next i
    HTH


    MTB
    Last edited by MikeTheBike; Jun 13 '12, 11:22 AM. Reason: More Info

    Comment

    Working...