+ Reply to Thread
Results 1 to 5 of 5

Run code once

Hybrid View

  1. #1
    Registered User
    Join Date
    06-11-2012
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Run code once

    Hi,

    I have the following code for one of my worksheets:

    Private Sub Worksheet_Calculate()
    Dim c As Range
    For Each c In Range("O4:O46")
    If c.Value <= 0 Then MsgBox "Updates are available", vbInformation
    Next c
    End Sub

    How do I get this to run only once when I open Excel?

    Any help you be appreciated.

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Run code once

    Put it in the Thisworkbook code module

    Private Sub Worksheet_Open()
    Dim c As Range
        For Each c In Worksheets("whatever it is called").Range("O4:O46")
            If c.Value <= 0 Then MsgBox "Updates are available", vbInformation
        Next c
    End Sub

  3. #3
    Forum Expert
    Join Date
    09-27-2011
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    1,312

    Re: Run code once

    I think that You wants run Msgbox only once. If so You can use this
    Private Sub Workbook_Open()
    Dim c As Range
    Dim x As Long
    For Each c In Range("O4:O46")
    If Not IsEmpty(c) And c.Value <= 0 Then
    x = x + 1
    End If
    Next c
    If x > 0 Then MsgBox "Updates are available", vbInformation
    End Sub
    Regards

    tom1977

    If You are satisfied with my solution click the small star icon on the left to say thanks.

  4. #4
    Registered User
    Join Date
    06-11-2012
    Location
    Virginia, USA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Run code once

    Tom, I just replaced my code with yours. The only thing I changed was the range, but no message box appears. Im just right clicking on the sheet tab, selecting view code, and pasting it in.

    Any suggestions?
    Last edited by emoandy05; 06-13-2012 at 09:28 AM.

  5. #5
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Run code once

    Quote Originally Posted by emoandy05 View Post
    Tom, I just replaced my code with yours. The only thing I changed was the range, but no message box appears. Im just right clicking on the sheet tab, selecting view code, and pasting it in.

    Any suggestions?
    Maybe qualify the range with the sheet name as I suggested.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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