Need VBA Macro to Parse XLSX File - Appreciate Your Help

powerjak

New Member
Joined
Jan 28, 2003
Messages
29
I'm an intermediate Excel user, but "don't know diddly" about creating VBA Macros for recurrent processes. I'd really appreciate some help from someone more advanced than I am to create a Macro to reduce the time it takes to process these files.

I frequently have to pre-process Hourly Sample data to reduce data volume and sync it up against other data at the same time intervals for deeper analysis. The raw data is provided to me in a XLSX file in a format as follows:

Date Time Value1 Value2 Value3 Value4 Value5 Value6 Value(n)
9/2/2011 12:00:00 AM 50.21 42 47.38 79.77 84.74 1.16
9/2/2011 1:00:00 AM 49.1 42 47.39 78.76 83.69 0.83
9/2/2011 2:00:00 AM 48.18 42 47.38 78.39 83.52 0.62
9/2/2011 3:00:00 AM 48.29 42 47.38 78.39 83.52 0.62
9/2/2011 4:00:00 AM 47.8 42 47.38 78.39 83.44 0.61
9/2/2011 5:00:00 AM 47.08 42 47.38 78.39 82.3 0.41
9/2/2011 6:00:00 AM 47.39 42 47.38 76.68 81.42 0.12
9/2/2011 7:00:00 AM
9/2/2011 8:00:00 AM 46.35 42 47.38 74.73 79.85 -0.33
9/2/2011 9:00:00 AM 45.68 42 47.4 74.73 79.85 -0.36
9/2/2011 10:00:00 AM 48.79 42 48.1 74.85 80.14 -0.1
9/2/2011 11:00:00 AM 52.17 42 48.34 76.23 81.88 0.48
9/2/2011 12:00:00 PM 55.12 42 48.84 77.62 83.35 0.94
9/2/2011 1:00:00 PM 56.81 42 48.84 78.39 83.76 1.19
9/2/2011 2:00:00 PM 56.17 42 48.84 78.39 84.17 1.24
9/2/2011 3:00:00 PM 57.28 42 48.84 78.39 84.98 1.33
9/2/2011 4:00:00 PM 56.86 42 48.84 78.69 84.98 1.48
9/2/2011 5:00:00 PM 57.36 42 48.84 79.61 84.98 1.48
9/2/2011 6:00:00 PM 56.47 42 48.84 79.61 84.98 1.39
9/2/2011 7:00:00 PM 56.37 42 48.84 79.61 84.98 1.42
9/2/2011 8:00:00 PM 56.64 42 48.84 79.61 84.94 1.33
9/2/2011 9:00:00 PM 53.88 42 48.15 78.39 83.76 0.93
9/2/2011 10:00:00 PM 52.08 42 48.11 78.39 83.76 0.89
9/2/2011 11:00:00 PM 51.48 42 48.11 78.39 83.76 0.9
9/3/2011 12:00:00 AM 51.26 42 48.11 78.39 83.76 0.9
9/3/2011 1:00:00 AM 50.98 42 48.11 77.9 82.61 0.79
9/3/2011 2:00:00 AM 50.21 42 48.11 77.9 82.54 0.73
9/3/2011 3:00:00 AM 49.65 42 48.11 77.9 82.54 0.68
9/3/2011 4:00:00 AM 49.61 42 48.07 77.9 82.54 0.65
9/3/2011 5:00:00 AM 48.48 42 47.38 77.34 82.54 0.48
9/3/2011 6:00:00 AM 48.83 42 47.4 77.17 82.54 0.46
9/3/2011 7:00:00 AM 48.83 42 48.11 76.78 81.54 0.22
9/3/2011 8:00:00 AM 48.02 42 48.11 75.95 81.32 0.06
9/3/2011 9:00:00 AM 48.05 42 48.11 75.95 81.32 -0.06
9/3/2011 10:00:00 AM 49.8 42 48.11 75.95 81.32 0.2
9/3/2011 11:00:00 AM 50.91 42 48.11 75.95 82.05 0.59
9/3/2011 12:00:00 PM 51.83 42 48.11 75.95 82.54 0.59
9/3/2011 1:00:00 PM 53.34 42 48.18 76.74 82.54 0.66
9/3/2011 2:00:00 PM 53.94 42 48.84 77.17 82.54 0.8
9/3/2011 3:00:00 PM 54.11 42 48.84 77.17 82.54 0.87
9/3/2011 4:00:00 PM 54.91 42 48.84 77.17 83.21 0.94
9/3/2011 5:00:00 PM 53.91 42 48.84 77.17 83.76 0.9
9/3/2011 6:00:00 PM 54.52 42 48.84 77.17 83.76 0.92
9/3/2011 7:00:00 PM 53.38 42 48.49 77.17 83.56 0.86
9/3/2011 8:00:00 PM 50.54 42 48.11 77.17 82.54 0.47
9/3/2011 9:00:00 PM 49.9 42 48.11 77.17 81.87 0.26
9/3/2011 10:00:00 PM 48.4 42 48.11 77.17 81.32 0.2
9/3/2011 11:00:00 PM 47.76 42 47.5 77.17 81.32 0.13

