Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Excel > Macros et VBA Excel
Macros et VBA Excel Vos questions relatives aux macros Excel, à l'utilisation de VBA et à l'automatisation de vos classeurs Excel.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 19/08/2011, 11h30   #1
Invité régulier
 
Femme
Étudiant
Inscription : août 2011
Messages : 63
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : Industrie

Informations forums :
Inscription : août 2011
Messages : 63
Points : 6
Points : 6
Par défaut Problème avec combobox

Bonjour,

Voilà j'ai crée un userform avec deux combobox qui sont alimenté par une plage de donnée dans une feuille excel. ces données sont des chiffres elles sont donc censé être de type numérique. J'ai réalisé un programme permettant de venir récupérer des valeurs dans un tableau à partir des valeurs rentrées dans les combobox. En gros si je met 1.5 dans ma combobox il me trouve toute les valeurs 1.5 dans la colonne du tableau.

Le problème que j'ai c'est que ceci fonctionne quand je rentre du texte dans mes cellules (toto) mais il ne parvient pas a trouver les valeurs numériques. Auriez vous une idée de comment remédier à ce pb?

Je met mon code
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Private Sub CommandButton8_Click()
 
Sheet3.Rows("2:70").Delete
 
iL2 = Sheet1.Cells(100, 2).Value
 
n = 2
ratio = ComboBox3.Value
hubratio = ComboBox4.Value
 
If ratio <> "" And hubratio <> "" Then
 
    For i = n To 160
    celluleratio = ("m" & i)
    cellulehubratio = ("k" & i)
 
    nomratio = Sheet5.Range(celluleratio).Value
    nomhubratio = Sheet5.Range(cellulehubratio).Value
 
     If nomratio = ratio Then
        If nomhubratio = hubratio Then
            Sheet5.Cells(i, "k").Copy Sheet3.Cells(iL2, 3)
            Sheet5.Cells(i, "m").Copy Sheet3.Cells(iL2, 4)
            Sheet5.Cells(i, "a").Copy Sheet3.Cells(iL2, 1)
         iL2 = iL2 + 1
        End If
     End If
    Next
 
ElseIf ratio <> "" And hubratio = "" Then
 
    For i = n To 160
        celluleratio = ("m" & i)
 
        nomratio = Sheet5.Range(celluleratio).Value
 
         If nomratio = ratio Then
            Sheet5.Cells(i, "k").Copy Sheet3.Cells(iL2, 3)
            Sheet5.Cells(i, "m").Copy Sheet3.Cells(iL2, 4)
            Sheet5.Cells(i, "a").Copy Sheet3.Cells(iL2, 1)
             iL2 = iL2 + 1
         End If
    Next
 
ElseIf ratio = "" And hubratio <> "" Then
 
    For i = n To 160
           cellulehubratio = ("k" & i)
 
           nomhubratio = Sheet5.Range(cellulehubratio).Value
 
            If nomhubratio = ratio Then
            Sheet5.Cells(i, "k").Copy Sheet3.Cells(iL2, 3)
            Sheet5.Cells(i, "m").Copy Sheet3.Cells(iL2, 4)
            Sheet5.Cells(i, "a").Copy Sheet3.Cells(iL2, 1)
                iL2 = iL2 + 1
            End If
    Next
 
End If
 
ComboBox3.Value = Empty
ComboBox4.Value = Empty
Sheet3.Select
 
End Sub
JulienLeno est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/08/2011, 12h57   #2
Membre éprouvé
 
Homme Franck PRESSE
Inscription : août 2010
Messages : 202
Détails du profil
Informations personnelles :
Nom : Homme Franck PRESSE
Âge : 38
Localisation : France, Nord (Nord Pas de Calais)

Informations forums :
Inscription : août 2010
Messages : 202
Points : 444
Points : 444
Bonjour,
Ton problème réside dans le fait qu'une ComboBox, si je ne m'abuse, renvoie un String.
Il faut donc convertir le contenu numérique de ta combobox en une variable de type numérique (Integer, Currency, Long, Double...)
Pour cela, tu peux utiliser : CInt() CCur() CLng() CDbl() etc...
Un peu comme ceci :
Code :
1
2
Dim MaVar As Double
MaVar = CDbl(ComboBox1)
Attention toutefois!!! Si ton ComboBox est vide, CDbl va renvoyer une erreur d'incomptaibilité de type. Il te faut donc "biaiser"... Peut être comme ceci :
Code :
1
2
3
4
5
6
7
Dim MaVar As Double
 
If ComboBox1 <> "" Then
    MaVar = CDbl(ComboBox1)
Else
    MaVar = 0
End If
__________________
Cordialement,
Franck P.


Ps : n'oubliez pas de placer vos posts comme "résolus" () si tel est le cas...
pijaku est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/08/2011, 13h30   #3
Invité régulier
 
Femme
Étudiant
Inscription : août 2011
Messages : 63
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : Industrie

Informations forums :
Inscription : août 2011
Messages : 63
Points : 6
Points : 6
J'ai essayé ta solution mais je ne parviens pas a m'en sortir. J'ai rajouté ceci et mon début de code est désormais celui ci mais rien n'y fait à chaque fois il bugg ou ne me trouve aucune réponse.
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Dim ratio As Double
 
If ComboBox3 <> "" Then
    ratio = CDbl(ComboBox3.Value)
Else
    ratio = 0
End If
 
Dim hubratio As Double
 
If ComboBox4 <> "" Then
    hubratio = CDbl(ComboBox3.Value)
Else
    hubratio = 0
End If
 
 
Sheet3.Rows("2:70").Delete
 
iL2 = Sheet1.Cells(100, 2).Value
 
