Listview Row clicked

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I need the row number clicked in Excel ListView control.
Does anyone have a solution ? All I can find is
The last message there has some code but it won't work for me.
Error is variable not found on 'Screen'
VBA Code:
With hitTest
        .flags = LVHT_ONITEM
        .pt.x = (x \ Screen.TwipsPerPixelX)
        .pt.y = (y \ Screen.TwipsPerPixelY)
    End With
Which is quite right as 'Screen' isn't mentioned. May be connected to instructions in the declarations area which I don't follow.
Can anyone help, thank you.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I don't have the ListView control installed so I can't test but try removing the /Screen.TwipsPerPixelX/Y and see if it works :
VBA Code:
With hitTest
        .flags = LVHT_ONITEM
        .pt.x = (x)
        .pt.y = (y)
End With

Also to get the row, replace the Text Property with the Index Property:
MsgBox yourListView.ListItems(hitTest.iItem + 1).Text
MsgBox ListView1.ListItems(hitTest.iItem + 1).Index
 
Upvote 0
Solution
Magnificent, thanks Jaafar. I really needed this!
.Index didn't seem to do anything, but your modified code works. The Col is hitTest.iSubItem and the Row hitTest.iItem + 1
 
Upvote 0
I need the row number clicked in Excel ListView control.
Does anyone have a solution ? All I can find is
The last message there has some code but it won't work for me.
Error is variable not found on 'Screen'
VBA Code:
With hitTest
        .flags = LVHT_ONITEM
        .pt.x = (x \ Screen.TwipsPerPixelX)
        .pt.y = (y \ Screen.TwipsPerPixelY)
    End With
Which is quite right as 'Screen' isn't mentioned. May be connected to instructions in the declarations area which I don't follow.
Can anyone help, thank you.
The reason it doesn't work is because it's VB6 code. Annoyingly, they got a few more objects and functions than VBA and so we have to try and code our way to some semblance of parity.
Out of curiosity, @AlexanderBB , what version of Office and bitness do you have?
 
Upvote 0
I have Office 2019 on Win 10 machine. Not sure what bitness is... maybe I don't have that.
 
Upvote 0
You either have 32 or 64 bit.

If all you want is the row number, what is wrong with SelectedItem.Index?
 
Upvote 0
I have Office 2019 on Win 10 machine. Not sure what bitness is... maybe I don't have that.
I ask because of it seems that some 64bit Office uses inexplicably don't have access to the ListView control, etc, and so I just wanted to check.

@AlexanderBB You can get the information by clicking File -> Account -> About Excel, and then it is at the end of the first line of text.
Alternatively, you can run:

VBA Code:
Sub OfficeBitness()
    Dim Result As String
    #If Win64 Then
        Result = "You're using 64-bit MSOffice"
    #Else
        Result = "You're using 32-bit MSOffice"
    #End If
    MsgBox Result
End Sub
 
Upvote 0
Aah I see. Office is 32bit, OS is 64 bit. But I share the workbook with a 64 bit user and have to add ptrsafe etc.
Rory, I wasn't able to get any results from SelectedItem.index in Excels Listview 6.0 (SP6). Were you ?
 
Upvote 0
Dan, I tried a test with Excel 2019 64 Bit with a listview control and it all worked okay. Including the HitTest routine.
The workbook was created on XP with Office 2010 32bit.
The Toolbox Listview control was added via Tools- Additional Controls -Microsoft Listview Control 6.0 (SP6) and reference
Microsoft Windows Common Controls 6.0 SP6 added.
You may need to browse to C:\WINDOWS\system32\MSCOMCTL.OCX
 
Upvote 0
Rory, I wasn't able to get any results from SelectedItem.index in Excels Listview 6.0 (SP6). Were you ?
Yes, I was - it returns the position of the selected item. How did you use it?
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

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