Valor de celda para modificar datos de la fila correspondiente

Tengo un UserForm para buscar datos de una tabla, y requiero modificar los datos seleccionados del ListBox del UserForm. Los datos seleccionados se copian en TextBox3, ComboBox1... Luego al dar click en un botón aceptar, se tiene que modificar los datos de la fila correspondiente. Mas o menos de la siguiente manera:

Private Sub CommandButton1_Click()
fila = Cells(?,?).Value   ... 'aquí deberá ir el valor de la celda seleccionada de un ListBox que corresponde al valor de la celda buscada en la tabla en la columna X

' Luego estos valores los reemplazo en las celdas correspondientes

Cells(fila, 25).Value = TextBox3.Text
Cells(fila, 26).Value = ComboBox1.Text
Cells(fila, 28).Value = TextBox4.Text

End Sub

Respuesta
2

Una opción es cargar el número de fila en el listbox. Es decir, cuando filtras la información, puede ser que cargaste en el listbox la información de las filas 3, 8 y 20, entonces en el listbox en una columna tienes que cargar el número de fila.

Pon tu código para ver cómo estás cargando los datos en el listbox, ya que depende de si estás cargando con .Rowsource o con .List o con .Additem

Pon tu código y te ayudo con las 2 partes, para guardar el número de fila. Y después cuando vas a modificar los datos en la hoja para recuperar ese número de fila.

Private Sub CommandButton1_Click()
fila = Cells(24, 24).Value

If TextBox3.Value = "" Then
MsgBox "Tiene que introducir un valor en el cuadro 'Precio'"
GoTo Fin:
ElseIf ComboBox1.Value = "" Then
MsgBox "Tiene que seleccionar un valor de la listra desplegable en el cuadro 'Unidad'"
GoTo Fin:
ElseIf TextBox4.Value = "" Then
MsgBox "Tiene que introducir un valor en el cuadro 'Proveedor'"
GoTo Fin:
ElseIf TextBox4.Value <> "" Then

Cells(fila, 25).Value = TextBox3.Text
Cells(fila, 26).Value = ComboBox1.Text
Cells(fila, 28).Value = TextBox4.Text
ActiveSheet.Range("X23").Value = ActiveSheet.Range("X23").Value + 1

End If
Unload Me
Fin:
End Sub


Private Sub TextBox1_Change()
On Error Resume Next
uf = Hoja6.Range("X" & Rows.Count).End(xlUp).Row

If TextBox1 = "" Then
Me.ListBox1.RowSource = ""
Exit Sub
End If
Me.ListBox1.RowSource = "Tabla16715"
Hoja6.AutoFilterMode = False
Me.ListBox1 = Clear
Me.ListBox1.RowSource = Clear