Macro/VBA Parsing Process Specification
1. Worksheet to be processed has Header Row containing Column Titles. Must remain intact.
2. Number of Columns are variable. Example has Columns A through H, but could have many more. Must process all Columns until end of data.
3. Number of Rows are variable. Example has 48 Rows (excluding Header/Title Row), but could be many thousands of Rows total. Must process all Rows until end of data.
4. Each Row is an Hourly Data Sample of values taken Daily, On-the-Hour, beginning at 12:00:00 AM to 11:00:00 PM (24 Records/Rows per Day). File may start with any Calendar Date in format m/d/yyyy in Column A.
5. Column B contains the Hourly Time Stamp 12:00:00 AM through 11:00:00 PM for each Date in Column A.
6. Some data value cells may be blank although there are valid Date & Time values in Columns A & B. Such a Row must process correctly as long as the Date & Time values are valid. See Row 10 in "Example_BEFORE"
7. Rows to Extract have the Column B Time Stamps of 3:00:00 AM, 9:00:00 AM, 3:00:00 PM and 9:00:00 PM. (The Rows in "Example_BEFORE" that are highlighted in Yellow.)
8. DO NOT use Row Counting to capture every 6th Row because first Time Stamp may NOT be 12:00:00 AM, and Rows may be missing. Macro MUST process based on the actual Date & Time Values in Columns A & B.
9. Result of Macro Run leaves the original worksheet intact without change, and creates a NEW Worksheet in the Workbook containing each of the extracted Rows with their corresponding data.

Resulting New Worksheet After Macro Processing

Date Time Value1 Value2 Value3 Value4 Value5 Value6 Value(n)
9/2/2011 3:00:00 AM 48.29 42 47.38 78.39 83.52 0.62
9/2/2011 9:00:00 AM 45.68 42 47.4 74.73 79.85 -0.36
9/2/2011 3:00:00 PM 57.28 42 48.84 78.39 84.98 1.33
9/2/2011 9:00:00 PM 53.88 42 48.15 78.39 83.76 0.93
9/3/2011 3:00:00 AM 49.65 42 48.11 77.9 82.54 0.68
9/3/2011 9:00:00 AM 48.05 42 48.11 75.95 81.32 -0.06
9/3/2011 3:00:00 PM 54.11 42 48.84 77.17 82.54 0.87
9/3/2011 9:00:00 PM 49.9 42 48.11 77.17 81.87 0.26
Can some lend a helping hand?

Thanks in Advance.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
powerjak,

We do not know the worksheet names, or, where the data resides in each worksheet.

Can we see your workbook with before and after worksheets?

See below in my Signature block: You can upload your workbook to Box Net, mark the workbook for sharing, and provide us with a link to your workbook.
 
Upvote 0
It would be straightforward if the times were in 24 hour format. Is it possible to get the program that creates the Excel file to do that?
 
Upvote 0
It is reckless of me to give a solution where MVPs have already given suggestions

I have copied the data in sheet 3 as follows.

the result is in sheet 2

logic is
1.clears sheet1 data
2..copies from sheet3
3. it finds the required times and copies those rows in sheet 2

copy the following data in sheet3

