using applicationonkey if statem
Microsoft Office TrainingThe UK's Number 1 for Microsoft Office Training Sitemap add this page to your favourites/bookmarksBookmark page

view a printable version of this pagePrintable version
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft VBA Training and help » Using Application.onkey in an if statement

Using Application.onkey in an if statement

The UK's most regular instructor-led training courses.
Training information: 2003 access vba online classes · Excel london

resolvedResolved · Medium Priority · Version 2003

No ranking yet
6 posts
replyReplyMon 1 Mar 2010, 10:27Delegate Alex said...

Alex has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

Using Application.onkey in an if statement

Option Explicit
Dim bCancelPressed As Boolean
Private Sub Commandbutton1_Click()
bCancelPressed = False
Dim x As Integer
x = 1

Do While bCancelPressed = False


Selection.Cut
Selection.Offset(1, 0).Select
ActiveSheet.Paste
Selection.Name = ("mycell")

DoEvents
Application.Wait Now + TimeSerial(0, 0, 0.6)
Loop
End Sub


What i want to do is end the loop when the right key is pressed. I am using application.onkey to detect the whether the key has been pressed and that works fine outside the loop.

This is what i want to happen

If Appliction.onkey = Right Then
Exit Do
End If



Thanks

Gold
452 posts
replyReplyMon 1 Mar 2010, 16:48Trainer Anthony said...

RE: Using Application.onkey in an if statement

Hi Alex, thanks for your query. The Onkey method does have its limitations and ideally you should be making a call to the Windows API in order to capture any keyboard input. The following code should do what you want:

***************

Option Explicit
Dim bCancelPressed As Boolean
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Const VK_LEFT As Integer = &H25 'left

Sub test()
bCancelPressed = False
Dim x As Integer
Dim leftpressed As Boolean

x = 1

Do While bCancelPressed = False

Selection.Cut
Selection.Offset(1, 0).Select
ActiveSheet.Paste
Selection.Name = ("mycell")

If GetKeyState(VK_LEFT) < 0 Then leftpressed = True Else leftpressed = False
If leftpressed = True Then
bCancelPressed = True
Else
bCancelPressed = False
End If

DoEvents
Application.Wait Now + TimeSerial(0, 0, 0.6)
Loop

MsgBox "Stopped"

End Sub

*******************

Hope this helps,

Anthony

Mon 8 Mar 2010: Automatically marked as resolved.


Related articles

· Introduction to Visual Basic for Applications
· How VBA Can Add Power To Your PowerPoint Presentations
· What Is Microsoft Excel VBA and How Can It Help You Work Smarter?
· Make Office work for you with VBA
· Trigger Happy In Excel VBA

Please browse our web site to find out more about
2003 access vba online classes and other Microsoft training courses.

Vba tip:

Use GoTo to Select A Cell With VBA

To select a certain cell on a Worksheet you can use:

Application.Goto Reference:=Range("V300")

or more simply

Range("V300").Select

If, on the other hand, you want the selected cell to be the top/left cell on the screen you use:

Application.Goto Reference:=Range("V300"), Scroll=True

View all VBA hints and tips


Rate this page:
2.2/5 (127 votes cast)
Institute of IT Training - Accredited Training Provider ILM
Microsoft Certified Partner
Microsoft Office Specialist Authorised Testing Centre (MOS and MCAS)

Prodigy Platinum Learning Partner

Institute of IT Training - Accredited Training Provider
McAfee Secure sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams
Association of Computer Trainers verified by visa, mastercard securecard

Mini sitemap. These are the main areas of our web site. Full sitemap.

Professional & Management

Professional Skills courses
Project Management Course London
Project Management Courses London
Project Management Training London
Project Management Training
Project Seminar
Project Seminars
Time Management Course London
Time Management London
Time Management Courses London
Time Management Training London
Introduction to Finance course
Assertiveness Skills course
Effective Communications Skills training
Presentation Skills London

Training Formats

Public scheduled courses
On-site training
Closed company courses

MS Office & IT Applications

Excel courses
Excel training
Excel Training Courses Medway
Microsoft Excel training
Microsoft Project training
Microsoft Office training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training
MS Project courses
MS Project training
Outlook courses
PowerPoint courses
PowerPoint training
VBA courses
Word courses
(more...)

Crystal Reports training

DreamWeaver courses
Dreamweaver training

Training Information

London Computer Training
Computer Training London

Training venues
Client list
FAQ
Pricing and availability
Course details / Syllabus

Training Information
Training Articles
Microsoft Training Blog

Interested in Access training?
Please see the following pages:

Access courses
Access training
Microsoft Access training
Microsoft access courses
Microsoft training access course
Microsoft+access+training
Access courses in london