How do you know which worksheet line the selected listview item is on?

Asked

Viewed 163 times

0

I filled my Listview and applied a filter, so that the user can choose the values to be observed.

However, I need that the user can add information to the selected item and that these values are saved in the database source sheet in the same row and in an empty column specifies through the "ADD ACTION" button.

I’ve tried several ways and I don’t know if my code is also making sense...

Thank you all!

'''

Private Sub CommandButton1_Click()

Dim lineDp As Integer
Dim Dp As Worksheet
Dim Ultima_coluna As Long
Dim ColH As ColumnHeader
Dim Index As Long

Set Dp = Sheets("Details Pipeline")

'Number of Rows count
 lineDp = Dp.Range("A100000").End(xlUp).Row

'Number of Colums count - Ctrl + Shift + End
Ultima_coluna = Dp.Cells(lineDp, Dp.Columns.Count).End(xlToLeft).Column

For i = 4 To Ultima_coluna

If Dp.Cells(3, i).Value <> "" Then

For j = 1 To ListView1.ListItems.Count

    If ListView1.ListItems.Item(j).Checked Then

        txt_id = ListView1.ListItems.Item(j)
        Dp.Cells(j, i) = TextBox6.Text

     End If

Next j

End If

Next i

''' inserir a descrição da imagem aqui

  • Create a [mcve] with some sample data in a table.

1 answer

0

You can load the list through a For and the first column put the row that the record is in, but you leave this column with the width reset in the table.

Ex:

Private Sub CarregarListaEmail()
Dim ws As Worksheet
Dim ul As Integer

Set ws = ThisWorkbook.Sheets("Emails")
ul = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

With lstEmail
    .Clear

    .ColumnCount = 7 'NÚMERO DE COLUNAS QUE CONTEM SUA TABELA

    ' OBSERVA QUE O PRIMEIRO '0 PT' É DA COLUNA QUE REPRESENTA O NÚMERO DA LINHA
    .ColumnWidths = "0 pt;170 pt;138,95 pt;38 pt;110 pt;60 pt;0 pt"

    If ul > 1 Then
        For Each lin In ws.Range("A2:A" & ul)
            .AddItem lin.Row
            .List(.ListCount - 1, 1) = lin.Value
            .List(.ListCount - 1, 2) = lin.Offset(0, 1).Value
            .List(.ListCount - 1, 3) = lin.Offset(0, 2).Value
            .List(.ListCount - 1, 4) = lin.Offset(0, 3).Value
            .List(.ListCount - 1, 5) = lin.Offset(0, 4).Value
            .List(.ListCount - 1, 6) = lin.Offset(0, 5).Value
        Next lin
    End If

End With

End Sub

Api when you want to manipulate the record in the spreadsheet just use the row number that of the first column. My example is with Listbox, but the logic will be the same for Listview. I used that example because I have it easy here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.