Consulting

Results 1 to 16 of 16

Thread: Worksheet VBA naming

  1. #1
    VBAX Regular
    Joined
    Jun 2005
    Posts
    7
    Location

    Worksheet VBA naming

    I have a summary worksheet from which I have student names that are hyperlinked to their own worksheet which has student grades on it. My problem is that I would like to change the hyperlink labeled "student 1" on the summary page, which is the default name when opening the workbook from the template, to the actual student name. After doing so it should then take me to that worksheet and place that name in cell A1. It should also change the name of the worksheet to the student name. Here is the code I currently have that does in fact change the worksheet name but then upon coming back to the summary hyperlink won't link back to the same page. It won't recognize the new name and links me to the summary by default. BTW, the student worksheets A1 cell is referenced to the summary worksheet hyperlinked cell.

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Target.Address = "$A$1" Then Sh.Name = Target
    End Sub
    What's wrong? I did not develop this code, it was given to me by someone else. I say this only to let you know I'm not a VBA programmer.

    Thanks!

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi vossler, welcome to VBAX

    Try this

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then ActiveSheet.Name = Target
    End Sub
    HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Regular
    Joined
    Jun 2005
    Posts
    7
    Location

    Thanks!

    Thanks for welcome and quick response. I never thought I'd see myself writing in to a programmers board.

    Your help is greatly appreciated John, I'll try your recommendation tomorrow when I get to work. I'll let you know how it went. Also thanks for telling me how to post VBA code so that it is easier for folks to read.

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi vossler,

    Not a prob. By the way, that bit goes in the relevant worksheet module. (just read your last sentence ) - also - your original code belongs in the ThisWorkbook module and it should work fine in there (it seems strange it doesn't)... is that where you had it?

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    VBAX Regular
    Joined
    Jun 2005
    Posts
    7
    Location
    John,

    I installed the code but it had no effect on the problem and didn't appear to really do anything.

    Yes, I had the original code in the ThisWorkbook Module. I installed your code in what I thought was a specific worksheet module (I clicked on a worksheet and then pasted the code) but this installed in all worksheets. I must not be aware of how to install something in the worksheet module.

    Thank again,
    Reno

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Reno,

    Can you copy the workbook, remove any sensitive info (and only need 3 or 4 sheets), zip it and post it here for us to look at? I'm not getting this problem...

    Use 'Go Advanced' and scroll down a bit to find a 'Manage Attachments' button (to upload the zip file)

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    VBAX Regular
    Joined
    Jun 2005
    Posts
    7
    Location

    Here it is...

    I've enclosed the file in question, it's a template so it doesn't have any data.

    Thanks,

    Reno

  8. #8
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    I took a quick look at the spreadsheet. The Student names in the relevant A1 cells are linked by to the Summary sheet. So if a hyperlink on the Summary sheet is edited (right click | Hyperlink | Edit Hyperlink | Text to display) and the actual student's name used for the link text, the first part of the problem is resolved.

    One would think that Johnske's suggestion of using the sheet change procedure would be a simple way to rename the activesheet to whatever is found in cell A1. However, when the hyperlink executes, nothing on the target sheet is changed and, thus, the Worksheet_Change does not trigger (at least not in Excel2000). I Have been meaning to post a query about this type of problem, i.e., how to execute a sheet-specific macro when that sheet is the target of a hyperlink.

    Another solutino to Vossler's problem would be a single macro that sweeps through all sheets starting with, say, "Student" and changes the name of the sheet to whatever is found in cell A1. A command button could be placed on the summary sheet and linked to the relevant macro.

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by MWE
    how to execute a sheet-specific macro when that sheet is the target of a hyperlink.
    Hi MWE!

    Maybe check out the FollowHyperlink or SheetFollowHyperlink events. (Sheet/ThisWorkbook respectively). (in 2003 anyway.)

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  10. #10
    VBAX Regular
    Joined
    Jun 2005
    Posts
    7
    Location
    MWE, Yes this is exactly what happens for me. You can force the worksheet to change it's name by double clicking on cell A1, but when you go back to the summary sheet and attempt to click on the link it tells you "Reference is not valid".

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this.

  12. #12
    VBAX Regular
    Joined
    Jun 2005
    Posts
    7
    Location
    Thanks XLD, It works like a charm.


    Now I'm going to have to take a look under the hood and see just how you did that. That ought to be fun!

    Thanks to everyone who took a look and helped out. I know that I certainly couldn't have done it on my own.

  13. #13
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by vossler
    Thanks XLD, It works like a charm.


    Now I'm going to have to take a look under the hood and see just how you did that. That ought to be fun!

    Thanks to everyone who took a look and helped out. I know that I certainly couldn't have done it on my own.
    I must have missed the intent of the problem. xld's version (as it runs on my system: Excel2000 under WinXP) does not appear to solve any of the problem(s) originally posed. My interpretation of what was required:
    1. post the name in the hyperlink anchor cell to cell A1 of the hyperlink target sheet
    2. rename the target sheet to the name in the hyperlink target
    3. (as result of renaming the target sheet) rebuild the hyperlink so that it points to a valid sheet

  14. #14
    VBAX Regular
    Joined
    Jun 2005
    Posts
    7
    Location
    MWE,

    Your 3 point assessment is right on, those are the requirements. After receiving the solution I sent it to my boss (Win XP and Excel 2000) and it didn't work completely as intended but he was able to do the renaming as described in your three points. Other parts of the workbook however didn't work as it did on my system (Win XP Excel 2003). I guess there are some anomalies between the different versions of Excel.

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by MWE
    I must have missed the intent of the problem. xld's version (as it runs on my system: Excel2000 under WinXP) does not appear to solve any of the problem(s) originally posed.
    That too was my understanding of the problem.

    Are you saying that it doesn't rname the sheet, doesn't put the new name in the A1 cell on that sheet, and doesn't redirect the hyperlink?

    If so, what does it do on your system?

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just tried it on 2000, and in essence it works okay for me there too.

    Some of the sheets and names seem to have gotten mixed up in tyhe posted version, but I was able to fix these by changing the name back to the same as the sheet name, then change again to the base name, forcing correction.

    I think this version is now correct in 2000 and XP.

Posting Permissions

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