Pb condition de manipulation de "SI"

euro.speed

XLDnaute Nouveau
Bonjour,

j'ai un module qui reprend les infos d'un tableau pour écrire des colonnes:

IL FAUT QUE JE PUISSE ECRIRE COLONNE APRES COLONNE et non pas par ligne.

VAR1 = 1
for i = 1 to 100

VAR1 = VAR1 + i
'écriture colonne B:
SI VAR1 = "A" then ChampColonneB = "formuleA"
SI VAR1 = "B" then ChampColonneB = "formuleB"

'écriture colonne c:
SI VAR1 = "A" then ChampColonneC = "BLABLA" ' le pb est là car j'ai déjà posé la condition SI VAR1 = "A"
SI VAR1 = "B" then ChampColonneC = "BLABLABLA"

next i

ceci est un exemple mais dans mon programme si VAR = "N", ça fonctionne quand même ???? Mais pas avec "A", "D" et "X" où est la logique ?

Est ce que je suis obligé de faire successivement 3 fois la même boucle ? Où existe-il une combine ?
 

Robert

XLDnaute Barbatruc
Repose en paix
Re : Pb condition de manipulation de "SI"

Bonjour euro.speed, bonjour le forum,

Je ne comprends pas bien ni tes codes ni ta question mais je te propose ça :

Code:
Sub Macro1()
 
VAR1 = 1
For i = 1 To 100
 
    VAR1 = VAR1 + i
 
    If VAR1 = "A" Then
        ChampColonneB = "formuleA"
        ChampColonneC = "BLABLA"
    End If
 
    If VAR1 = "B" Then
        ChampColonneB = "formuleB"
        ChampColonneC = "BLABLABLA"
    End If
Next i
End Sub
 

porcinet82

XLDnaute Barbatruc
Re : Pb condition de manipulation de "SI"

Bonjour,

Je sais bien que je suis fatigué et pas tres futé mais si VAR1 = 1+i, je ne vois pas trop comment il peut etre egal à "A" ou "B" ??????

Ou alors l'ami Robert est une fois de plus devin !!! Au quel cas, Robert, je te serait reconnaissant de me livrer ton secret ;)

@+
 

pierrejean

XLDnaute Barbatruc
Re : Pb condition de manipulation de "SI"

bonjour

pour ma part je proposerai;

for i = 1 to 100

VAR1 = VAR1 + i
'écriture colonne B:
SI VAR1 = "A" then
ChampColonneB = "formuleA"
ChampColonneC="BLABLA"

SI VAR1 = "B" then ChampColonneB = "formuleB"
'écriture colonne c:
SI VAR1 = "B" then ChampColonneC = "BLABLABLA"

next i
 

euro.speed

XLDnaute Nouveau
Re : Pb condition de manipulation de "SI"

Merci pour ta réponse Robert,

Mais pour des soucis de présentation, je souhaite écrire colonne par colonne.
Mais c'est pour des soucis de clareté, car en plus, j'ai plusieurs conditions contradictoire d'une colonne à l'autre.

exemple :

si VAR1 = "A" or "X" then colonneB = formuleA

si VAR1 = "A" then colonneC = formuleA
si VAR1 = "X" then colonneC = formuleX
 

Robert

XLDnaute Barbatruc
Repose en paix
Re : Pb condition de manipulation de "SI"

Robert à dit:
Bonjour euro.speed, bonjour le forum,

Je ne comprends pas bien ni tes codes ni ta question mais je te propose ça :

Pourtant c'est clair non ? Romain.

Mais sur ce coup j'abandonne... Vraiment pas assez de clarté. Je sens venir le plan des 45 réponses avec chaque fois un "oui mais"....
 

euro.speed

XLDnaute Nouveau
Re : Pb condition de manipulation de "SI"

:D Bon désolé si j'ai du mal à me faire comprendre, enfin bon j'ai pris la décision de refaire la boucle pour l'écriture de chaqu'une de mes colonnes (appellées groupe 1,2,3,..).

