+ Reply to Thread
Results 1 to 7 of 7

Copy worksheet name into cell A1?

  1. #1
    Registered User
    Join Date
    08-18-2006
    Posts
    68

    Copy worksheet name into cell A1?

    Ok, so I just want to copy the name from a worksheet into the cell A1, I need this to work relative across 20 worksheets. Also in the same macro is it possible to delete the last 4 characters in the worksheet name?
    The worksheet was originally named from the file name and so has .xls at the end of the name.
    Any help gratefully appreciated.
    Patrick

  2. #2
    Norman Jones
    Guest

    Re: Copy worksheet name into cell A1?

    Hi Patrick,

    Try something like:

    '=============>>
    Public Sub Tester()
    Dim WB As Workbook
    Dim SH As Worksheet

    Set WB = Workbooks("YourBook.xls") '<<==== CHANGE

    For Each SH In WB.Worksheets
    With SH
    .Name = Left(.Name, Len(.Name) - 4)
    .Range("A1").Value = .Name
    End With
    Next SH
    End Sub
    '<<=============


    ---
    Regards,
    Norman


    "crowdx42" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Ok, so I just want to copy the name from a worksheet into the cell A1, I
    > need this to work relative across 20 worksheets. Also in the same macro
    > is it possible to delete the last 4 characters in the worksheet name?
    > The worksheet was originally named from the file name and so has .xls
    > at the end of the name.
    > Any help gratefully appreciated.
    > Patrick
    >
    >
    > --
    > crowdx42
    > ------------------------------------------------------------------------
    > crowdx42's Profile:
    > http://www.excelforum.com/member.php...o&userid=37749
    > View this thread: http://www.excelforum.com/showthread...hreadid=573657
    >




  3. #3
    NickHK
    Guest

    Re: Copy worksheet name into cell A1?

    One way is with a UDF:
    Public Function WSName(AnyCell As Range) As String
    With Application
    .Volatile
    WSName = .Caller.Parent.Name
    End With
    End Function

    As for the name change:
    With Worksheets(x)
    .Name = Left(.Name, Len(.Name) - 4)
    End With

    NickHK

    "crowdx42" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Ok, so I just want to copy the name from a worksheet into the cell A1, I
    > need this to work relative across 20 worksheets. Also in the same macro
    > is it possible to delete the last 4 characters in the worksheet name?
    > The worksheet was originally named from the file name and so has .xls
    > at the end of the name.
    > Any help gratefully appreciated.
    > Patrick
    >
    >
    > --
    > crowdx42
    > ------------------------------------------------------------------------
    > crowdx42's Profile:

    http://www.excelforum.com/member.php...o&userid=37749
    > View this thread: http://www.excelforum.com/showthread...hreadid=573657
    >




  4. #4
    NickHK
    Guest

    Re: Copy worksheet name into cell A1?

    Norman,
    That's what I thought first, but it is not possible to have the 20 sheets
    named after the WB, as they would conflict.
    So maybe 1 sheet is called that but not the other 19. In which case, you
    only need to rename 1 sheet.
    But depends what the OP means...

    NickHK

    "Norman Jones" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Patrick,
    >
    > Try something like:
    >
    > '=============>>
    > Public Sub Tester()
    > Dim WB As Workbook
    > Dim SH As Worksheet
    >
    > Set WB = Workbooks("YourBook.xls") '<<==== CHANGE
    >
    > For Each SH In WB.Worksheets
    > With SH
    > .Name = Left(.Name, Len(.Name) - 4)
    > .Range("A1").Value = .Name
    > End With
    > Next SH
    > End Sub
    > '<<=============
    >
    >
    > ---
    > Regards,
    > Norman
    >
    >
    > "crowdx42" <[email protected]> wrote

    in
    > message news:[email protected]...
    > >
    > > Ok, so I just want to copy the name from a worksheet into the cell A1, I
    > > need this to work relative across 20 worksheets. Also in the same macro
    > > is it possible to delete the last 4 characters in the worksheet name?
    > > The worksheet was originally named from the file name and so has .xls
    > > at the end of the name.
    > > Any help gratefully appreciated.
    > > Patrick
    > >
    > >
    > > --
    > > crowdx42
    > > ------------------------------------------------------------------------
    > > crowdx42's Profile:
    > > http://www.excelforum.com/member.php...o&userid=37749
    > > View this thread:

    http://www.excelforum.com/showthread...hreadid=573657
    > >

    >
    >




  5. #5
    Norman Jones
    Guest

    Re: Copy worksheet name into cell A1?

    Hi Nick,

    > That's what I thought first, but it is not possible to have the 20 sheets
    > named after the WB, as they would conflict.


    My suggested code changes the existing sheet names and merely shortens
    previously valid names.


    ---
    Regards,
    Norman



    "NickHK" <[email protected]> wrote in message
    news:[email protected]...
    > Norman,
    > That's what I thought first, but it is not possible to have the 20 sheets
    > named after the WB, as they would conflict.
    > So maybe 1 sheet is called that but not the other 19. In which case, you
    > only need to rename 1 sheet.
    > But depends what the OP means...
    >
    > NickHK
    >
    > "Norman Jones" <[email protected]> wrote in message
    > news:[email protected]...
    >> Hi Patrick,
    >>
    >> Try something like:
    >>
    >> '=============>>
    >> Public Sub Tester()
    >> Dim WB As Workbook
    >> Dim SH As Worksheet
    >>
    >> Set WB = Workbooks("YourBook.xls") '<<==== CHANGE
    >>
    >> For Each SH In WB.Worksheets
    >> With SH
    >> .Name = Left(.Name, Len(.Name) - 4)
    >> .Range("A1").Value = .Name
    >> End With
    >> Next SH
    >> End Sub
    >> '<<=============
    >>
    >>
    >> ---
    >> Regards,
    >> Norman
    >>
    >>
    >> "crowdx42" <[email protected]> wrote

    > in
    >> message news:[email protected]...
    >> >
    >> > Ok, so I just want to copy the name from a worksheet into the cell A1,
    >> > I
    >> > need this to work relative across 20 worksheets. Also in the same macro
    >> > is it possible to delete the last 4 characters in the worksheet name?
    >> > The worksheet was originally named from the file name and so has .xls
    >> > at the end of the name.
    >> > Any help gratefully appreciated.
    >> > Patrick
    >> >
    >> >
    >> > --
    >> > crowdx42
    >> > ------------------------------------------------------------------------
    >> > crowdx42's Profile:
    >> > http://www.excelforum.com/member.php...o&userid=37749
    >> > View this thread:

    > http://www.excelforum.com/showthread...hreadid=573657
    >> >

    >>
    >>

    >
    >




  6. #6
    Norman Jones
    Guest

    Re: Copy worksheet name into cell A1?

    Hi Nick,

    Just to add and to guess at a possible scenario, the OP may have named the
    sheets in a fashion resembling:

    '=============>>
    Public Sub Tester()
    Dim SH As Worksheet
    Dim i As Long
    Const myPrefix As String = "ABC"

    For Each SH In ActiveWorkbook.Worksheets
    i = i + 1
    SH.Name = i & ActiveWorkbook.Name
    Next SH

    End Sub
    '<<=============


    ---
    Regards,
    Norman



    "NickHK" <[email protected]> wrote in message
    news:[email protected]...
    > Norman,
    > That's what I thought first, but it is not possible to have the 20 sheets
    > named after the WB, as they would conflict.
    > So maybe 1 sheet is called that but not the other 19. In which case, you
    > only need to rename 1 sheet.
    > But depends what the OP means...
    >
    > NickHK
    >
    > "Norman Jones" <[email protected]> wrote in message
    > news:[email protected]...
    >> Hi Patrick,
    >>
    >> Try something like:
    >>
    >> '=============>>
    >> Public Sub Tester()
    >> Dim WB As Workbook
    >> Dim SH As Worksheet
    >>
    >> Set WB = Workbooks("YourBook.xls") '<<==== CHANGE
    >>
    >> For Each SH In WB.Worksheets
    >> With SH
    >> .Name = Left(.Name, Len(.Name) - 4)
    >> .Range("A1").Value = .Name
    >> End With
    >> Next SH
    >> End Sub
    >> '<<=============
    >>
    >>
    >> ---
    >> Regards,
    >> Norman
    >>
    >>
    >> "crowdx42" <[email protected]> wrote

    > in
    >> message news:[email protected]...
    >> >
    >> > Ok, so I just want to copy the name from a worksheet into the cell A1,
    >> > I
    >> > need this to work relative across 20 worksheets. Also in the same macro
    >> > is it possible to delete the last 4 characters in the worksheet name?
    >> > The worksheet was originally named from the file name and so has .xls
    >> > at the end of the name.
    >> > Any help gratefully appreciated.
    >> > Patrick
    >> >
    >> >
    >> > --
    >> > crowdx42
    >> > ------------------------------------------------------------------------
    >> > crowdx42's Profile:
    >> > http://www.excelforum.com/member.php...o&userid=37749
    >> > View this thread:

    > http://www.excelforum.com/showthread...hreadid=573657
    >> >

    >>
    >>

    >
    >




  7. #7
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    Ok, just to clarify. The original sheets were opened renamed from their file name and then moved into a master workbook. This is how each sheet has a name with .xls in it.
    Duh, just tried the original code with the workbook name change.... worked like a charm... EXCELLENT Stuff


    Patrick
    Last edited by crowdx42; 08-21-2006 at 10:55 AM.

+ 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