Excel incorrectly activates a cell after it is selected by code in the form

The problem occurs when a cell is selected based on a custom form that is invoked by a macro that is assigned to a form control (Button) on the sheet.

After selecting a worksheet and a cell, the data will not be entered into the selected cells (initially) and disappear when another cell is selected (Tab, Return, arrow keys), clicking the mouse in another cell stops the effect.

To reproduce the problem, you will need:

  • A book with two sheets (Mine are called: Book1, Sheet1 and Sheet2, respectively).
  • Form Control Button on Sheet 1
  • Custom form using the command button (UserForm1 and CommandButton1)
  • Module (Module1)

Module 1 should contain the following:

Sub ShowForm()
    UserForm1.Show
End Sub

UserForm1 Must contain the following:

Private Sub CommandButton1_Click()
    With ThisWorkbook.Worksheets(2)
        .Activate
        .Cells(5, 5).Select
    End With
    Unload Me
End Sub

ShowForm 1.

1 . . , Sheet2, E5 .

E2 2, , , , , , 1, , , Enter, ( , , , ). , 2, . , . , , 2, 1.

ShowForm , , , , .

, ActiveX CommandButton Sheet1 , .

Private Sub CommandButton1_Click()
    call ShowForm
End Sub

ActiveX, Form?

Excel 2013 32bit Windows 8.1 64bit.
Excel 2016, .

+4
1

, , screenupdating=False :

Private Sub CommandButton1_Click()
    With ThisWorkbook.Worksheets(2)
        .Activate
        .Cells(5, 5).Select
    End With
    Unload Me
    Application.ScreenUpdating = False
End Sub
0

Source: https://habr.com/ru/post/1620834/


All Articles