VBA procedure to update worksheets...

Posted on 2008-11-11
Last Modified: 2012-06-27
I would like to update formulas & text in worksheets.  What is the VBA procedure that will do it?  Let's say:
1)  the worksheet to be updated is open
2)  the formula is "A1 = B1"
3)  the text is "C1 = "NewText""

Question by:ronadair
    LVL 1

    Expert Comment


    Paste the following code into the worksheet code area (Alt F11) and it will run every time the worksheet is activated.

    Hope this helps
    Private Sub Worksheet_Activate()
        Application.ActiveSheet.Range("A1").Formula = "=B1"
        Application.ActiveSheet.Range("C1").Value = "NewText"
    End Sub

    Open in new window


    Author Comment

    Can this code be altered so that every worksheet with the character " > " (or some other character) in its name was updated as above?
    LVL 1

    Accepted Solution

    Yep try something like this
    Private Sub Worksheet_Activate()
        Dim sheet, key
        key = ">"
        For Each sheet In Application.Worksheets
            If InStr(sheet.Name, key) Then
                sheet.Range("A1").Formula = "=B1"
                sheet.Range("C1").Value = "NewText"
            End If
        Next sheet
    End Sub

    Open in new window


    Author Closing Comment

    Works like a charm!


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now