Re: VBA code ok in Excel 2003, but crashes Excel 2002 & 2000

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Tom,

I haven't tried it but slightly surprised it even works in XL2003. Try
changing

Private Sub CommandButton1_KeyDown(
to
Private Sub CommandButton1_KeyUp(

- for any routine that will lead to changing the active sheet. Notice if you
press the Space key, which fires the click event, it only runs on KeyUp.

If you particularly prefer KeyDown for the other Case's, for the Case
Enter-key to activate another sheet, call code to do that in macro in an
ordinary module but called with the OnTime method.

In passing,
TextBox1_KeyDown includes TextBox1.Activate
and
CommandButton1_KeyDown includes CommandButton1.Activate

Why, when the respective controls already have focus.

In the snippet you posted there's no need to disable Screenupdating

Regards,
Peter T


"tomgreen1000" <tomgreen1000@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:6C0C2092-0B5E-4CCA-B0C1-1FA78FD6EDE1@xxxxxxxxxxxxxxxx
Hello. I have a very complicated (and poorly constructed) legacy
program/spreadsheet that I must now support. I have found a VBA code
segment
that is causing some problems. The actual code is excessively
complicated,
so I have recreated this simple example.

Open a new workbook. On Sheet 1, place one text box and one command
button.
Sheet 2 should be completely blank. Sheet 3 can be deleted or left as
is.
Click into the textbox. The intent of the VBA is to allow you to use Tab,
Return, or Down Arrow to get onto the command button. Once on the command
button, Shift+Tab or Up Arrow will return you to the textbox. Tab or Down
Arrow will keep you on the command button. Using Return, while on the
command button, should execute the code associated with the Click Event.

In Excel 2003, everything works as expected. In Excel 2002 or 2000, the
code executes correctly if the command button is clicked. However if you
attempt to use Return to execute the Click Event, Excel crashes. If you
step
thru the code, everything executes ok until the last End Sub. I need this
to
work in Excel 2002, so any ideas or work arounds will be greatly
appreciated.

I am attempting to cut/paste the code which is located on the Sheet1
object.

Thanks for your help, Tom

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
Dim bBackwards As Boolean
Select Case KeyCode
Case vbKeyTab, vbKeyReturn, vbKeyDown, vbKeyUp
Application.ScreenUpdating = False
bBackwards = CBool(Shift And 1) Or (KeyCode = vbKeyUp)
If bBackwards Then TextBox1.Activate Else CommandButton1.Activate
Application.ScreenUpdating = True
End Select
End Sub

Private Sub CommandButton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
Dim bBackwards, bForwards As Boolean
Select Case KeyCode
Case vbKeyTab, vbKeyReturn, vbKeyDown, vbKeyUp
Application.ScreenUpdating = False
bBackwards = CBool(Shift And 1) Or (KeyCode = vbKeyUp)
bForwards = (KeyCode = vbKeyTab) Or (KeyCode = vbKeyDown)
If bBackwards Then
TextBox1.Activate
ElseIf bForwards Then
CommandButton1.Activate
Else
Call CommandButton1_Click
End If
Application.ScreenUpdating = True
End Select
End Sub

Private Sub CommandButton1_Click()
Sheet2.Cells(1, 1).Value = "test"
Sheet2.Activate
End Sub



.



Relevant Pages

  • RE: using colour
    ... Now if you go to the properties of each of these command buttons, ... Any time you change selection on the sheet, it stores the address of the ... selected cell into cell L1. ... Private Sub Worksheet_SelectionChange ...
    (microsoft.public.excel.worksheet.functions)
  • RE: Command Button Problem
    ... Thanks once again Mike. ... inserting new command buttons but still same error is comming up. ... Private Sub CommandButton9_Click ... sheet that has these buttons on. ...
    (microsoft.public.excel.misc)
  • Re: Saving a single worksheet
    ... I used the script for Mil one sheet in Outlook object model. ... I am trying to apply this macro to a command button in the sheet. ... if I use display under the lines for OutMail will it stop at the point where I will have to push the send button for the message to go? ... > Private Sub CommandButton1_Click ...
    (microsoft.public.excel.programming)
  • Re: Universal CommandButtons Accross Worksheets
    ... Private Sub Workbook_Deactivate ... try to use the commands it automatically opens the original workbook. ... Right now I have six command buttons and 485 lines of code for Just One ... buttons into everyworks sheet and then configure every button like this... ...
    (microsoft.public.excel.programming)
  • Re: VBA Autorefresh format change
    ... "Dave Peterson" wrote: ... Private Sub Worksheet_Calculate ... Dim C As String ... If I go to another sheet in the same workbook and change a number or do ...
    (microsoft.public.excel.programming)