Excel Workbook
ABCDEFGH
1datetimevalue1value2value3value4value5value6
29/2/2011########50.214247.3879.7784.741.16
39/2/2011########49.14247.3978.7683.690.83
49/2/2011########48.184247.3878.3983.520.62
59/2/2011########48.294247.3878.3983.520.62
69/2/2011########47.84247.3878.3983.440.61
79/2/2011########47.084247.3878.3982.30.41
89/2/2011########47.394247.3876.6881.420.12
9################
109/2/2011########46.354247.3874.7379.85-0.33
119/2/2011########45.684247.474.7379.85-0.36
129/2/2011########48.794248.174.8580.14-0.1
139/2/2011########52.174248.3476.2381.880.48
149/2/2011########55.124248.8477.6283.350.94
159/2/2011########56.814248.8478.3983.761.19
169/2/2011########56.174248.8478.3984.171.24
179/2/2011########57.284248.8478.3984.981.33
189/2/2011########56.864248.8478.6984.981.48
199/2/2011########57.364248.8479.6184.981.48
209/2/2011########56.474248.8479.6184.981.39
219/2/2011########56.374248.8479.6184.981.42
229/2/2011########56.644248.8479.6184.941.33
239/2/2011########53.884248.1578.3983.760.93
249/2/2011########52.084248.1178.3983.760.89
259/2/2011########51.484248.1178.3983.760.9
269/3/2011########51.264248.1178.3983.760.9
279/3/2011########50.984248.1177.982.610.79
289/3/2011########50.214248.1177.982.540.73
299/3/2011########49.654248.1177.982.540.68
309/3/2011########49.614248.0777.982.540.65
319/3/2011########48.484247.3877.3482.540.48
329/3/2011########48.834247.477.1782.540.46
339/3/2011########48.834248.1176.7881.540.22
349/3/2011########48.024248.1175.9581.320.06
359/3/2011########48.054248.1175.9581.32-0.06
369/3/2011########49.84248.1175.9581.320.2
379/3/2011########50.914248.1175.9582.050.59
389/3/2011########51.834248.1175.9582.540.59
399/3/2011########53.344248.1876.7482.540.66
409/3/2011########53.944248.8477.1782.540.8
419/3/2011########54.114248.8477.1782.540.87
429/3/2011########54.914248.8477.1783.210.94
439/3/2011########53.914248.8477.1783.760.9
449/3/2011########54.524248.8477.1783.760.92
459/3/2011########53.384248.4977.1783.560.86
469/3/2011########50.544248.1177.1782.540.47
479/3/2011########49.94248.1177.1781.870.26
489/3/2011########48.44248.1177.1781.320.2
499/3/2011########47.764247.577.1781.320.13
Sheet1


now try this macro "test"
and see whether it give you what you want.

Code:
Sub test()
Dim c As Range
Dim r As Range, ttime
Dim cfind As Range

Application.ScreenUpdating = False
undo
Worksheets("sheet1").Activate
For Each c In Range(Range("B2"), Range("B2").End(xlDown))
c = c.Offset(0, -1) + c
Next c

Set r = Range(Range("B2"), Range("B2").End(xlDown))
ttime = Range("B5").Value
'MsgBox ttime
Set cfind = r.Find(what:=ttime)
If Not cfind Is Nothing Then
'MsgBox cfind.Address
cfind.EntireRow.Copy
With Worksheets("sheet2")
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
End If
Do
ttime = ttime + 6 / 24
'MsgBox ttime
Set cfind = r.Find(what:=ttime)
If Not cfind Is Nothing Then
'MsgBox cfind.Address
cfind.EntireRow.Copy
With Worksheets("sheet2")
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
Else
GoTo exitsub
End If
Loop
exitsub:
Range("a1").EntireRow.Copy
Worksheets("sheet2").Activate
Range("a1").PasteSpecial
Range("B1").EntireColumn.AutoFit
Range("a1").Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub


Code:
Sub undo()
Worksheets("sheet1").Cells.Clear
Worksheets("sheet3").Cells.Copy Worksheets("sheet1").Range("a1")
Worksheets("sheet2").Cells.Clear
Worksheets("sheet1").Activate
Columns("B:B").NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"

End Sub
 
Upvote 0
It is reckless of me to give a solution where MVPs have already given suggestions

I have copied the data in sheet 3 as follows.

the result is in sheet 2

logic is
1.clears sheet1 data
2..copies from sheet3
3. it finds the required times and copies those rows in sheet 2

copy the following data in sheet3

