Sorting with Automated Program

kryan84

New Member
Joined
Jun 15, 2016
Messages
1
Hi All, I'm an avid reader to this forum and you all have been a great help to me since I've started using VBA and macros more actively.

I'm still pretty amateur to this but I've been working with my Dad (whose got a pretty good handle on these kinds of things) to create an automated program that will take an excel sheet that has data records for every minute over a two week period in a excel sheet, separate the data into byDay periods, and save as a .csv file so that we can later use a stat program to read the data. The program utilizes both Access and Excel. Access is used to organize the data into tables by day and then each table is exported out to their own excel sheets and saved as a .csv file.

Now the problem is that when I look into the by day excel files, sometimes they are not sorted correctly, meaning instead of the record beginning at 0:01 (12:01 AM), a random chunk of data will be at the top of the sheet.

Here's what we wrote in the module regarding the the format of the excel sheet. We know it has to be something in excel, because we are able to check the access tables and see that they all are in the correct order.

Code:
 'Make sure excel is visible on the screen        
XlBook.Windows(1).Visible = True
        'Define the sheet in the Workbook as XlSheet
        Set XlSheet = XlBook.Worksheets(1)
        'Insert Row and the Value in the Excel sheet starting at specified cell
        XlSheet.Range("A1:D1440").Sort key1:=Range("A1:B1440"), order1:=xlAscending 'Sorts data (from cell A3 to last data entry in that column) in ascending order
        Pause (3)
        XlSheet.Rows(1).EntireRow.Insert
        XlSheet.Range("A1") = "PLOT TITLE: " + CStr(DatePart("m", X)) + "." + CStr(DatePart("d", X)) + "." + CStr(DatePart("yyyy", X)) + "_" + cow
        XlSheet.Rows(2).EntireRow.Insert
        XlSheet.Range("A2") = "#"
        XlSheet.Range("B2") = "Date Time, GMT-05:00"
        XlSheet.Range("C2") = "Y Accel, g"
        XlSheet.Range("D2") = "Z Accel, g"
        Pause (5) 'Executes a 2 second delay. Excel automation is tempermental.
        XlBook.Save
        XlBook.Close True
        'Status this day's record as "processed"
        dqt.Edit
            dqt!Processed = True
            dqt!TableName = y
            dqt!CSVLocation = filepath
        dqt.Update


Else
    MsgBox "There are no records left to process."
End If
PROC_EXIT:
  Exit Sub
  
PROC_ERR: 
      Debug.Print Err.Description 
        Stop
    Resume
    
End Sub

If you are able to see what may be the problem, let me know and I will be very grateful for it. I've spent quite a long time on what seems to me to be a simple problem.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Forum statistics

Threads
1,214,899
Messages
6,122,155
Members
449,068
Latest member
shiz11713

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