(RESOLU) Aide correction macro tri et aditem d'une listebox suivant combobox

Morihei

XLDnaute Nouveau
Bonjour à tous,

j'ai un souci avec code VBA ci-dessous, il fonctionne parfaitement si la liste recherchée suivant le critère trouve quelque chose => sinon vide alors plante au niveau For Each c In .Range("C15:C" & lastlig).SpecialCells(xlCellTypeVisible). Logique puisque c = Nothing.

Ma question comment tester c et si c="" alors msgbox : exit sub

Merci à vous

Code:
Private Sub ComboBox1_Change()
If Me.ComboBox1.ListIndex = -1 Then Exit Sub

RAZ ' rappel macro vide tout les textbox
Me.ListBox1.Clear

Sheets("Feuille1").Unprotect Password:="xxx"

Application.ScreenUpdating = False
With Me.ListBox1
    .Clear
    .Visible = False
End With
code = Me.ComboBox1.Value
If Me.ComboBox1.ListIndex > -1 Then
With Worksheets("Feuille1")
    .AutoFilterMode = False
    lastlig = Cells(.Rows.Count, "B").End(xlUp).Row
    .Range("B14:B" & lastlig).AutoFilter Field:=1, Criteria1:=code

' ' ' => PLANTE ICI ' ' For Each c In .Range("C15:C" & lastlig).SpecialCells(xlCellTypeVisible)
If c.Value = "" Then Exit Sub ' si c est vide alors => ne fonctionne pas renvoi erreur ligne au dessus

    With Me.ListBox1
        .AddItem c
        .List(.ListCount - 1, 1) = c.Offset(0, 2)
        .List(.ListCount - 1, 2) = c.Offset(0, 3)
        End With
    Next c
    Me.ListBox1.Visible = True
    .AutoFilterMode = False
    'End If

    
Sheets("Feuille1").Protect Password:="xxx"
End With
End If
End Sub
 
Dernière édition:

fanfan38

XLDnaute Barbatruc
Re : Aide correction macro tri et aditem d'une listebox suivant combobox

Bonjour

A l'endroit où sa plante tu peux mettre un on error resume next
ou mieux
en debut de macro: on error goto monerreur
et en fin de macro
exit sub
monerreur:
msgbox("il n'y a pas de " & code)
end sub

A+ François
 

Morihei

XLDnaute Nouveau
Re : Aide correction macro tri et aditem d'une listebox suivant combobox

Bonjour Fanfan38,

correction faite = OK, merci à vous ;)


Private Sub ComboBox1_Change()
If Me.ComboBox1.ListIndex = -1 Then Exit Sub

RAZ ' rappel macro vide tout les textbox
Me.ListBox1.Clear

On Error GoTo Erreur
Sheets("Feuille1").Unprotect Password:="xxx"

Application.ScreenUpdating = False
With Me.ListBox1
.Clear
.Visible = False
End With
code = Me.ComboBox1.Value
If Me.ComboBox1.ListIndex > -1 Then
With Worksheets("Feuille1")
.AutoFilterMode = False
lastlig = Cells(.Rows.Count, "B").End(xlUp).Row
.Range("B14:B" & lastlig).AutoFilter Field:=1, Criteria1:=code

For Each c In .Range("C15:C" & lastlig).SpecialCells(xlCellTypeVisible)

With Me.ListBox1
.AddItem c
.List(.ListCount - 1, 1) = c.Offset(0, 2)
.List(.ListCount - 1, 2) = c.Offset(0, 3)
End With
Next c
Me.ListBox1.Visible = True
.AutoFilterMode = False

Sheets("Feuille1").Protect Password:="xxx"
End With
End If
Exit Sub

Erreur:
MsgBox ("Pas de données à valider pour " & "- " & Me.ComboBox1 & " -" & " fin d'action."): RAZ: Me.ComboBox1 = "": Me.ComboBox1.SetFocus: Me.ListBox1.Visible = True: Me.ListBox1.Clear
Worksheets("Feuille1").AutoFilterMode = False
Sheets("Feuille1").Protect Password:="xxx"
End Sub

Petites corrections pour finaliser en cas d'erreur : :eek:
- permet d'effacer le filtre sur la feuille
Worksheets("Feuille1").AutoFilterMode = False

- Permet de remettre protection de la feuille
Sheets("Feuille1").Protect Password:="xxx"
 

Discussions similaires

Réponses
4
Affichages
209

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 206
Messages
2 086 210
Membres
103 158
dernier inscrit
laufin