+ Reply to Thread
Results 1 to 5 of 5

Sending VBA code from a Module to Code a worksheet

  1. #1
    Registered User
    Join Date
    02-24-2009
    Location
    York, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Post Sending VBA code from a Module to Code a worksheet

    Hi All,
    Although I've often used this site and found the answers quite easily - this time I've had to post the question....:
    So new here at posting - here goes.

    I'm building up a macro that takes a newly added name from a staff_list sheet on activating the macro, it adds a new worksheet, names the worksheet as the member of staff and copies in a template page with standard table & formulae on (to record Annual Leave etc).
    This seems to work - although one of the cells on the template is coded with :
    =RIGHT(CELL("filename"),LEN(CELL("filename"))-FIND("]",CELL("filename"),1))
    to pick up the name from the worksheet and display it in large font at the top of the Leave card.
    This works initially but when you tab away from the sheet and back it picks up the name from the last sheet....
    the code I need on each worksheet is :
    Private Sub cmdSelectWorkbook_Click()
    Worksheets(Item).Activate
    Cells(2, 2).Calculate
    End Sub

    this refreshes the sheet on activation.
    However I need to send this code from the main Module to the Worksheet as it is created. so I need some way to hold that code in the Module and paste it to the new sheet uder 'Worksheet / Activation'

    hope this makes sense

    many thanks...
    Any advice welcomed.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Sending VBA code from a Module to Code a worksheet

    You don't need the code in every worksheet - you can use the workbook level event SheetActivate.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Sending VBA code from a Module to Code a worksheet

    What if you used this formula instead? It's value doesn't change when other worksheets are activated.
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    02-24-2009
    Location
    York, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Sending VBA code from a Module to Code a worksheet

    Many thanks to Norie & RHCPgergo for such prompt responses,
    I liked the idea of modifing the formula in the cell to fix the value - so I tried that, but somehow the error trap stops the VBA on the first mention of 'filename'.
    Not sure why this is - as the existing formula has no problems. ( unless they can't exist together on the same sheet - I didn't try that) mmm.
    Anyway I had a go with the Workbook level code to refresh all - failed at first but was probaly due to the rouge REM marker on line 3. Once into the 'this workbook' area , worked in a flash. brilliant.
    so thanks very much folks .
    I'll remember to use Code Tags next time round..!!
    cheers

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Sending VBA code from a Module to Code a worksheet

    That comment mark was meant to be there - I was just trying to show a possible alternative to the formula.

    Mind you've I've had a few rogue things happen when copying code.

+ 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