Mismatch 13 error using Multiple Sort macro

leeman

New Member
Joined
Feb 9, 2010
Messages
11
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.

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
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>
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Take a look here: http://answers.yahoo.com/question/index?qid=20091229161846AApjUB4

Also, when you get the error, hit the debug button. It should highlight the exact line causing you problems. That is a good place to start.

Sometimes it is helpful to make the VB Editor about one-quarter the size of the screen, then use the F8 button to step through your VBA code line-by-line. This will give you the chance to see what the code is doing on the worksheet, and note where the cursor/active cell is when the error occurs. That may shed a lot of light on the issue.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top