+ Reply to Thread
Results 1 to 15 of 15

VBA to change Sheet Name based on lookup from cell value.

  1. #1
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    VBA to change Sheet Name based on lookup from cell value.

    Hi guys,

    I need help with a VBA code that would look up the contents in a cell on some of my worksheets and then look up that value in a table that I have on a lookup sheet that would then name the original sheet based on the lookup value.

    I have attached an example sheet

    Sheet1 is lookup
    Manager name is in cell A1, lookup this name in the table on sheet1 and then return the abbreviation as the sheet name that the manager name appears.
    Attached Files Attached Files

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA to change Sheet Name based on lookup from cell value.

    This can do what you need

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    Mike,

    Thanks fro quick reply. I entered code into my test sheet and it works great, however when i plugged into my workbook i got an error. The lookup cell is L1 and the sheet with with the table is now called lookup. Here is the code and how I changed it.

    Sub abc()
    Const shList As String = "Lookup"
    Dim ws As Worksheet
    Dim shName As String

    For Each ws In Worksheets
    With ws
    If .Name <> shList Then
    If Not IsError(Evaluate("=VLOOKUP(" & .Name & "!L1," & shList & "!A:B,2,FALSE)")) Then
    shName = Evaluate("=VLOOKUP(" & .Name & "!L1," & shList & "!A:B,2,FALSE)")
    .Name = shName
    End If
    End If
    End With
    Next
    End Sub

    The error is a Run tim error 1004

    Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by visual basic.

    Thanks

  4. #4
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    I found the problem. Thanks for ur help.

    I have another item i could use ur help with. When I name these sheet there are 2 sheets that will have the same value in L1. One sheet is named the abbreviation as previouly stated, the other sheet is the abbreviation with a space and a F after it. Is there a way to make the names work also. The sheet with the F after always is listed after the reglar sheet. Do you know of a way to accomadate this?

  5. #5
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA to change Sheet Name based on lookup from cell value.

    Do you mean to skip those sheets when looping thru the worksheets?

  6. #6
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    yes and then I would want to name them the same as the sheet before it and add a space and F.
    So
    1st Sheet is MB
    2nd Sheet is MB F
    3rd Sheet is AB
    4th Sheet is AB F

    and so on

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA to change Sheet Name based on lookup from cell value.

    I probably would need to see the workbook to get a better visual of your request.

  8. #8
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    Here is an updated test file.

    Same thing as before with the manager lookup. I just need to name the following sheet the same and add a F to it. The name in Cell A1 will be the same
    Attached Files Attached Files

  9. #9
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA to change Sheet Name based on lookup from cell value.

    Give this a try

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    It doesnt seem to work. I thought the sheet with the F was in L1 it is actuall in K1. What do I change?

  11. #11
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    I just added an extra column to those sheets and hid it. Thank you for you help

  12. #12
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA to change Sheet Name based on lookup from cell value.

    I forgot the single quotes, try this.

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    do you have the code to reference K1 on that F sheet. If it requires alot more work, I will just insert and hide the row. If you can come up with it quickly i would appreciate it.

  14. #14
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: VBA to change Sheet Name based on lookup from cell value.

    No I cant really think of a way to do that being were referencing L1 to rename the sheet to MA F

  15. #15
    Registered User
    Join Date
    09-04-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    75

    Re: VBA to change Sheet Name based on lookup from cell value.

    Yea I will just add and hide a column. Thanks alot for your help Mike

+ 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