Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus back to active cell 1

Status
Not open for further replies.

JoJ

Programmer
May 3, 2002
71
CA
After clicking a cmd button, I cannot transfer focus back to the active cell in Excel. (I was hoping to eliminate one extra mouse click to get back to the cell I was already in.)

The button is on a modeless form. All controls on this form are not tab stops. The TakeFocusOnClick properties are also set to false.

Among many other suggestions, I tried
ActiveSheet.Range(ActiveCell.Address).Activate
in the cmd buttons click event. Even that didn't work.

..tsk
 
It's tricky, but it can be done:

In the code page for "ThisWorkbook":
[blue]
Code:
Private Sub Workbook_Open()
  ExcelWindowHandle = GetForegroundWindow
End Sub
[/color]


In the code page for the form:
[blue]
Code:
Private Sub CommandButton1_Click()
  DoSomething
End Sub
[/color]


In a separate code module:
[blue]
Code:
Option Explicit
Declare Function GetForegroundWindow Lib "User32" () As Long
Declare Function SetForegroundWindow Lib "User32" (ByVal HWND As Long) As Long
Public ExcelWindowHandle As Long

Sub Test()
  UserForm1.Show vbModeless
End Sub

Sub DoSomething()
[green]
Code:
  ' Do whatever it is that needs doing when the buton is pushed
  ' Then pass control back to the workbook (active cell)
  '
  '
[/color]
Code:
  SetForegroundWindow ExcelWindowHandle
End Sub
[/color]


 
Thanks.

Using the User32 API didn't even cross my mind.

I gave you a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top