OVH Cloud OVH Cloud

Raccourcir mon code VBA

25 réponses
Avatar
YANN24
Bonjour,
je voudrais savoir s'il est possible de raccourcir mon code VBA.
En fait je copie la valeur de la cellule de la page "Analyse", et je colle
sur ma page.
Puis je descend d'une ligne, et copie la valeur de la cellulle de la page
"Analyse", mais en descendant de 20 lignes.
cette opération se répette 22 fois.
Est-ce possible de racourcir le code?
Merci d'avance.
YANN

Ci-après, le code actuelle pour ma première ligne et ma seconde ligne de mon
tableau :
Sub MAJ_mai()
' MAJ_mai Macro
'ligne 01
Range("B116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("C116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("D116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("E116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("F116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("G116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("H116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[2]"
Range("I116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("J116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("K116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[6]"
'ligne 02
Range("B117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("C117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("D117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("E117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("F117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("G117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("H117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[2]"
Range("I117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("J117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("K117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[6]"

5 réponses

1 2 3
Avatar
YANN24
Re,

bon, si je comprend bien, avec ce code, celà me copie les valeurs qui sont
dans ma feuille "Analyse"? Moi c'est ce que je voudrais, aucune formule sur
ma feuille. (pour pouvoir l'envoyer en temps que données.
Merci.
YANN


Correctif
Range("j" & x & ",k" & x).Value = .Range("k" & y & ",q" & y).Value
remplacer le h par le q
désolé

"ru-th" a écrit dans le message de news:

variante

Sub test2()
With Sheets("analyse")
x = 116
y = 12
suite:
Range("b" & x & ":e" & x).Value = .Range("l" & y & ":o" & y).Value
Range("f" & x & ":g" & x).Value = .Range("g" & y & ":h" & y).Value
Range("h" & x & ":i" & x) = .Range("j" & y)
Range("j" & x & ",k" & x) = .Range("k" & y & ",h" & y)
If x = 116 Then
x = 117: y = 32
GoTo suite
End If
End With
End Sub

a+
rural thierry
"YANN24" a écrit dans le message de
news:


Bonjour,
je voudrais savoir s'il est possible de raccourcir mon code VBA.
En fait je copie la valeur de la cellule de la page "Analyse", et je
colle


sur ma page.
Puis je descend d'une ligne, et copie la valeur de la cellulle de la
page


"Analyse", mais en descendant de 20 lignes.
cette opération se répette 22 fois.
Est-ce possible de racourcir le code?
Merci d'avance.
YANN

Ci-après, le code actuelle pour ma première ligne et ma seconde ligne de
mon

tableau :
Sub MAJ_mai()
' MAJ_mai Macro
'ligne 01
Range("B116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("C116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("D116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("E116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[10]"
Range("F116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("G116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("H116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[2]"
Range("I116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("J116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[1]"
Range("K116").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-104]C[6]"
'ligne 02
Range("B117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("C117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("D117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("E117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[10]"
Range("F117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("G117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("H117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[2]"
Range("I117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("J117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[1]"
Range("K117").Select
ActiveCell.FormulaR1C1 = "=Analyse!R[-85]C[6]"











Avatar
Phil99999
Oui !!


Encore moi
juste encore un truc.
Comment adapter ce code pour ce ça me copie les données qui sont dans la
feuille "Analyse", sans qu'il y ait une formule dans ma cellule?
Avec l'idée de ru-th ?
Merci
YANN


Je ne comprends pas ta question !!! Tout dépend de tes données, non ?


Re,
Ok, je vais essayer.
PAr contre, j'ai un petit sousci : les chiffres de "=Analyse!R[-104]C[" &
CStr(col(k)) & "]" arrivent assez vite en positif. Est-ce un soucis ou normal?
Et si non, comment faire?
Merci.
YANN


Comme ça ??


Sub MAJ_mai_court()
Dim i, j, k As Integer
Dim col As Variant

col = Array("10", "10", "10", "10", "1", "1", "2", "1", "1", "6")

For i = 116 To 117
k = 0
For j = 2 To 11

If i = 116 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[-104]C[" & CStr(col(k)) & "]"
Else
Cells(i, j).FormulaR1C1 = "=Analyse!R[-85]C[" & CStr(col(k)) & "]"
End If

If Cells(i, j).Value = 0 Then
Cells(i, j).Value = ""
End If

k = k + 1
Next j
Next i


End Sub









Avatar
Clément Marcotte
je me souviens qu'il y avait déjà à l'époque, un vieux ronchon
lyonnais,

il existe encore ?


Oh que si ;-)

--
On n' apprend pas à un vieux singe à faire des grimaces

Avatar
YANN24
Voici ma macro grace à Phil.
Comment dois-je adapter ta macro à celle-ci ?

---------
'Mise à jour mois de JANVIER
Sub MAJ_janvier()
Dim i, j, k As Integer
Dim col As Variant

col = Array("10", "10", "10", "10", "1", "1", "2", "1", "1", "6")

For i = 8 To 29
k = 0
For j = 2 To 11

If i = 8 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[0]C[" & CStr(col(k)) & "]"
Else
If i = 9 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[19]C[" & CStr(col(k)) & "]"
Else
If i = 10 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[38]C[" & CStr(col(k)) & "]"
Else
If i = 11 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[57]C[" & CStr(col(k)) & "]"
Else
If i = 12 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[76]C[" & CStr(col(k)) & "]"
Else
If i = 13 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[95]C[" & CStr(col(k)) & "]"
Else
If i = 14 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[114]C[" & CStr(col(k)) & "]"
Else
If i = 15 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[133]C[" & CStr(col(k)) & "]"
Else
If i = 16 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[152]C[" & CStr(col(k)) & "]"
Else
If i = 17 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[171]C[" & CStr(col(k)) & "]"
Else
If i = 18 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[190]C[" & CStr(col(k)) & "]"
Else
If i = 19 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[209]C[" & CStr(col(k)) & "]"
Else
If i = 20 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[228]C[" & CStr(col(k)) & "]"
Else
If i = 21 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[247]C[" & CStr(col(k)) & "]"
Else
If i = 22 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[266]C[" & CStr(col(k)) & "]"
Else
If i = 23 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[285]C[" & CStr(col(k)) & "]"
Else
If i = 24 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[304]C[" & CStr(col(k)) & "]"
Else
If i = 25 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[323]C[" & CStr(col(k)) & "]"
Else
If i = 26 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[342]C[" & CStr(col(k)) & "]"
Else
If i = 27 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[361]C[" & CStr(col(k)) & "]"
Else
If i = 28 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[380]C[" & CStr(col(k)) & "]"
Else
If i = 29 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[399]C[" & CStr(col(k)) & "]"
Else
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If

k = k + 1
Next j
Next i
End Sub
---------------


**************
Sub test2()
With Sheets("analyse")
x = 8
y = 12
suite:
Range("b" & x & ":e" & x).Value = .Range("l" & y & ":o" & y).Value
Range("f" & x & ":g" & x).Value = .Range("g" & y & ":q" & y).Value
Range("h" & x & ":i" & x) = .Range("j" & y)
Range("j" & x & ",k" & x) = .Range("k" & y & ",q" & y)
If x = 8Then
x = 9: y = 32
x = 10: y = 52
x = 11: y = 72
...
x = 29: y = 312
GoTo suite
End If
End With
End Sub
*****************
Avatar
YANN24
OK, merci.
YANN


Oui !!


Encore moi
juste encore un truc.
Comment adapter ce code pour ce ça me copie les données qui sont dans la
feuille "Analyse", sans qu'il y ait une formule dans ma cellule?
Avec l'idée de ru-th ?
Merci
YANN


Je ne comprends pas ta question !!! Tout dépend de tes données, non ?


Re,
Ok, je vais essayer.
PAr contre, j'ai un petit sousci : les chiffres de "=Analyse!R[-104]C[" &
CStr(col(k)) & "]" arrivent assez vite en positif. Est-ce un soucis ou normal?
Et si non, comment faire?
Merci.
YANN


Comme ça ??


Sub MAJ_mai_court()
Dim i, j, k As Integer
Dim col As Variant

col = Array("10", "10", "10", "10", "1", "1", "2", "1", "1", "6")

For i = 116 To 117
k = 0
For j = 2 To 11

If i = 116 Then
Cells(i, j).FormulaR1C1 = "=Analyse!R[-104]C[" & CStr(col(k)) & "]"
Else
Cells(i, j).FormulaR1C1 = "=Analyse!R[-85]C[" & CStr(col(k)) & "]"
End If

If Cells(i, j).Value = 0 Then
Cells(i, j).Value = ""
End If

k = k + 1
Next j
Next i


End Sub











1 2 3