PB de code

anneh

XLDnaute Nouveau
Bonjour a tous,

J'ai un pb avec mon code. Quand j'ai la feuil1 qui est affiché ça fonction mais si feuil2 est ouvert ça marche plus

voici le code
Private Sub UserForm_Initialize()
With Worksheets("feuil1")
Dim Cell As Range
'LstView pour Trier
For Each Cell In Sheets("feuil1").Range("O2:O" & [O65000].End(xlUp).Row).SpecialCells(xlCellTypeVisible)
ListView1.ListItems.Add = Cell
Next
'liste triée sans doublon
For vLi = 1 To ListView1.ListItems.Count
ComboBox1 = ListView1.ListItems(vLi)
If ComboBox1.ListIndex = -1 Then ComboBox1.AddItem ListView1.ListItems(vLi)
Next
ListView1.ListItems.Clear
ComboBox1 = ""
End With
End Sub
Private Sub ComboBox1_Click()
With Worksheets("feuil1")
Dim Plage As Range, Est, Add As String, vLi As Integer, Vcol As Byte
With ListBox1
.Clear
.ColumnCount = 6
.ColumnWidths = "100;100;200;200;90"
Set Plage = Sheets("feuil1").Range("O2:O" & [O65000].End(xlUp).Row)
Set Est = Plage.Find(ComboBox1)
If Not Est Is Nothing Then
Add = Est.Address
Do
.AddItem Cells(Est.Row, 1)
For Vcol = 2 To 6
.List(vLi, Vcol - 1) = Cells(Est.Row, Vcol)
Next
vLi = vLi + 1
Set Est = Plage.FindNext(Est)
Loop While Not Est Is Nothing And Est.Address <> Add
End If

merci par avance pour votre aide
 

Papou-net

XLDnaute Barbatruc
Re : PB de code

Bonjour anneh,
Bonjour vgendron,

Voici une proposition de correction :

Code:
Private Sub UserForm_Initialize()
With ActiveSheet
Dim Cell As Range
'LstView pour Trier
For Each Cell In .Range("O2:O" & [O65000].End(xlUp).Row).SpecialCells(xlCellTypeVisible)
ListView1.ListItems.Add = Cell
Next
'liste triée sans doublon
For vLi = 1 To ListView1.ListItems.Count
ComboBox1 = ListView1.ListItems(vLi)
If ComboBox1.ListIndex = -1 Then ComboBox1.AddItem ListView1.ListItems(vLi)
Next
ListView1.ListItems.Clear
ComboBox1 = ""
End With
End Sub
Private Sub ComboBox1_Click()
With ActiveSheet
Dim Plage As Range, Est, Add As String, vLi As Integer, Vcol As Byte
End With
With ListBox1
.Clear
.ColumnCount = 6
.ColumnWidths = "100;100;200;200;90"
Set Plage = ActiveSheet.Range("O2:O" & [O65000].End(xlUp).Row)
Set Est = Plage.Find(ComboBox1)
If Not Est Is Nothing Then
Add = Est.Address
Do
.AddItem Cells(Est.Row, 1)
For Vcol = 2 To 6
.List(vLi, Vcol - 1) = Cells(Est.Row, Vcol)
Next
vLi = vLi + 1
Set Est = Plage.FindNext(Est)
Loop While Not Est Is Nothing And Est.Address <> Add
End If
End With
End Sub
C'est une correction "à la volée". Il est possible qu'il subsiste quelque(s) erreur(s), n'ayant pu vérifier sans l'application.

Cordialement.
 

anneh

XLDnaute Nouveau
Re : PB de code

voici mon fichier
en gros quand la feuille 1 est afficher tt marche quand c'est feuille 2 ça ne marche plus
clique sur accès au tableur pour revenir au tableau excel

merci
 

Pièces jointes

  • devis bdd test.xlsm
    122 KB · Affichages: 29

frangy

XLDnaute Occasionnel
Re : PB de code

Bonjour,

Pour éviter ce type de problème, il vaut mieux privilégier cette écriture
Code:
Private Sub UserForm_Initialize()
With Worksheets("feuil1")
  Dim Cell As Range
  'LstView pour Trier
  For Each Cell In .Range("O2:O" & .Range("O65000").End(xlUp).Row).SpecialCells(xlCellTypeVisible)

A+
 

Jam

XLDnaute Accro
Re : PB de code

Bonjour à tous,

Un p'tit Worksheets("Feuil1").Select en début de procédure devrait régler le problème.
Dès lors que tu fais mumuse avec les cellules - en-dehors d'y mettre des valeurs - il faut activer ta feuille en la sélectionant ;)

Bon courage

P'tit ajout: d'ailleurs dans ton code il y a un Activesheet...qui correspond à Feuil2 si c'est celle-ci qui est active...
 

Jam

XLDnaute Accro
Re : PB de code

RE,

De rien anneh.

Juste un petit conseil. Quand tu veux jouer avec plusieurs feuilles ou classeurs (surtout avec ceux-là), il vaut mieux utiliser des variables qui pointent sur eux après les avoir déterminés, par exemple:

VB:
Sub Test
Dim wksSource as Worksheet
Dim wksDestination as Worksheet

Set wksSource = Worksheets("toto")
Set wksDestination = Worksheets("titi")

....le code
End Sub

après tu jongles avec wksSource et wksDestination comme tu veux dans ton code. C'est (beaucoup) plus pratique et surtout ça évite ce genre d'erreur.

Bon courage.
 

Discussions similaires

Réponses
11
Affichages
308
Réponses
1
Affichages
179
Réponses
2
Affichages
279

Statistiques des forums

Discussions
312 367
Messages
2 087 645
Membres
103 627
dernier inscrit
nabil