pour info voici mon code (son but final sera d'écrire un module permettant d'export d'une base access qui comporte 104 tables et 1400 champs) d'où l'intérêt de créer une module de ~200 lignes qui va me générer un autre de ~ 5000 lignes au final je pense).
:cool:

pour info mon prog :

Sub CreationFeuille()
'effacer les 104 feuilles
Application.DisplayAlerts = False
Dim Feuille As Worksheet
On Error Resume Next
Application.DisplayAlerts = False
For Each Feuille In Worksheets
If (Feuille.Name <> "Matrice") Then
Feuille.Delete
End If
Next
Application.DisplayAlerts = True
'créer les feuilles pour les 104 tables
NT = 1
For i = 1 To 104
Sheets("Matrice").Select
NT = NT + 1
NumCellule = "A" & NT
Sheets.Add.Name = Range(NumCellule).Value
Sheets("Matrice").Select
DebutCopie = "A" & NT
FinCopie = "CM" & NT
PlageCopie = DebutCopie & ":" & FinCopie
Sheets("Matrice").Select
Range(PlageCopie).Select
Application.CutCopyMode = False
Selection.Copy
NameTable = Range(NumCellule).Value
Sheets(NameTable).Select
Range("A3").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True

'boucle pour ajouter le format de chaque champ
NC = 3
For j = 1 To 93
NC = NC + 1
ChampReference = "A" & NC
ChampLength = "B" & NC
ChampType = "C" & NC
ChampJustification = "D" & NC
ChampDecimalPlacement = "E" & NC

If Range(ChampReference).Value <> "" Then
Range(ChampLength).FormulaLocal = "=RECHERCHEV($" & ChampReference & ";Matrice!$B$111:$H$1040;4;FAUX)"
Range(ChampType).FormulaLocal = "=RECHERCHEV($" & ChampReference & ";Matrice!$B$111:$H$1040;5;FAUX)"
Range(ChampJustification).FormulaLocal = "=RECHERCHEV($" & ChampReference & ";Matrice!$B$111:$H$1040;6;FAUX)"
Range(ChampDecimalPlacement).FormulaLocal = "=RECHERCHEV($" & ChampReference & ";Matrice!$B$111:$H$1040;7;FAUX)"
End If
Next j


' Ecriture Groupe1
NC = 3
For k = 1 To 93
NC = NC + 1
ChampReference = "A" & NC
ChampLength = "B" & NC
ChampType = "C" & NC
ChampJustification = "D" & NC
ChampDecimalPlacement = "E" & NC
Groupe1 = "G" & NC
If Range(ChampReference).Value <> "" Then
If Range(ChampType).Value = "A" Or "X" Then Range(Groupe1).Value = "str" & Range(ChampReference).Value & " = rcs" & NameTable & "!" & Range(ChampReference).Value
If Range(ChampType).Value = "D" Then Range(Groupe1).Value = "If Len(CStr(rcs" & NameTable & "!" & Range(ChampReference).Value & ")) > 10 Then str" & Range(ChampReference).Value & " = Replace((CStr(Format(CDbl(CStr(rcs" & NameTable & "!" & Range(ChampReference).Value & ")), ""0.0###E+""))), "","", ""."") Else str" & Range(ChampReference).Value & " = Replace((CStr(rcs" & NameTable & "!" & Range(ChampReference).Value & ")), "","", ""."")"
If Range(ChampType).Value = "N" Then
If Range(ChampDecimalPlacement).Value = "-" Then
Range(Groupe1).Value = "str" & Range(ChampReference).Value & " = rcs" & NameTable & "!" & Range(ChampReference).Value '* CDbl("1E6") CDbl(" & """1E6""" & ")"
Else
Range(Groupe1).Value = "str" & Range(ChampReference).Value & " = rcs" & NameTable & "!" & Range(ChampReference).Value & " * CDbl(" & """1E" & Range(ChampDecimalPlacement).Value & """)"
End If
End If
End If
Next k

' Ecriture Groupe2
NC = 3
For l = 1 To 93
NC = NC + 1
ChampReference = "A" & NC
NC2 = NC + 1
ChampReference2 = "A" & NC2 'utile pour condition "D"
ChampLength = "B" & NC
ChampType = "C" & NC
ChampJustification = "D" & NC
ChampDecimalPlacement = "E" & NC
Groupe2 = "O" & NC
If Range(ChampReference).Value <> "" Then
If Range(ChampType).Value = "D" Then Range(Groupe2).Value = "str" & Range(ChampReference).Value & " = IIf(Val(str" & Range(ChampReference).Value & ") * 1 = 0, IIf((Trim(str" & Range(ChampReference2).Value & ") <> """"), ""0"", """"), str" & Range(ChampReference).Value & ")"
If Range(ChampType).Value = "N" Then Range(Groupe2).Value = "If Val(str" & Range(ChampReference).Value & ") * 1 = 0 Then str" & Range(ChampReference).Value & " = """""
End If
Next l

' Ecriture Groupe3
NC = 3
For l = 1 To 93
NC = NC + 1
ChampReference = "A" & NC
ChampLength = "B" & NC
ChampType = "C" & NC
ChampJustification = "D" & NC
ChampDecimalPlacement = "E" & NC
Groupe3 = "V" & NC
If Range(ChampReference).Value <> "" Then
If Range(ChampType).Value = ...
If Range(ChampType).Value = ...
End If
Next l



Next i ' Incrémente le compteur des tables (feuilles)
'positionne le focus
Sheets("Matrice").Select
Range("a1").Select
End Sub
 

Discussions similaires

Statistiques des forums

Discussions
312 493
Messages
2 088 945
Membres
103 989
dernier inscrit
jralonso