Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Pb avec macro ComboBox

8 réponses
Avatar
David T.
Bonsoir à tous

Cette macro ne fonctionne pas tout a fait. Pourquoi ?
Cela m'affiche bien le nom des onglets + des Dialogue1, Dialogue2, etc puis
bug a la fonction
If ComboBox1.Selected(i) = True Then '


Précision : le ComboBox1 se trouve sur une feuille pas dans un UserForm.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(1 To Sheets.Count)
For i = 1 To Sheets.Count
Arr(i) = Sheets(i).Name
Next
ComboBox1.Clear
ComboBox1.List = Arr
End Sub

Private Sub ComboBox1_Click()
Dim i&
For i = 0 To ComboBox1.ListCount - 1
If ComboBox1.Selected(i) = True Then '
Range("P2").Value = ComboBox1.List(i)
Exit For
End If
Next i
End Sub

Merci à tous de votre aide très précieuse
David

--
direction-ternoise@(supprimerceci)wanadoo.fr

8 réponses

Avatar
michdenis
Bonsoir David,

Le problème avec cette procédure, c'est qu'elle est écrite comme si ton contrôle était une "ListBox" au lieu d'un
"Combobox". La méthode "Selected(i) est utilisable lorsque tu as une "listbox" dont la propriété :" Multiselect" a été
défini à : "fmMultiselectMulti"

'-------------------------------------
Private Sub ComboBox1_Click()
Dim i&
For i = 0 To ComboBox1.ListCount - 1
If ComboBox1.Selected(i) = True Then '
Range("P2").Value = ComboBox1.List(i)
Exit For
End If
Next i
End Sub
'-------------------------------------

Remplace la procédure précédente par :

'-----------------------
Private Sub ComboBox1_Click()
Dim i&
If ComboBox1.Text <> "" Then
Range("P2").Value = ComboBox1.List(i)
End If

End Sub
'-----------------------


Salutations!



"David T." <direction-ternoise@(supprimerceci)wanadoo.fr> a écrit dans le message de
news:
Bonsoir à tous

Cette macro ne fonctionne pas tout a fait. Pourquoi ?
Cela m'affiche bien le nom des onglets + des Dialogue1, Dialogue2, etc puis
bug a la fonction
If ComboBox1.Selected(i) = True Then '


Précision : le ComboBox1 se trouve sur une feuille pas dans un UserForm.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(1 To Sheets.Count)
For i = 1 To Sheets.Count
Arr(i) = Sheets(i).Name
Next
ComboBox1.Clear
ComboBox1.List = Arr
End Sub

Private Sub ComboBox1_Click()
Dim i&
For i = 0 To ComboBox1.ListCount - 1
If ComboBox1.Selected(i) = True Then '
Range("P2").Value = ComboBox1.List(i)
Exit For
End If
Next i
End Sub

Merci à tous de votre aide très précieuse
David

--
direction-ternoise@(supprimerceci)wanadoo.fr
Avatar
David T.
Bonjour denis

Merci de ta réponse, cela ne bug plus mais ne fonctionne toujours pas.
Cela innitialise bien la liste mais en plus mais des dialogue1,dialogue2,etc
dans cette liste
Que ce soit aussi bien avec un ListBox avec :

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(1 To Sheets.Count)
For i = 1 To Sheets.Count
Arr(i) = Sheets(i).Name
Next
ListBox1.Clear
ListBox1.List = Arr
End Sub

Private Sub ListBox1_Click()
Dim i&
If ListBox1.Text <> "" Then
Range("P2").Value = ListBox1.List(i)
End If
End Sub

OU avec un ComboBox :

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(1 To Sheets.Count)
For i = 1 To Sheets.Count
Arr(i) = Sheets(i).Name
Next
ComboBox1.Clear
ComboBox1.List = Arr
End Sub

Private Sub ComboBox1_Click()
Dim i&
If ComboBox1.Text <> "" Then
Range("P2").Value = ComboBox1.List(i)
End If
End Sub
Avatar
Alain CROS
Bonjour.

