Excel VBA fonction sort

Bonjour à tous,

J'aurais besoin d'aide sur une subroutine que j'essaye de créer dans laquelle je voudrait intégrer une fonction sort sur un critère.

Le role de ma subroutine est de calcule une marge sur cout d'un fichier de production et d'exporter le resultat dans un fichier csv.

La subroutine fonctionne parfaitement sans le sort mais une fois le sort intégrer elle bug.

Pouvez vous me dire ou j'ai fait une erreur?

Declaration of variables (local scope)
' Technical variables (for splitting my text file into Excel columns)
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

' Select the worksheet BICYCLES
Sheets("BICYCLES").Select

' Save as an ASCII file (MARGINS.TXT) in "writing mode" using VBA Excel GetSaveAsFileName method
FilePath = Application.GetSaveAsFilename(fileFilter:="CSV Files (*.CSV), *.CSV")

MsgBox ("PATH OF THE FILE " & FilePath)

' Open is a VBA Excel method for opening an external text file
' For Output : Because you want to export to a text file
' #1 is a handle. This value will replace the name of the text file in source code of this subroutine.
' The handle is like a nickname for the text file. It's a numeric value from 1 to 9 (please put a # before).

Open FilePath For Output As #1

' A loop for writing data to the text file
' Extracting data from the workskeet
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)

' Formula to compute Margin for number of bicyles produced for each type

MARGIN = (SELL_PRICE - COST) * NUMBER_OF_BICYCLES

MsgBox ("MARGIN: " & MARGIN)

TEXT_LINE = BIKE_TYPE & ";" & MARGIN
MsgBox ("TEXT_LINE: " & TEXT_LINE)
Print #1, TEXT_LINE

'Sort the margin in ascending

Range("MARGIN").Sort key1:=Range("MARGIN"), _
order1:=xlAscending, Header:=xlNo

' Totalization
TOTAL = TOTAL + MARGIN
ActiveCell.Offset(-29, 1).Select ' Go to next column at Row 2 for Margin of next bike type

Loop

TEXT_LINE = "TOTAL" & ";" & TOTAL
Print #1, TEXT_LINE

' To do at the end
Close #1 ' Close the text file
MsgBox ("End of treatment")

End Sub

bonsoir,

la méthode sort que tu utilises s'applique à une plage de cellule. si tu veux avoir un fichier trié en fonction de la marge, une façon de faire est de mettre toutes tes lignes dans un tableau, tu tries le tableau selon la marge puis tu écris les données dans ton fichier.

Bonjour et bienvenue,
Peux-tu préciser la version Excel utilisée ?
Joins un fichier csv et ton fichier Excel avec macro pour une aide adaptée du forum.
Cdlt.

Bonjour,

Merci pour votre aide, en effet j'ai aussi penser à la solution de le faire dans une autre feuille excel et exporter le résultat.

Ma version excel est la dernière dans office 365 en anglais. Voici les deux fichiers en question.

11margin.csv (205.00 Octets)
6bicycles-v1.xlsm (46.14 Ko)
Rechercher des sujets similaires à "vba fonction sort"