Solved

Slowing down a "For Next"  Loop in Visual basic

Posted on 2013-11-20
4
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

752 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