Form para buscar y eliminar articulo e imagen (vba excel)
Tengo un form funcional pero no puedo eliminar la imagen asociada del articulo eliminado.
Coloco una imagen de como tengo el código de ese botón (cmb_eliminar):

CReo que falla el trozo de "arch=" no consigo hacerlo funcionar.
¿Alguna ayuda?
1 respuesta
Y qué hace el código, es decir, hasta dónde llega. Ejecuta el comando Kill o siempre te aparece el mensaje "No hay imagen para eliminar"
Puedes poner un ejemplo de lo que tienes en tu listbox. Y también puedes poner un ejemplo de cómo se ve el archivo con el explorador de windows. Lo que quiero ver es qué nombre aparece en el listbox y qué nombre aparece en tu carpeta.
¿Estás seguro que el archivo es jpg o jpeg?
El código solo se ejecuta hasta el mensaje de articulo eliminado. No me da la opción de eliminar la imagen.
La imagen esta asociada mediante el nombre del articulo, el mismo nombre que sale en el listbox tiene la imagen. Y si, son jpg p jpeg.
Pongo una captura:

Cómo estás cargando el listbox. Pon el código completo para ver cómo haces la carga. No lo pongas como imagen.
¿En cuál columna de la hoja tienes el nombre del archivo o en cuál columna del listbox tienes el nombre del archivo?
Dim h1
'
Private Sub cmb_eliminar_Click()
'Por.Dante Amor
If txt_buscar.Value = "" Then
MsgBox "Escribe un dato a buscar.", vbInformation, "fjpg GAMES"
Exit Sub
End If
If ListBox1.ListIndex = -1 Then
MsgBox "Selecciona un artículo.", vbInformation, "fjpg GAMES"
Exit Sub
End If
fila = ListBox1.List(ListBox1.ListIndex, 7)
If (MsgBox("¿Se eliminará el artículo seleccionado?.", vbCritical + vbYesNo, "fjpg GAMES") = vbYes) Then
h1.Rows(fila).Delete
MsgBox "Artículo eliminado.", vbInformation, "fjpg GAMES"
'====================================================================================================
''' ESTA PARTE CREO QUE ES LA QUE FALLA
ruta = ThisWorkbook.Path & "\imagenes\"
arch = fila & ".jpg"
If (MsgBox("¿Quieres eliminar la imagen del artículo eliminado?.", vbCritical + vbYesNo, "fjpg GAMES") = vbYes) Then
If Dir(ruta & arch) <> "" Then
Kill ruta & arch
img_articulo_buscar.Picture = Nothing
MsgBox "Se eliminó la imagen del artículo eliminado.", vbInformation, "fjpg GAMES"
Else
MsgBox "No hay imagen para eliminar.", vbInformation, "fjpg GAMES"
End If
End If
Else
Cancel = 1
End If
'=======================================================================================================
txt_buscar = ""
ListBox1.Clear
End Sub
Private Sub cmb_volver_Click()
Unload Me
End Sub
Private Sub cmb_buscar_Click()
'Por.Dante Amor
ListBox1.Clear
If txt_buscar.Value = "" Then
MsgBox "Escribe un dato a buscar.", vbInformation, "fjpg GAMES"
Exit Sub
End If
'
Set r = h1.Columns("B")
Set b = r.Find(txt_buscar, LookAt:=xlPart)
If Not b Is Nothing Then
celda = b.Address
Do
'detalle
ListBox1.AddItem h1.Cells(b.Row, "A")
ListBox1.List(ListBox1.ListCount - 1, 1) = h1.Cells(b.Row, "B")
ListBox1.List(ListBox1.ListCount - 1, 2) = h1.Cells(b.Row, "C")
ListBox1.List(ListBox1.ListCount - 1, 3) = h1.Cells(b.Row, "D")
ListBox1.List(ListBox1.ListCount - 1, 4) = h1.Cells(b.Row, "E")
ListBox1.List(ListBox1.ListCount - 1, 5) = h1.Cells(b.Row, "F")
ListBox1.List(ListBox1.ListCount - 1, 6) = h1.Cells(b.Row, "G")
ListBox1.List(ListBox1.ListCount - 1, 7) = b.Row
Set b = r.FindNext(b)
Loop While Not b Is Nothing And b.Address <> celda
End If
End Sub
Private Sub cmb_modificar_Click()
If txt_buscar.Value = "" Then
MsgBox "Escribe un dato a buscar.", vbInformation, "fjpg GAMES"
Exit Sub
End If
'Por.Dante Amor
If ListBox1.ListIndex = -1 Then
MsgBox "Selecciona un artículo.", vbInformation, "fjpg GAMES"
Exit Sub
End If
Unload Me
With frm_articulos_modificar2
.fila = ListBox1.List(ListBox1.ListIndex, 7)
.Show
End With
'
txt_buscar = ""
ListBox1.Clear
End Sub
Private Sub lb_volver_Click()
Unload Me
End Sub
Private Sub ListBox1_Click()
'Por.Dante Amor
ruta = ThisWorkbook.Path & "\Imagenes\"
arch = ListBox1.List(ListBox1.ListIndex, 1) & ".jpg"
If Dir(ruta & arch) <> "" Then
img_articulo_buscar.Picture = LoadPicture(ruta & arch)
img_articulo_buscar.PictureSizeMode = fmPictureSizeModeStretch
Else
If Dir(ruta & "0000.jpg") <> "" Then
img_articulo_buscar.Picture = LoadPicture(ruta & "0000.jpg")
img_articulo_buscar.PictureSizeMode = fmPictureSizeModeStretch
End If
End If
End Sub
Private Sub txt_buscar_Change()
If txt_buscar.Value = "" Then
ListBox1.Clear
cmb_modificar.Enabled = False
cmb_eliminar.Enabled = False
Else
cmb_modificar.Enabled = True
cmb_eliminar.Enabled = True
Exit Sub
End If
End Sub
'
Private Sub UserForm_Activate()
Set h1 = Sheets("ARTICULOS")
Sheets(2).Select
Range("A2").Select
txt_buscar.SetFocus
cmb_modificar.Enabled = False
cmb_eliminar.Enabled = False
End SubEste es el codigo que tengo en el form, me funciona todo, excepto la eliminacion de la imagen del articulo eliminado.
El nombre lo tengo en la columna 2 y en el listbox también es en la columna 2.
El código es una adaptación suya de otra vez que me ayudó.
Con esta instrucción identifica en cuál fila de la hoja se encuentra el registro que tienes marcado en el listbox.
fila = ListBox1.List(ListBox1.ListIndex, 7)
Pero para saber el nombre del archivo tienes que traerlo de la celda, por ejemplo:
arch = h1.Cells(fila, "B").Value & ".jpg"
Con eso, en la variable arch estoy poniendo el dato que tienes en la celda "B" y el número de fila
Entonces quedaría así:
Private Sub cmb_eliminar_Click()
'Por.Dante Amor
If txt_buscar.Value = "" Then
MsgBox "Escribe un dato a buscar.", vbInformation, "fjpg GAMES"
Exit Sub
End If
If ListBox1.ListIndex = -1 Then
MsgBox "Selecciona un artículo.", vbInformation, "fjpg GAMES"
Exit Sub
End If
fila = ListBox1.List(ListBox1.ListIndex, 7)
arch = h1.Cells(fila, "B").Value & ".jpg"
If (MsgBox("¿Se eliminará el artículo seleccionado?.", vbCritical + vbYesNo, "fjpg GAMES") = vbYes) Then
h1.Rows(fila).Delete
MsgBox "Artículo eliminado.", vbInformation, "fjpg GAMES"
ruta = ThisWorkbook.Path & "\imagenes\"
If (MsgBox("¿Quieres eliminar la imagen del artículo eliminado?.", vbCritical + vbYesNo, "fjpg GAMES") = vbYes) Then
If Dir(ruta & arch) <> "" Then
Kill ruta & arch
img_articulo_buscar.Picture = Nothing
MsgBox "Se eliminó la imagen del artículo eliminado.", vbInformation, "fjpg GAMES"
Else
MsgBox "No hay imagen para eliminar.", vbInformation, "fjpg GAMES"
End If
End If
Else
Cancel = 1
End If
txt_buscar = ""
ListBox1.Clear
End SubCuando hagas la prueba ve ejecutando línea por línea y me dices hasta dónde llega la macro. o si te envía algún error, qué dice el error y en cuál línea se detiene.
- Compartir respuesta