Listview Row clicked

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,953
Office Version
  1. 2019
  2. 2016
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.
 
I tried various methods and spent ages Googling... eventually read somewhere it wasn't supported.
Both these show 1 regardless of what row
VBA Code:
Private Sub ListView1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
MsgBox ListView1.SelectedItem.Index
End Sub
Private Sub ListView1_Click()
    MsgBox ListView1.SelectedItem.Index
End Sub
Be good to know what does make it work... and if both row and column can be obtained - although the HitTest works well.
I found it hard to find decent docs for this control, which IMO is one of the best! Especially how the columns can be dragged.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Your click routine works fine for me. If you need column as well then you need the approach you already have; I only mentioned this as you never mentioned column originally and getting the row should be simple. ;)
 
Upvote 0
Wow, how can it work for you and not here? That's very weird... I just tried it on Win 11/Office 2019 and it still doesn't work.
Must be some reason..... pity we can't upload a workbook here.
 
Upvote 0
Do you not have access to something like OneDrive or Dropbox?
 
Upvote 0
Ah, I suspect I know what's going on. SelectedItem (naturally) refers to the currently selected row. If what you are clicking on is not in the currently selected row and doesn't change the current selection (e.g. you click a subitem and you don't have FullRowSelect turned on) then you will just get the same number repeatedly.
 
Upvote 0
No, have never set anything like that up. Perhaps someone else might reply with their results
 
Upvote 0
Ah yes, that might be it. Full row select isn't on or off - it's not references at all.
But if it works like that, wouldn't you have you click twice? The idea is get the row just clicked. Any row.
 
Upvote 0
It's off by default, you have to turn it on. I always do because it means you can click in any column to select a row. I find it really irritating to have that turned off. And no, you don't have to click twice.
 
Upvote 0
Roger... I turned it on and seen it working. Handy to know... thanks.
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,341
Members
451,638
Latest member
MyFlower

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