Set Focus to Listview after Closing Another Userform

LogheadLeech

New Member
Joined
Sep 23, 2011
Messages
3
Hello All,

Thank you in advance for any help offered on this most frustrating problem.

Details
I have a Userform with a listview on it. The user selects a row using the Up+Down arrows, then presses Enter to add detail to that row. This shows a second userform with controls for the info.
My problem is that once the second form has been closed, and the original form reactivated, I can't seem to get focus back to the Listview so that the user can carry on using Up+Down Arrows.

I've created a simplified example workbook that demonstrates my problem, http://www.mediafire.com/?pm2i1e2cwi2g2yb.

Code follows

Userform1:
Code:
Private Sub ListView1_KeyDown(KeyCode As Integer, ByVal Shift As Integer)
    Dim i As Integer
    
    Select Case Shift
        Case 0
            'Shift not pressed
            Select Case KeyCode
                Case 13
                    'Enter button pressed
                    
                    'This line shows Userform2, and returns the selection, all fine
                    Label1.Caption = "You selected " & UserForm2.GatherData
                    
                    'However, this line (and all others I've tried) does not return focus to the listview
                    ListView1.SetFocus
            End Select
    End Select
    
End Sub

Userform2:
Code:
Public Function GatherData() As String
    Me.Show
    GatherData = ComboBox1.Text
End Function

Any and all help will be greatly appreciated. I devoted many hours of attempting to solve this last night and got nowhere.


Also, Awesome forum. Have honed my skills here over the past few years and will be looking to help others as much as possible.


Paul
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Solution Found

I feel incredibly stupid now, but here is the fix that a colleague suggested.

First, set focus to another control (eg CommandButton1) then set focus to Listview.
This worked perfectly.

Code:
Private Sub ListView1_KeyDown(KeyCode As Integer, ByVal Shift As Integer)
    Dim i As Integer
    
    Select Case Shift
        Case 0
            'Shift not pressed
            Select Case KeyCode
                Case 13
                    'Enter button pressed
                    
                    'This line shows Userform2, and returns the selection, all fine
                    Label1.Caption = "You selected " & UserForm2.GatherData
                    
                    '### added this line, solved the problem ###
                    CommandButton1.SetFocus
                    ListView1.SetFocus
            End Select
    End Select
    
End Sub

My apologies to anyone who spent any time on this. I will endevour to repay my debt over the next week by answering any posts that I can.
 
Last edited:
Upvote 0
Quite interesting and glad it worked out for you. But not for me on VBA Excel 365 (MSCOMCTL..OCX version 6).
For me, now way to get the focus of the Listview!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top