Allouer un calcul dans VBA à une cellule avec loop
I
Bonjour,
je voudrais alloué le résultat d'un calcul à une selection de cellule mais je bloque sur cette partie de mon code global à pour but d'exporter les noms de produits et la marge sur coût (Margin) calculé via le code vers un fichier CSV.
Pouvez vous m'aider?
compute the Margin into cell C32 to J32
Range("C32").Select
Do Until MARGIN = 0
MARGIN = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
LoopSub test()
Dim TEXT_LINE As String ' Text line
' Variables for my differents fields
Dim BIKE_TYPE As String ' Type of bicycle produced
Dim MARGIN As Double ' Margin = Selling price without VAT - Cost excluding VAT
Dim COST As Double ' Total cost for producing the bicycle (excluding VAT)
Dim SELL_PRICE As Double ' Selling price of bicycle without VAT
Dim FilePath As String ' The path for accessing to the the text file
Dim NUMBER_OF_BICYCLES As Integer ' Number of bicyles to be produced
Dim TOTAL As Double
TOTAL = 0
'create a new sheet "MARGIN" that will be exported later to CSV
Sheets.Add.Name = "MARGIN"
'Go back to sheet "BICYCLES"
Sheets("BICYCLES").Select
'Copy paste transpose the product name into sheet "MARGIN"
Range("C2").Select
Range("C2:J2").Select
Selection.Copy
Sheets("MARGIN").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
'Go back to sheet "BICYCLES" to calcul the margin
Sheets("BICYCLES").Select
Range("C2").Select ' Select cell C2
Do While ActiveCell.Value <> "COST EXCLUDING VAT (TAX)"
BIKE_TYPE = ActiveCell.Value
MsgBox ("BIKE_TYPE: " & BIKE_TYPE)
ActiveCell.Offset(23, 0).Select ' Go to Row 25 - Cost excluding VAT
COST = ActiveCell.Value
MsgBox ("COST: " & COST)
ActiveCell.Offset(1, 0).Select ' Go to Row 26 - Selling Price without VAT
SELL_PRICE = ActiveCell.Value
MsgBox ("SELL_PRICE: " & SELL_PRICE)
ActiveCell.Offset(5, 0).Select ' Go to Row 31 for Quantity of Bicycles to produce
NUMBER_OF_BICYCLES = ActiveCell.Value
MsgBox ("NUMBER_OF_BICYCLES: " & NUMBER_OF_BICYCLES)
MARGIN = (SELL_PRICE - COST) * NUMBER_OF_BICYCLES ' Formula to compute Margin for number of bicyles produced for each type
MsgBox ("MARGIN: " & MARGIN)
'compute the Margin into cell C32 to J32
Range("C32").Select
Do Until MARGIN = 0
MARGIN = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Loop
Loop
'Copy paste transpose the calculated MARGIN into sheet "MARGIN"
Range("C32").Select
Range("C32:J32").Select
Selection.Copy
Sheets("MARGIN").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("BICYCLES").Select
Range("C32:J32").Clear
Sheets("MARGIN").Select
' Totalization
'TOTAL = TOTAL + MARGIN
'ActiveCell.Offset(-29, 1).Select ' Go to next column at Row 2 for Margin of next bike type
'Sheets("MARGIN").Select
' Cells(1, 2).Select
'MARGIN = (SELL_PRICE - COST) * NUMBER_OF_BICYCLES
' Do While MARGIN <> ""
' Store the value in the current cell
' ActiveCell.Value = MARGIN
' Totalize my values
' TOTAL = TOTAL + MARGIN
' Select next cell in the same column (Go Down)
'ActiveCell.Offset(1, 0).Select
' Loop
'Application.DisplayAlerts = False
'Sheets("MARGIN").Delete
'Application.DisplayAlerts = True
End SubSalut,
Pas de fichier, pas de réponse ??
Cordialement.
I
Bonjour,
J'ai trouver la solution par moi meme. Merci