n = 2
'ratio = ComboBox3.Value
'hubratio = ComboBox4.Value
 
If ratio <> "" And hubratio <> "" Then
 
    For i = n To 160
    celluleratio = ("m" & i)
    cellulehubratio = ("k" & i)
 
    nomratio = Sheet5.Range(celluleratio).Value
    nomhubratio = Sheet5.Range(cellulehubratio).Value
 
     If nomratio = ratio Then
        If nomhubratio = hubratio Then
            Sheet5.Cells(i, "k").Copy Sheet3.Cells(iL2, 3)
            Sheet5.Cells(i, "m").Copy Sheet3.Cells(iL2, 4)
            Sheet5.Cells(i, "a").Copy Sheet3.Cells(iL2, 1)
         iL2 = iL2 + 1
        End If
     End If
    Next
Est ce qu'il ne serait pas plus simple de convertir les données que je regarde dans mon tableau en string? Comment peut on faire ceci?

merci
JulienLeno est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/08/2011, 13h47   #4
Membre éprouvé
 
Homme Franck PRESSE
Inscription : août 2010
Messages : 202
Détails du profil
Informations personnelles :
Nom : Homme Franck PRESSE
Âge : 38
Localisation : France, Nord (Nord Pas de Calais)

Informations forums :
Inscription : août 2010
Messages : 202
Points : 444
Points : 444
C'est la même démarche mais en convertissant les données des cellules dans une variable String :
Code :
1
2
3
Dim nomration As String, nomhubratio As String
    nomratio = CStr(Sheet5.Range(celluleratio).Value)
    nomhubratio = CStr(Sheet5.Range(cellulehubratio).Value)
Tu pourras alors les comparer directement avec tes Combobox :

Code :
If nomratio = ComboBox1 And nomhubratio = ComboBox2 Then
__________________
Cordialement,
Franck P.


Ps : n'oubliez pas de placer vos posts comme "résolus" () si tel est le cas...
pijaku est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/08/2011, 14h01   #5
Invité régulier
 
Femme
Étudiant
Inscription : août 2011
Messages : 63
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : Industrie

Informations forums :
Inscription : août 2011
Messages : 63
Points : 6
Points : 6
Super! Cà marche nickel comme çà. J'ai passé les cellules en string et c'est bon merci a toi en tous cas!!!
JulienLeno est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/08/2011, 09h12   #6
Invité régulier
 
Femme
Étudiant
Inscription : août 2011
Messages : 63
Détails du profil
Informations personnelles :
Sexe : Femme
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : Industrie

Informations forums :
Inscription : août 2011
Messages : 63
Points : 6
Points : 6
Bonjour,

Je reviens vers vous car ce matin mon programme ne marche plus. Tout fonctionnait bien vendredi mais aujourd'hui quand je lance une recherche je ne parviens plus a otenir les résultats. Malgré la conversion des données en string. Je ne sais pas pourquoi. Je met mon code afin que vous puissiez voir si vous reperez une erreur.

Mais là j'avoue ne pas comprendre.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Private Sub CommandButton8_Click()
 
Dim nomratio As String
Dim nomhubratio As String
Sheet3.Rows("2:70").Delete
 
iL2 = Sheet1.Cells(100, 2).Value
 
n = 2
ratio = ComboBox3.Value
hubratio = ComboBox4.Value
 
If ratio <> "" And hubratio <> "" Then
 
    For i = n To 160
    celluleratio = ("m" & i)
    cellulehubratio = ("k" & i)
 
 
    nomratio = CStr(Sheet5.Range(celluleratio).Value)
    nomhubratio = CStr(Sheet5.Range(cellulehubratio).Value)
 
 
    'nomratio = Sheet5.Range(celluleratio).Value
    'nomhubratio = Sheet5.Range(cellulehubratio).Value
 
     If nomratio = ratio Then
        If nomhubratio = hubratio Then
            Sheet5.Cells(i, "k").Copy Sheet3.Cells(iL2, 3)
            Sheet5.Cells(i, "m").Copy Sheet3.Cells(iL2, 4)
            Sheet5.Cells(i, "a").Copy Sheet3.Cells(iL2, 1)
         iL2 = iL2 + 1
        End If
     End If
    Next
 
ElseIf ratio <> "" And hubratio = "" Then
 
    For i = n To 160
        celluleratio = ("m" & i)
 
        nomratio = CStr(Sheet5.Range(celluleratio).Value)
 
         If nomratio = ratio Then
            Sheet5.Cells(i, "k").Copy Sheet3.Cells(iL2, 3)
            Sheet5.Cells(i, "m").Copy Sheet3.Cells(iL2, 4)
            Sheet5.Cells(i, "a").Copy Sheet3.Cells(iL2, 1)
             iL2 = iL2 + 1
         End If
    Next
 
ElseIf ratio = "" And hubratio <> "" Then
 
    For i = n To 160
           cellulehubratio = ("k" & i)
 
             nomhubratio = CStr(Sheet5.Range(cellulehubratio).Value)
 
            If nomhubratio = hubratio Then
            Sheet5.Cells(i, "k").Copy Sheet3.Cells(iL2, 3)
            Sheet5.Cells(i, "m").Copy Sheet3.Cells(iL2, 4)
            Sheet5.Cells(i, "a").Copy Sheet3.Cells(iL2, 1)
                iL2 = iL2 + 1
            End If
    Next
 
End If
 
ComboBox3.Value = Empty
ComboBox4.Value = Empty
Sheet3.Select
 
End Sub
merci
JulienLeno est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 11h20.


 
 
 
 
Partenaires

Hébergement Web