I have a folder full of *.csv files that I want to do a multiple sort on. I want to sort on column "A" then on Column "B" (note the data has headers)
Each csv file contains only one worksheet with headers above each of the five fields/columns.
Every *.csv file is the same. The columns cells can have any any number of rows (but every field has same number of rows).
When I run the code below I get a 1004 run time error and other times I get a type 13 mismatch .
The macro is save in the Personal.xlsb file.
The data I am useing is as follows...
<table style="width: 821px; height: 310px;" border="0" cellpadding="0" cellspacing="0"><colgroup><col style="mso-width-source:userset;mso-width-alt:4169;width:86pt" width="114"> <col style="mso-width-source:userset;mso-width-alt:4534;width:93pt" width="124"> <col style="mso-width-source:userset;mso-width-alt:3803;width:78pt" width="104"> <col style="mso-width-source:userset;mso-width-alt:1353;width:28pt" width="37"> <col style="mso-width-source:userset;mso-width-alt:2304;width:47pt" width="63"> </colgroup><tbody><tr style="height:15.0pt" height="20"> <td style="height:15.0pt;width:86pt" height="20" width="114">Meter</td> <td style="width:93pt" width="124"> Address</td> <td style="width:78pt" width="104"> Time</td> <td style="width:28pt" width="37"> Type</td> <td style="width:47pt" width="63">Reading</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-007</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">1.12</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-006
</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">58.713</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-005</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">220.28</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-008</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">23.75</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-301-351-004</td> <td> 02838-10-09437834</td> <td class="xl63" align="right">3/12/2010 23:52</td> <td align="right">8</td> <td align="right">350.02</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-301-351-003</td> <td> 02838-10-09437834</td> <td class="xl63" align="right">3/12/2010 23:52</td> <td align="right">8</td> <td align="right">1.317</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-201-351-001</td> <td> 02838-10-09437826</td> <td class="xl63" align="right">3/12/2010 23:50</td> <td align="right">8</td> <td align="right">1597.4</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-002-351-019</td> <td> 02838-10-09437829</td> <td class="xl63" align="right">3/12/2010 23:46</td> <td align="right">8</td> <td align="right">288.1</td> </tr> </tbody></table>
Each csv file contains only one worksheet with headers above each of the five fields/columns.
Every *.csv file is the same. The columns cells can have any any number of rows (but every field has same number of rows).
When I run the code below I get a 1004 run time error and other times I get a type 13 mismatch .
The macro is save in the Personal.xlsb file.
Code:
Sub LoopFiles()
Dim MyFileName, MyPath As String
Dim MyBook As Workbook
MyPath = "J:\MyFolderPath\test\"
MyFileName = Dir(MyPath & "*.csv")
Do Until MyFileName = ""
Workbooks.Open MyPath & MyFileName
Set MyBook = ActiveWorkbook
Application.Run "SortFields"
MyBook.Save
MyBook.Close
MyFileName = Dir
Loop
End Sub
Sub SortFields()
ActiveWorkbook.Worksheets(ActiveSheet).Sort.SortFields.Add Key:= _
Range("A2", Range(ActiveCell, ActiveCell.End(xlDown)).Select), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
ActiveWorkbook.Worksheets(ActiveSheet).Sort.SortFields.Add Key:= _
Range("C2", Range(ActiveCell, ActiveCell.End(xlDown)).Select), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets(ActiveSheet).Sort
.SetRange Range("A1", Range("A1").End(xlToRight).End(xlDown)).Select
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
<table style="width: 821px; height: 310px;" border="0" cellpadding="0" cellspacing="0"><colgroup><col style="mso-width-source:userset;mso-width-alt:4169;width:86pt" width="114"> <col style="mso-width-source:userset;mso-width-alt:4534;width:93pt" width="124"> <col style="mso-width-source:userset;mso-width-alt:3803;width:78pt" width="104"> <col style="mso-width-source:userset;mso-width-alt:1353;width:28pt" width="37"> <col style="mso-width-source:userset;mso-width-alt:2304;width:47pt" width="63"> </colgroup><tbody><tr style="height:15.0pt" height="20"> <td style="height:15.0pt;width:86pt" height="20" width="114">Meter</td> <td style="width:93pt" width="124"> Address</td> <td style="width:78pt" width="104"> Time</td> <td style="width:28pt" width="37"> Type</td> <td style="width:47pt" width="63">Reading</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-007</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">1.12</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-006
</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">58.713</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-005</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">220.28</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-003-351-008</td> <td> 02838-10-09437404</td> <td class="xl63" align="right">3/12/2010 23:53</td> <td align="right">8</td> <td align="right">23.75</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-301-351-004</td> <td> 02838-10-09437834</td> <td class="xl63" align="right">3/12/2010 23:52</td> <td align="right">8</td> <td align="right">350.02</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-301-351-003</td> <td> 02838-10-09437834</td> <td class="xl63" align="right">3/12/2010 23:52</td> <td align="right">8</td> <td align="right">1.317</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-201-351-001</td> <td> 02838-10-09437826</td> <td class="xl63" align="right">3/12/2010 23:50</td> <td align="right">8</td> <td align="right">1597.4</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">4030-002-351-019</td> <td> 02838-10-09437829</td> <td class="xl63" align="right">3/12/2010 23:46</td> <td align="right">8</td> <td align="right">288.1</td> </tr> </tbody></table>