Modifier une macro calculant des tranches

Bonjour, j'ai un fichier avec des montants, j'ai créé un code qui permet de calculer la tranche de chaque montant

per exemple si montant =1700 la tranche est de [1500;2000]. il me faut des tranches de 500, j'ai effectué le calcul de la façon suivante j'ai divisé 1700 par 500, et je n'ai gardé que le nbr entier càd 3, que j'ai multiplié par 500, ça me donne la borne inférieure et pour avoir la borne exterieur je met le résultat +500 ça me donne en borne supérieure 2000. Le code que j'ai mis en place pour effectuer ces opération est le suivant:

Public Sub tranche2()

Dim m As Single

Dim e As Integer

Dim x As Integer

Dim y As Integer

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row

m = Range("A" & i).Value

e = Int(m / 500)

x = e * 500

y = x + 500

Range("B" & i) = "[" & CStr(x) & "; " & CStr(y) & "]"

Next i

End Sub

Maintenant, je souhaiterai modifier mon code de la façon suivante si 500<m<750 alors afficher [500;750]

si 750<m<1000 alors afficher [750;1000]

si m>5000 alors afficher >5000

sinon garder le calcul d'avant (càd la macro que j'ai mis en place)

36tranche.xlsm (19.31 Ko)

Merci

Public Sub tranche2()
Dim m As Single
Dim e As Integer
Dim x As Integer
Dim y As Integer
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
m = Range("A" & i).Value
If m >= 750 And m<=1000 then
      Range("B" & i) = "[750;1000]"
Elseif m>=5000 then
      Range("B" & i) = ">5000"
Else
e = Int(m / 500)
x = e * 500
y = x + 500
Range("B" & i) = "[" & CStr(x) & "; " & CStr(y) & "]"
End if
Next i
End Sub

Je pense que cela devrait faire l'affaire !

LE CODE NE MARCHE PAS? IL ME MET COMME MESSAGE D ERREUR ELSE SANS IF, je ne sais pas ce qui ne va pas

Public Sub tranche2()

Dim m As Single

Dim e As Integer

Dim x As Integer

Dim y As Integer

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row

m = Range("A" & i).Value

If m >= 500 And m <= 750 Then Range("B" & i) = "[500;750]"

ElseIf m >= 750 And m <= 1000 Then Range("B" & i) = "[750;1000]"

ElseIf m >= 5000 Then Range("B" & i) = ">5000"

Else

e = Int(m / 500)

x = e * 500

y = x + 500

Range("B" & i) = "[" & CStr(x) & "; " & CStr(y) & "]"

End If

Next i

End Sub

Public Sub tranche2()
Dim m As Single
Dim e As Integer
Dim x As Integer
Dim y As Integer

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
    m = Range("A" & i).Value

    If (m >= 500 And m <= 750) Then
        Range("B" & i) = "[500;750]"

    ElseIf (m > 750 And m <= 1000) Then
        Range("B" & i) = "[750;1000]"

    ElseIf m >= 5000 Then
        Range("B" & i) = ">5000"

    Else
        e = Int(m / 500)
        x = e * 500
        y = x + 500
        Range("B" & i) = "[" & CStr(x) & "; " & CStr(y) & "]"
    End If

Next i
End Sub

Ca fonctionne chez moi.

ca venait surement du cas 750 où tu avais mis inférieur ou égale et supérieur ou égale en même temps.

Merci pour votre aide

Bonjour, J'ai modifié le code de façon à mettre le résultat non pas en colonne B, mais dans la première cononne vide. Le probleme c'est que j'ai le résultat de la dernière ligne de A dans toutes les cellules.

Public Sub tranche2()

Dim m As Single

Dim e As Integer

Dim x As Integer

Dim y As Integer

Dim j As Integer

Dim k As Long

Dim i As Integer

j = Cells(1, Columns.Count).End(xlToLeft).Column + 1

i = Range("A" & Rows.Count).End(xlUp).Row

Cells(1, j).Select

ActiveCell.FormulaR1C1 = "tranches"

For k = 2 To i

m = Range("A" & i).Value

If (m >= 500 And m <= 750) Then

Cells(k, j) = "[500;750]"

ElseIf (m > 750 And m <= 1000) Then

Cells(k, j) = "[750;1000]"

ElseIf m >= 5000 Then

Cells(k, j) = ">5000"

Else

e = Int(m / 500)

x = e * 500

y = x + 500

Cells(k, j) = "[" & CStr(x) & "; " & CStr(y) & "]"

End If

Next

End Sub

31tranche.xlsm (20.03 Ko)

Public Sub tranche2()

Dim m As Single

Dim e As Integer

Dim x As Integer

Dim y As Integer

Dim j As Integer

Dim i As Integer

j = Cells(1, Columns.Count).End(xlToLeft).Column + 1

Cells(1, j).Select

ActiveCell.FormulaR1C1 = "tranches"

For i = 2 To Range("A" & Rows.Count).End(xlUp).Row

m = Range("A" & i).Value

If (m >= 500 And m <= 750) Then

Cells(i, j) = "[500;750]"

ElseIf (m > 750 And m <= 1000) Then

Cells(i, j) = "[750;1000]"

ElseIf m >= 5000 Then

Cells(i, j) = ">5000"

Else

e = Int(m / 500)

x = e * 500

y = x + 500

Cells(i, j) = "[" & CStr(x) & "; " & CStr(y) & "]"

End If

Next

End Sub

Tu as changé ta variable de boucle for en k et tu ne l'as modifié pour le la valeur de m

==> m = Range("A" & i).Value devient m = Range("A" & k).Value

Et c'est bon

Rechercher des sujets similaires à "modifier macro calculant tranches"