Excel Workbook
ABCDEFGH
1datetimevalue1value2value3value4value5value6
29/2/20111/0/00 12:00 AM50.214247.3879.7784.741.16
39/2/20111/0/00 1:00 AM49.14247.3978.7683.690.83
49/2/20111/0/00 2:00 AM48.184247.3878.3983.520.62
59/2/20111/0/00 3:00 AM48.294247.3878.3983.520.62
69/2/20111/0/00 4:00 AM47.84247.3878.3983.440.61
79/2/20111/0/00 5:00 AM47.084247.3878.3982.30.41
89/2/20111/0/00 6:00 AM47.394247.3876.6881.420.12
9########1/0/00 7:00 AM
109/2/20111/0/00 8:00 AM46.354247.3874.7379.85-0.33
119/2/20111/0/00 9:00 AM45.684247.474.7379.85-0.36
129/2/20111/0/00 10:00 AM48.794248.174.8580.14-0.1
139/2/20111/0/00 11:00 AM52.174248.3476.2381.880.48
149/2/20111/0/00 12:00 PM55.124248.8477.6283.350.94
159/2/20111/0/00 1:00 PM56.814248.8478.3983.761.19
169/2/20111/0/00 2:00 PM56.174248.8478.3984.171.24
179/2/20111/0/00 3:00 PM57.284248.8478.3984.981.33
189/2/20111/0/00 4:00 PM56.864248.8478.6984.981.48
199/2/20111/0/00 5:00 PM57.364248.8479.6184.981.48
209/2/20111/0/00 6:00 PM56.474248.8479.6184.981.39
219/2/20111/0/00 7:00 PM56.374248.8479.6184.981.42
229/2/20111/0/00 8:00 PM56.644248.8479.6184.941.33
239/2/20111/0/00 9:00 PM53.884248.1578.3983.760.93
249/2/20111/0/00 10:00 PM52.084248.1178.3983.760.89
259/2/20111/0/00 11:00 PM51.484248.1178.3983.760.9
269/3/20111/0/00 12:00 AM51.264248.1178.3983.760.9
279/3/20111/0/00 1:00 AM50.984248.1177.982.610.79
289/3/20111/0/00 2:00 AM50.214248.1177.982.540.73
299/3/20111/0/00 3:00 AM49.654248.1177.982.540.68
309/3/20111/0/00 4:00 AM49.614248.0777.982.540.65
319/3/20111/0/00 5:00 AM48.484247.3877.3482.540.48
329/3/20111/0/00 6:00 AM48.834247.477.1782.540.46
339/3/20111/0/00 7:00 AM48.834248.1176.7881.540.22
349/3/20111/0/00 8:00 AM48.024248.1175.9581.320.06
359/3/20111/0/00 9:00 AM48.054248.1175.9581.32-0.06
369/3/20111/0/00 10:00 AM49.84248.1175.9581.320.2
379/3/20111/0/00 11:00 AM50.914248.1175.9582.050.59
389/3/20111/0/00 12:00 PM51.834248.1175.9582.540.59
399/3/20111/0/00 1:00 PM53.344248.1876.7482.540.66
409/3/20111/0/00 2:00 PM53.944248.8477.1782.540.8
419/3/20111/0/00 3:00 PM54.114248.8477.1782.540.87
429/3/20111/0/00 4:00 PM54.914248.8477.1783.210.94
439/3/20111/0/00 5:00 PM53.914248.8477.1783.760.9
449/3/20111/0/00 6:00 PM54.524248.8477.1783.760.92
459/3/20111/0/00 7:00 PM53.384248.4977.1783.560.86
469/3/20111/0/00 8:00 PM50.544248.1177.1782.540.47
479/3/20111/0/00 9:00 PM49.94248.1177.1781.870.26
489/3/20111/0/00 10:00 PM48.44248.1177.1781.320.2
499/3/20111/0/00 11:00 PM47.764247.577.1781.320.13
Sheet1




now try this macro "test"
and see whether it give you what you want.

Code:
Sub test()
Dim c As Range
Dim r As Range, ttime
Dim cfind As Range

Application.ScreenUpdating = False
undo
Worksheets("sheet1").Activate
For Each c In Range(Range("B2"), Range("B2").End(xlDown))
c = c.Offset(0, -1) + c
Next c

Set r = Range(Range("B2"), Range("B2").End(xlDown))
ttime = Range("B5").Value
'MsgBox ttime
Set cfind = r.Find(what:=ttime)
If Not cfind Is Nothing Then
'MsgBox cfind.Address
cfind.EntireRow.Copy
With Worksheets("sheet2")
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
End If
Do
ttime = ttime + 6 / 24
'MsgBox ttime
Set cfind = r.Find(what:=ttime)
If Not cfind Is Nothing Then
'MsgBox cfind.Address
cfind.EntireRow.Copy
With Worksheets("sheet2")
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
Else
GoTo exitsub
End If
Loop
exitsub:
Range("a1").EntireRow.Copy
Worksheets("sheet2").Activate
Range("a1").PasteSpecial
Range("B1").EntireColumn.AutoFit
Range("a1").Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub


Code:
Sub undo()
Worksheets("sheet1").Cells.Clear
Worksheets("sheet3").Cells.Copy Worksheets("sheet1").Range("a1")
Worksheets("sheet2").Cells.Clear
Worksheets("sheet1").Activate
Columns("B:B").NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"

End Sub
 
Upvote 0
I apologise to the moderators as at the time of posting my solution there was some problem with my computer and Internet connection and my reply was duplicated. SORRY

powerjak

you copy your data in sheet3 from A1 and run the macro "test"
the data I have posted may not be correct.
 
Upvote 0
venkat,

Thanks so much for your reply and code. I'll give this a test run tonight after I end my work day, and get back to you with results and any issues.

Thanks again ... back to you soon!

Sincerely,
powerjak
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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