# Visual Basic > Office Development >  excel VBA and listview question

## Rodmac

Hello im trying to make a listview editable by trying to edit its cells with a texbox inside a frame as you can see in the sub bellow.

by trying to manipulate its cells i also want to know how can i get the index of the edited subitem of a listitem, with the code bellow i can get the index of the selected listitem, but my question is how can i get the selected column or subitem of the respecive selected item?





```
listview1.listitems.SelectedItem.index
```





```
Private Sub ListView1_DblClick()
Dim i           As Integer
Dim ItemSel    As ListItem
If Not ListView1.SelectedItem Is Nothing Then
    Set ItemSel = ListView1.SelectedItem
    i = 2
    With Frame1
        
        .Visible = True
        .Top = ItemSel.Top + ListView1.Top
        .Left = ListView1.ColumnHeaders(i + 1).Left + ListView1.Left
        .Width = ListView1.ColumnHeaders(i + 1).Width
        .Height = ItemSel.Height
        
        .ZOrder msoBringToFront
    End With
    
    With txtEdit
        
        .Visible = True
        .Text = ItemSel.SubItems(i)
        .SetFocus
        .SelStart = 0
        .Left = 0
        .Top = 0
        .Width = ListView1.ColumnHeaders(i + 1).Width
        .Height = ListView1.SelectedItem.Height
        .SelLength = Len(.Text)
    End With
End If
End Sub
```

----------


## Dan_W

Does this help at all? https://www.vbforums.com/showthread....-vba-for-excel and https://www.vbforums.com/showthread....-SubItems-only

----------


## Rodmac

> Does this help at all? https://www.vbforums.com/showthread....-vba-for-excel and https://www.vbforums.com/showthread....-SubItems-only



Thanks Dan, for your help, but in the first thread in th API code bellow, i get the following error: 

"Compile error 

the code in this project must be updates for use on 64-bit systems.
PLease review and update Declare statements ans then mark them with Ptrsafe attribute."

What do i have to change in the API code to make it work?, also if you see my editable listview code by passing to twips it seems that i will have some trouble to edit the rigth listview cell correct?





```

Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long,ByVal hdc As Long) As Long
  
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long

Const HWND_DESKTOP As Long = 0
Const LOGPIXELSX As Long = 88
Const LOGPIXELSY As Long = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As Long
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, lngDC
End Function
```

----------


## Dan_W

> Thanks Dan, for your help, but in the first thread in th API code bellow, i get the following error: 
> 
> "Compile error 
> 
> the code in this project must be updates for use on 64-bit systems.
> PLease review and update Declare statements ans then mark them with Ptrsafe attribute."
> 
> What do i have to change in the API code to make it work?, also if you see my editable listview code by passing to twips it seems that i will have some trouble to edit the rigth listview cell correct?


Of course, I didn't even think to check with you, I'm sorry. I'm so used to having to convert things into 64bit. So looking at it properly now, all of this is just a longwinded (though accurate) way of getting measurements - it's trying to work out how many Twips equate to a pixel of measurement. From memory, the back of the napkin calculations come to 15 twips per pixel, so you can shortcut all that code By just setting 15 as the return value of the function .... But the actual correct answer with the corrected code is probably more helpful to you in the long run.

I can't test it because my laptop with 64bit Office is in the laptop hospital, but this should work... let me know how it goes.



```
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hdc As LongPtr, ByVal nIndex As Long) As Long

Private Const HWND_DESKTOP As LongPtr = 0
Private Const LOGPIXELSX As Long = 88
Private Const LOGPIXELSY As Long = 90

'--------------------------------------------------
Function TwipsPerPixelX() As Single
'--------------------------------------------------
'Returns the width of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As LongPtr
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelX = 1440& / GetDeviceCaps(lngDC, LOGPIXELSX)
  ReleaseDC HWND_DESKTOP, lngDC
End Function

'--------------------------------------------------
Function TwipsPerPixelY() As Single
'--------------------------------------------------
'Returns the height of a pixel, in twips.
'--------------------------------------------------
  Dim lngDC As LongPtr
  lngDC = GetDC(HWND_DESKTOP)
  TwipsPerPixelY = 1440& / GetDeviceCaps(lngDC, LOGPIXELSY)
  ReleaseDC HWND_DESKTOP, lngDC
End Function
```

----------


## Rodmac

Thanks for your Help Dan its been fantastic, im not used to work with API, so just one more question is it possible to change the code bellow to instead of display the subitems count, to diplay the index of the subitem?



```
Private Sub ListView1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
    Dim nodTemp As ListItem
    Dim nodtemp1 As ListSubItem
    Dim strText As String
    If Button = 0 Then
        Set nodTemp = ListView1.HitTest(TwipsPerPixelX * x, TwipsPerPixelY * y)
        If Not nodTemp Is Nothing Then
            strText = " Index code Number " & nodTemp.Index & " Key " & nodTemp.Key & " Count " & nodTemp.ListSubItems.Count
        Else
            strText = " Index code Number Nil"
        End If
        Me.Caption = strText
    End If
End Sub
```

----------


## Dan_W

Sorry, I only saw yesterday that you had responded with this, and I meant to reply last night, but real life got firmly in the way. Do you still need help with this? I ask because I saw your other question, and wasn't sure where you're up to. The short answer to your other question the Screen object is not available in VBA - it's a VB6 feature.

----------

