+ Reply to Thread
Results 1 to 16 of 16

VBA Sleep code time interval question

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    259

    VBA Sleep code time interval question

    Hello, I found some code on a site that can function similar to the codeline: Application.Wait (Now + TimeValue("0:00:01"))

    The code is:

    #If VBA7 Then
    
        Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64-Bit versions of Excel
    
    #Else
    
        Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32-Bit versions of Excel
    
    #End If
    
    Sub SleepDemo()
    
    Sleep 500 'milliseconds (pause for 0.5 second)
    
    'resume macro
    
    End Sub
    Currently, I have an event code that changes a shapes color if data is entered in a particular cell. Unfortunately though, it can only do 1 second intervals with the application.Wait codeline. The code I have doing this is as follows:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("J34")) Is Nothing Then Exit Sub
        MsgBox "REMINDER: After filling out info for this row, click the Archive and Reset Sheet button", vbOKOnly
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait (Now + TimeValue("0:00:01"))
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Application.Wait (Now + TimeValue("0:00:01"))
        
    End Sub
    Is it possible to somehow use the sleep 500 in my event code to do the half second intervals to replace the applicaton.wait aspect? How might this be done if so?

    Thank you for your time!
    Last edited by PitchNinja; 09-11-2024 at 12:51 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,802

    Re: VBA Sleep code time interval question

    Create a new standard module or use an existing one (like Module1) and add the API code at the top of the module
    Option Explicit
    
    #If VBA7 Then
    
        Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64-Bit versions of Excel
    
    #Else
    
        Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32-Bit versions of Excel
    
    #End If
    Then in your worksheet module update your existing code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Intersect(Target, Range("J34")) Is Nothing Then Exit Sub
        
        MsgBox "REMINDER: After filling out info for this row, click the Archive and Reset Sheet button", vbOKOnly
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Sleep 500 'milliseconds (pause for 0.5 second)
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Sleep 500 'milliseconds (pause for 0.5 second)
        
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    259

    Re: VBA Sleep code time interval question

    Thanks Jeff, I tested this out but it actually isn't changing the shapes color after I click "OK" on the message box that pops up when typing something into J34. It was changing the color before with the Application.Wait line but for this after clicking OK, it stays the same color and has a bit of a delay before you can click in another cell. That delay in being able to click was expected, but the lack of color change wasn't. Were you getting a different result?
    Last edited by PitchNinja; 09-11-2024 at 03:21 PM.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,802

    Re: VBA Sleep code time interval question

    I don't have your file so there was no way for me to test it. Attach your file and I'll be happy to look.

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA Sleep code time interval question

    I couldn't get Sleep to work either, even with DoEvents. At a guess I'd say the API overrides the vba.
    You can use Wait with < 1 second though.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numSecs As Double = 0.25 'quarter of a second
        If Intersect(Target, Range("J34")) Is Nothing Then Exit Sub
        MsgBox "REMINDER: After filling out info for this row, click the Archive and Reset Sheet button", vbOKOnly
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
    End Sub
    Last edited by ByteMarks; 09-12-2024 at 06:18 AM.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: VBA Sleep code time interval question

    You could add:

        Application.ScreenUpdating = True
        DoEvents
    after the line that makes it red.

    By the way, the VBA7 declaration is wrong - dwMilliseconds should always be a Long, not a LongPtr. It's just a number.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA Sleep code time interval question

    Didn't think of trying that given it was already True to start with.

  8. #8
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    259

    Re: VBA Sleep code time interval question

    Thank you guys for the replies, I was going to attach the sample but I tried ByteMarks solution and it works, no need for the separate coding on another module. I like that if I want to change the interval I only need to go to the top and change the .25 to whatever I need. Thank you so much for this solution. I really didn't think it was possible to go for less than 1 second, so this is great to know as I have some creative ideas I might put in place that would utilize this feature.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,802

    Re: VBA Sleep code time interval question

    Quote Originally Posted by PitchNinja View Post
    I tried ByteMarks solution and it works
    If your question has been answered please mark your thread as "Solved" so that members will know by looking at the thread title that your problem is solved. Go to the menu immediately above your first post to the thread and click on Thread Tools. From the dropdown menu select "Mark this thread as solved..."

    If a member helped you solve your problem, consider adding to their reputation by clicking $A addreputationiconsmall.jpg below their profile in any of their posts.

  10. #10
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA Sleep code time interval question

    Happy to have contributed.

  11. #11
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    259

    Re: VBA Sleep code time interval question

    Actually after thinking, I am curious - would there be a lot of code needed to also change a Shape's size as well? Ultimately, this is the code I have with the color change to flash different colors a few times:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numSecs As Double = 0.25  'quarter of a second
        If Intersect(Target, Range("J34")) Is Nothing Then Exit Sub
        MsgBox "REMINDER: After filling out info for this row, click the Archive and Reset Sheet button", vbOKOnly
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
    End Sub
    How about if you wanted to do something like this(see underlined):

    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numSecs As Double = 0.25  'quarter of a second
        If Intersect(Target, Range("J34")) Is Nothing Then Exit Sub
        MsgBox "REMINDER: After filling out info for this row, click the Archive and Reset Sheet button", vbOKOnly
        Increase Shape Size
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbRed
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        Shapes("Archive_Reset").Fill.ForeColor.RGB = vbBlue
        Decrease shape size back to original size
        Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
        
    End Sub
    Original height is 1.12" and width is 1.47" What if we wanted to go to 1.25" and 1.69" respectively?

  12. #12
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA Sleep code time interval question

    something like this:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numSecs As Double = 0.25  'quarter of a second
        Const hSmall As Double = 1.12: Const hBig As Double = 1.25
        Const wSmall As Double = 1.47: Const wBig As Double = 1.69
       
        If Intersect(Target, Range("J34")) Is Nothing Then Exit Sub
        MsgBox "REMINDER: After filling out info for this row, click the Archive and Reset Sheet button", vbOKOnly
        With Shapes("Archive_Reset")
            ' Increase Shape Size
            .Height = hBig: .Width = wBig
            'Flash colours
            For i = 1 To 3
                .Fill.ForeColor.RGB = vbRed: Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
                .Fill.ForeColor.RGB = vbBlue: Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
                DoEvents
            Next
            'Decrease Shape Size
            .Height = hSmall: .Width = wSmall
        End With
    End Sub

  13. #13
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    259

    Re: VBA Sleep code time interval question

    Ok ByteMarks, I pasted your code over mine, but it didn't behave as expected. It made the shape very tiny. I am attaching a sample copy with your code pasted. If you type anything in J34 and then click out of the cell or press enter, it will run the event code.(The same goes if you delete what you type in that cell). I don't know if I gave you bad info, but I had right-clicked the shape and went to size and properties and had copied what was there. Not sure if there was somewhere else I should have looked or if sizes are handled differently in VBA?
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA Sleep code time interval question

    Sorry, I missed that it was in inches.
    Convert the inches to points:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Const numSecs As Double = 0.25  'quarter of a second
        
        With Application
            Dim hSmall As Double: hSmall = .InchesToPoints(1.12)
            Dim hBig As Double: hBig = .InchesToPoints(1.25)
            Dim wSmall As Double: wSmall = .InchesToPoints(1.47)
            Dim wBig As Double: wBig = .InchesToPoints(1.69)
        End With
       
        If Intersect(Target, Range("J34")) Is Nothing Then Exit Sub
        MsgBox "REMINDER: After filling out info for this row, click the Archive and Reset Sheet button", vbOKOnly
        With Shapes("Archive_Reset")
            ' Increase Shape Size
            .Height = hBig: .Width = wBig
            'Flash colours
            For i = 1 To 3
                .Fill.ForeColor.RGB = vbRed: Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
                .Fill.ForeColor.RGB = vbBlue: Application.Wait CDbl(Date) + (Timer + numSecs) / 86400
                DoEvents
            Next
            'Decrease Shape Size
            .Height = hSmall: .Width = wSmall
        End With
    End Sub

  15. #15
    Forum Contributor
    Join Date
    05-12-2021
    Location
    USA
    MS-Off Ver
    Microsoft Office 365
    Posts
    259

    Re: VBA Sleep code time interval question

    Ah okay yep now it's working. That's really neat! So if I want to change the amount of color flashes, I just change the for i = 1 to 3 to something else. Going to play with this a bit. Thanks for your help again!
    Last edited by PitchNinja; 09-13-2024 at 08:19 AM.

  16. #16
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,984

    Re: VBA Sleep code time interval question

    Yes. Change the 3 to however many flashes. You could even use a variable and change the number of flashes depending on the cell or cell value.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Check if the in and out time falls between a specific time interval
    By srivibish in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-27-2020, 03:18 AM
  2. Sleep command question
    By Joven76 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2018, 08:21 PM
  3. VBA calculate sleep time, when time is entered in military time format
    By axm1955 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-29-2017, 10:28 AM
  4. Replies: 11
    Last Post: 10-24-2016, 08:12 PM
  5. [SOLVED] VBA code to find if station is booked in certain date and time interval
    By sysss in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2016, 06:24 PM
  6. Replies: 1
    Last Post: 04-29-2014, 04:42 AM
  7. Replies: 0
    Last Post: 04-23-2012, 10:06 AM

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