N'aurais tu pas des feuilles Boite de dialogue XL5 ?
Remplace Sheets par WorkSheets.

Alain CROS.

"David T." <direction-ternoise@(supprimerceci)wanadoo.fr> a écrit dans le message de news:
Bonjour denis

Merci de ta réponse, cela ne bug plus mais ne fonctionne toujours pas.
Cela innitialise bien la liste mais en plus mais des dialogue1,dialogue2,etc
dans cette liste
Que ce soit aussi bien avec un ListBox avec :

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(1 To Sheets.Count)
For i = 1 To Sheets.Count
Arr(i) = Sheets(i).Name
Next
ListBox1.Clear
ListBox1.List = Arr
End Sub

Private Sub ListBox1_Click()
Dim i&
If ListBox1.Text <> "" Then
Range("P2").Value = ListBox1.List(i)
End If
End Sub

OU avec un ComboBox :

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(1 To Sheets.Count)
For i = 1 To Sheets.Count
Arr(i) = Sheets(i).Name
Next
ComboBox1.Clear
ComboBox1.List = Arr
End Sub

Private Sub ComboBox1_Click()
Dim i&
If ComboBox1.Text <> "" Then
Range("P2").Value = ComboBox1.List(i)
End If
End Sub




Avatar
Michel Pierron
Bonjour David;
Pourquoi n'est-ce pas tout simplement:
Private Sub ComboBox1_Click()
If ComboBox1.ListIndex = -1 Then Exit Sub
Range("P2") = ComboBox1.Value
End Sub

MP

"David T." <direction-ternoise@(supprimerceci)wanadoo.fr> a écrit dans le message
de news:
Bonsoir à tous

Cette macro ne fonctionne pas tout a fait. Pourquoi ?
Cela m'affiche bien le nom des onglets + des Dialogue1, Dialogue2, etc puis
bug a la fonction
If ComboBox1.Selected(i) = True Then '


Précision : le ComboBox1 se trouve sur une feuille pas dans un UserForm.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(1 To Sheets.Count)
For i = 1 To Sheets.Count
Arr(i) = Sheets(i).Name
Next
ComboBox1.Clear
ComboBox1.List = Arr
End Sub

Private Sub ComboBox1_Click()
Dim i&
For i = 0 To ComboBox1.ListCount - 1
If ComboBox1.Selected(i) = True Then '
Range("P2").Value = ComboBox1.List(i)
Exit For
End If
Next i
End Sub

Merci à tous de votre aide très précieuse
David

--
direction-ternoise@(supprimerceci)wanadoo.fr




Avatar
David T.
Bonjour et merci Alain

En effet cela fonctionne trés bien.

Salutations
Avatar
David T.
Bonjour et merci Michel

Cela fonctionne aussi avec cette Proc, merci
A titre d'information, quelle est la différence si le résultat est le meme ?

Merci pour l'explication

Salutations
David
Avatar
David T.
Un petit truc qui dérange quand meme !!!

Le ComboBox1 ne s'affiche pas toujours. Il faut que le clic sur la feuille
pour qu'il apparaise puis
je sélectionne et je dois refaire un clic dans le ComboBox. Pas facile !!!

Es qu'il est possible qu'il reste toujours disponible. Je sélectionne et ca
referme la selection ?

Merci de votre aide

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Arr, i
ReDim Arr(4 To Worksheets.Count)
For i = 4 To Worksheets.Count
Arr(i) = Worksheets(i).Name
Next
ComboBox1.Clear
ComboBox1.List = Arr
End Sub

Private Sub ComboBox1_Change()
If ComboBox1.ListIndex = -1 Then Exit Sub
Range("P2") = ComboBox1.Value
End Sub
Avatar
Michel Pierron
Evident David; c'est beauoup plus simple.
MP

"David T." <direction-ternoise@(supprimerceci)wanadoo.fr> a écrit dans le message
de news:Oeqc%
Bonjour et merci Michel

Cela fonctionne aussi avec cette Proc, merci
A titre d'information, quelle est la différence si le résultat est le meme ?

Merci pour l'explication

Salutations
David