For fila = 26 To uf
strg = Hoja6.Cells(fila, 24).Value 'Variable para descripción
Precio = Hoja6.Cells(fila, 25).Value 'Variable para Precio de venta
Und = Hoja6.Cells(fila, 26).Value 'Variable para unidad de medida
'Exist = Hoja6.Cells(fila, 5).Value 'Variable para existencia en stok
Codg = Hoja6.Cells(fila, 27).Value 'Variable para codigo y ubicacion
Provee = Hoja6.Cells(fila, 28).Value 'Variable para probeedor
'Prec_copr = Hoja6.Cells(fila, 10).Value 'Variable para precio de venta
If UCase(strg) Like "*" & UCase(TextBox1.Value) & "*" Then
Me.ListBox1.AddItem
Me.ListBox1.List(x, 0) = Hoja6.Cells(fila, 24).Value
Me.ListBox1.List(x, 1) = Hoja6.Cells(fila, 25).Value
Me.ListBox1.List(x, 2) = Hoja6.Cells(fila, 26).Value
'Me.ListBox1.List(x, 3000000) = Hoja6.Cells(fila, 5).Value
Me.ListBox1.List(x, 3) = Hoja6.Cells(fila, 27).Value
Me.ListBox1.List(x, 4) = Hoja6.Cells(fila, 28).Value
'Me.ListBox1.List(x, 6000000) = Hoja6.Cells(fila, 10).Value
x = x + 1
ElseIf Codg Like "*" & UCase(TextBox1.Value) & "*" Then
Me.ListBox1.AddItem
Me.ListBox1.List(x, 0) = Hoja6.Cells(fila, 24).Value
Me.ListBox1.List(x, 1) = Hoja6.Cells(fila, 25).Value
Me.ListBox1.List(x, 2) = Hoja6.Cells(fila, 26).Value
'Me.ListBox1.List(x, 3000000) = Hoja6.Cells(fila, 5).Value
Me.ListBox1.List(x, 3) = Hoja6.Cells(fila, 27).Value
Me.ListBox1.List(x, 4) = Hoja6.Cells(fila, 28).Value
'Me.ListBox1.List(x, 6000000) = Hoja6.Cells(fila, 10).Value
x = x + 1
ElseIf Provee Like "*" & UCase(TextBox1.Value) & "*" Then
Me.ListBox1.AddItem
Me.ListBox1.List(x, 0) = Hoja6.Cells(fila, 24).Value
Me.ListBox1.List(x, 1) = Hoja6.Cells(fila, 25).Value
Me.ListBox1.List(x, 2) = Hoja6.Cells(fila, 26).Value
'Me.ListBox1.List(x, 3000000) = Hoja6.Cells(fila, 5).Value
Me.ListBox1.List(x, 3) = Hoja6.Cells(fila, 27).Value
Me.ListBox1.List(x, 4) = Hoja6.Cells(fila, 28).Value
'Me.ListBox1.List(x, 6000000) = Hoja6.Cells(fila, 10).Value
x = x + 1
End If
Next
Me.ListBox1.ColumnWidths = "220 pt;40 pt;40 pt;40 pt;80 pt"
End Sub


Private Sub UserForm_Initialize()
Me.ListBox1.ColumnCount = 5
Me.ListBox1.ColumnWidths = "220;40;40;40;80"
Me.ListBox1.RowSource = ""

Me.TextBox1.SetFocus
Frame1.Caption = "Modificar item existente: "
Me.ComboBox1.List = Array("Pza.", "Global", "Bolsa", "Rollo", "Kg", "Lb", "Lt", "m", "cm", "pulg", "Barra")
End Sub

Private Sub ListBox1_Click()
Dim i As Integer
For i = 0 To Me.ListBox1.ListCount - 1

If ListBox1.Selected(i) = True Then
'Me.TextBox2.Text = Me.ListBox1.List(i, 0)
'Me.TextBox4.Text = Me.ListBox1.List(i, 4)
Me.Label13.Caption = Me.ListBox1.List(i, 0)
Me.Label15.Caption = Me.ListBox1.List(i, 3)
Me.Label18.Caption = Me.ListBox1.List(i, 1)
Me.Label17.Caption = Me.ListBox1.List(i, 2)
Me.Label16.Caption = Me.ListBox1.List(i, 4)
End If
Next
End Sub

Estimado, 

Este es el código completo.

Me tomé la libertad de actualizar partes de tu código:

Private Sub CommandButton1_Click()
'Act.Por.Dante Amor
    If TextBox3.Value = "" Then
        MsgBox "Tiene que introducir un valor en el cuadro 'Precio'"
        Exit Sub
    End If
    If ComboBox1.Value = "" Then
        MsgBox "Tiene que seleccionar un valor de la listra desplegable en el cuadro 'Unidad'"
        Exit Sub
    End If
    If TextBox4.Value = "" Then
        MsgBox "Tiene que introducir un valor en el cuadro 'Proveedor'"
        Exit Sub
    End If
    If ListBox1.ListIndex = -1 Then
        MsgBox "Tiene que seleccionar un registro del list", vbExclamation, "ACTUALIZAR"
        Exit Sub
    End If
    '
    'toma le número de fila almacenado en la columna 5 del registro seleccionado
    fila = Val(ListBox1.List(ListBox1.ListIndex, 5))
    Cells(fila, 25).Value = TextBox3.Text
    Cells(fila, 26).Value = ComboBox1.Text
    Cells(fila, 28).Value = TextBox4.Text
    ActiveSheet.Range("X23").Value = ActiveSheet.Range("X23").Value + 1
    MsgBox "Registro actualizado"
