Solved

Slowing down a "For Next"  Loop in Visual basic

Posted on 2013-11-20
4
341 Views
Last Modified: 2014-01-06
I have the following code in Excel.
It basically, moves back and forth from Excel to my DOS based system
and Copy's and Pastes some information.
I'd just like to slow it down, so I can see what lines are not being copied.

I'd just like a 2 second delay on each loop.
I know how to do that in other programming languages I've used.
How can I do that in Visual Basic?




Sub sendtocp()
AppActivate "My Program"
For i = 3 To 21



 SendKeys Sheet1.Cells(i, 3), True
 SendKeys "~", True
 SendKeys Sheet1.Cells(i, 5), True
 SendKeys "~", True
 SendKeys Sheet1.Cells(i, 5), True
 SendKeys "~", True
 SendKeys "~", True
 y = 1
 For j = 1 To 10000
 y = y + 1
 Next j
Next i
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub
0
Comment
Question by:James Powell
4 Comments
 
LVL 9

Accepted Solution

by:
WebDevEM earned 300 total points
ID: 39662814
Hi,

I think what you're looking for would be
Sleep 2000 'Number of milliseconds to pause

Open in new window

There's a site called MrExcel.com that has some VERY handy tips, and I found this on it.  I hope it helps!

WebDevEM
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39663503
Do note, as in the link provided by WebDevEM, you have to declare the Sleep() API before you can use it:

    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
0
 
LVL 40
ID: 39664658
The .NET topic has nothing to do with VBA in Excel because Visual Basic .NET and VBA are quite different.

The Microsoft Access topic does not help much either, because although both Excel and Access use VBA, they do it in different ways.

You are losing potential experts by defining useless topics in your questions. Aren't there VBA and Excel topics?
0
 
LVL 13

Expert Comment

by:Surone1
ID: 39674646
i reccomend not using sleep since it will "freeze" things up.
as an alternative you could try something like this :


starttime = Now 'i put this in for debugging only
endtime = DateAdd("s", 90, Now) 'determine endtime
Do While endtime > Now 'start 90 second loop
Label1.Caption = Now 'do sonmething OR NOT!
DoEvents 'prevent this from freezing
Loop
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

803 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