End Sub
'
Private Sub TextBox1_Change()
    uf = Hoja6.Range("X" & Rows.Count).End(xlUp).Row
    If TextBox1 = "" Then
        Me.ListBox1.RowSource = ""
        Exit Sub
    End If
    'Me.ListBox1.RowSource = "Tabla16715"
    If Hoja6.AutoFilterMode Then Hoja6.AutoFilterMode = False
    Me.ListBox1.Clear
    'Me.ListBox1.RowSource = ""
    '
    x = 0
    For fila = 26 To uf
        strg = Hoja6.Cells(fila, 24).Value 'Variable para descripción
        Precio = Hoja6.Cells(fila, 25).Value 'Variable para Precio de venta
        Und = Hoja6.Cells(fila, 26).Value 'Variable para unidad de medida
        'Exist = Hoja6.Cells(fila, 5).Value 'Variable para existencia en stok
        codg = Hoja6.Cells(fila, 27).Value 'Variable para codigo y ubicacion
        provee = Hoja6.Cells(fila, 28).Value 'Variable para probeedor
        'Prec_copr = Hoja6.Cells(fila, 10).Value 'Variable para precio de venta
        '
        cadena = UCase(strg & codg & provee)
        If UCase(cadena) Like "*" & UCase(TextBox1.Value) & "*" Then
            Me.ListBox1.AddItem
            Me.ListBox1.List(x, 0) = Hoja6.Cells(fila, 24).Value
            Me.ListBox1.List(x, 1) = Hoja6.Cells(fila, 25).Value
            Me.ListBox1.List(x, 2) = Hoja6.Cells(fila, 26).Value
            Me.ListBox1.List(x, 3) = Hoja6.Cells(fila, 27).Value
            Me.ListBox1.List(x, 4) = Hoja6.Cells(fila, 28).Value
            'línea para guardar el número de fila
            Me.ListBox1.List(x, 5) = fila
            x = x + 1
        End If
    Next
    Me.ListBox1.ColumnWidths = "220 pt;40 pt;40 pt;40 pt;80 pt"
End Sub
'
Private Sub UserForm_Initialize()
    Me.ListBox1.ColumnCount = 5
    Me.ListBox1.ColumnWidths = "220;40;40;40;80"
    'Me.ListBox1.RowSource = ""
    '
    Me.TextBox1.SetFocus
    'Frame1.Caption = "Modificar item existente: "
    Me.ComboBox1.List = Array("Pza.", "Global", "Bolsa", "Rollo", "Kg", "Lb", "Lt", "m", "cm", "pulg", "Barra")
End Sub
'
Private Sub ListBox1_Click()
    i = ListBox1.ListIndex
    'Me.TextBox2.Text = Me.ListBox1.List(i, 0)
    'Me.TextBox4.Text = Me.ListBox1.List(i, 4)
    Me.Label13.Caption = Me.ListBox1.List(i, 0)
    Me.Label15.Caption = Me.ListBox1.List(i, 3)
    Me.Label18.Caption = Me.ListBox1.List(i, 1)
    Me.Label17.Caption = Me.ListBox1.List(i, 2)
    Me.Label16.Caption = Me.ListBox1.List(i, 4)
End Sub

Puse un par de comentarios para que veas cómo utilizo el número de fila.

.

'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

.

Feliz Año 2018

.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas