Solved

# Data Convertion

Posted on 2014-02-25
209 Views
Hi Experts,

I want to modify my code which take care of time between 9:15 AM To 15:30 PM to convert 1 min data to 15 min data.

currently it exclude data 15:30 PM onwards but not before 9:15 AM.

Thanks
Data-Convert-V03.xlsm
0
Question by:itjockey
• 14
• 11
• 7

LVL 29

Accepted Solution

gowflow earned 500 total points
ID: 39885195
Well I have an approach where we need to exclude all data that is not in the desired frame first then to manipulate the said data.

I created a routine that will do this job by creating a new sheet called Temp in which the data will be the desired data that is in the frame of time that you have in Cell I10 and J10 whatever data outside that frame time is deleted from the temp file.

Once this pass is done then the normal routine shall group by 1 to 15.

here is the code for that and attachÃ© dis the new workbook.

If and when Temp exist then the routine will first delete the sheet and recreate it. I left the sheet Temp there for you the result. We can delete the sheet after all is ok.

``````Private Sub CleanDataNotInTimeFrame()
Dim WS As Worksheet
Dim WSTemp As Worksheet
Dim StTime As Date, EndTime As Date
Dim MaxRow As Long, I As Long

Set WS = ActiveSheet

'---> Chk to see if Sheet Temp exist then delete it first
On Error Resume Next
Set WSTemp = Sheets("Temp")
If Err = 0 Then
WSTemp.Delete
End If
On Error GoTo 0

WS.Copy after:=Sheets(Sheets.Count)
Set WSTemp = ActiveSheet
WSTemp.Name = "Temp"

MaxRow = WSTemp.UsedRange.Rows.Count
StTime = WSTemp.Range("I10")
EndTime = WSTemp.Range("J10")

'---> Start Process
For I = MaxRow To 2 Step -1
If TimeValue(WSTemp.Cells(I, "A")) < StTime Or TimeValue(WSTemp.Cells(I, "A")) > EndTime Then
WSTemp.Range("A" & I & ":F" & I).Delete
End If
Next I

End Sub

Sub Convert_1to15()
Dim WS As Worksheet
Dim WSConv As Worksheet
Dim MaxRow As Long, MaxRowConv As Long, I As Long, J As Long, Incr As Long, StandardIncr As Long
Dim StRow As Long, EndRow As Long

'---> Disable Events
With Application
.EnableEvents = False
.ScreenUpdating = False
End With

'---> CleanDataNotInTimeFrame
CleanDataNotInTimeFrame

Set WS = Sheets("Temp")
MaxRow = WS.Range("A" & WS.Rows.Count).End(xlUp).Row

'---> Check if Sheet 15 Min there
On Error Resume Next
Set WSConv = Sheets("15 Min")
If Err <> 0 Then
Set WSConv = ActiveSheet
WSConv.Name = "15 Min"
WS.Range("1:1").Copy WSConv.Range("A1")
WS.Range("A:A").Copy
WSConv.Range("A1").PasteSpecial xlPasteFormats
WSConv.Range("A1").Select
End If
MaxRowConv = 2

On Error GoTo 0

StandardIncr = 14
Incr = StandardIncr
'---> Start Process
For I = 2 + Incr To MaxRow Step Incr + 1
If I <> 2 + StandardIncr Then
'---> Check to see if Current set of dates falls all within the same date
If DateValue(WS.Cells(I - Incr, "A")) <> DateValue(WS.Cells(I, "A")) Then
J = I - Incr
Do
J = J + 1
Loop Until DateValue(WS.Cells(I - Incr, "A")) <> DateValue(WS.Cells(J, "A"))
Incr = J - 1 - (I - Incr)
I = I - StandardIncr
Else
Incr = StandardIncr
End If
End If

WSConv.Cells(MaxRowConv, "A") = WS.Cells(I, "A")
WSConv.Cells(MaxRowConv, "B") = WS.Cells(I - Incr, "B")
WSConv.Cells(MaxRowConv, "C") = WS.Application.WorksheetFunction.Max(WS.Range("C" & I - Incr & ":C" & I))
WSConv.Cells(MaxRowConv, "D") = WS.Application.WorksheetFunction.Min(WS.Range("D" & I - Incr & ":D" & I))
WSConv.Cells(MaxRowConv, "E") = WS.Cells(I, "E")
WSConv.Cells(MaxRowConv, "F") = WS.Application.WorksheetFunction.Sum(WS.Range("F" & I - Incr & ":F" & I))
MaxRowConv = MaxRowConv + 1
Incr = StandardIncr

Next I

'---> Enable Events
With Application
.EnableEvents = True
.ScreenUpdating = True
End With

MsgBox ("Conversion 1 to 15 Done")

End Sub
``````

You will only see 1 Sub that is Convert_1To15 that will first runt the other sub that cleans first.

Just for trial I suggest you change the frame time in I10 and J10 and see if the results are ok.

Let me know.
Data-Convert-V04.xlsm
0

LVL 48

Expert Comment

ID: 39885197
Hi,

pls try

``````Option Explicit

Sub Convert_1to15()

Dim WS As Worksheet
Dim WSConv As Worksheet
Dim MaxRow As Long, MaxRowConv As Long, FirstLine As Long, IdxLine As Long
Dim StRow As Long, EndRow As Long
Application.ScreenUpdating = False
Set WS = ActiveSheet
MaxRow = WS.UsedRange.Rows.Count

'---> Check if Sheet 15 Min there
On Error Resume Next
Set WSConv = Sheets("15 Min")
If Err <> 0 Then
Set WSConv = ActiveSheet
WSConv.Name = "15 Min"
WS.Range("1:1").Copy WSConv.Range("A1")
WS.Range("A:A").Copy
WSConv.Range("A1").PasteSpecial xlPasteFormats
MaxRowConv = 2
Application.CutCopyMode = False
Else
WSConv.Rows("2:" & Rows.Count).EntireRow.ClearContents
MaxRowConv = WSConv.UsedRange.Rows.Count + 1
End If
On Error GoTo 0

FirstLine = 2
IdxLine = 2

Do

Do While (TimeValue(WS.Range("A" & FirstLine)) < TimeValue("9:15:00") Or _
TimeValue(WS.Range("A" & FirstLine)) > TimeValue("15:30:00"))
FirstLine = FirstLine + 1
If WS.Cells(FirstLine, 1) = "" Then
MsgBox "Done!"
Exit Sub
End If
Loop

IdxLine = FirstLine
Do While ((CLng(Mid(TimeValue(WS.Range("A" & IdxLine)), 4, 2)) + 1) Mod 15 <> 0 And _
TimeValue(WS.Range("A" & IdxLine)) < TimeValue("15:30:00"))
IdxLine = IdxLine + 1

Loop

WSConv.Cells(MaxRowConv, "A") = WS.Cells(IdxLine, "A")
WSConv.Cells(MaxRowConv, "B") = WS.Cells(FirstLine, "B")
WSConv.Cells(MaxRowConv, "C") = WS.Application.WorksheetFunction.Max(WS.Range("C" & FirstLine & ":C" & IdxLine))
WSConv.Cells(MaxRowConv, "D") = WS.Application.WorksheetFunction.Min(WS.Range("D" & FirstLine & ":D" & IdxLine))
WSConv.Cells(MaxRowConv, "E") = WS.Cells(IdxLine, "E")
WSConv.Cells(MaxRowConv, "F") = WS.Application.WorksheetFunction.Sum(WS.Range("F" & FirstLine & ":F" & IdxLine))
MaxRowConv = MaxRowConv + 1

FirstLine = IdxLine + 1
Loop While WS.Cells(FirstLine, 1) <> ""

Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
``````
Regards
Data-Convert-V03.xlsm
0

LVL 29

Expert Comment

ID: 39885212
@Rgonzo1971

Just to be fair I tested this sample data and your routine fails. Just not competing here but trying to optimize wasted time for asker !!!

gowflow
Data-Convert-V04-rgonzo.xlsm
0

LVL 8

Author Closing Comment

ID: 39885242
Excellent
0

LVL 48

Expert Comment

ID: 39885271
@gowflow

Tks for the remark but it fails only  if you run the macro from the 15 min Sheets but since the user has a button on Sh 1 Min I used it as reference

And it seems that my code is 3times quicker since I only loop the data once with the same end result.

corrected Version

``````Sub Convert_1to15_Rgonzo1971()

Dim WS As Worksheet
Dim WSConv As Worksheet
Dim MaxRow As Long, MaxRowConv As Long, FirstLine As Long, IdxLine As Long
Dim StRow As Long, EndRow As Long
Dim BgnTime As Variant, EndTime As Variant

Application.ScreenUpdating = False
Set WS = Sheets("1 Min ")
MaxRow = WS.UsedRange.Rows.Count

'---> Check if Sheet 15 Min there
On Error Resume Next
Set WSConv = Sheets("15 Min")
If Err <> 0 Then
Set WSConv = ActiveSheet
WSConv.Name = "15 Min"
WS.Range("1:1").Copy WSConv.Range("A1")
WS.Range("A:A").Copy
WSConv.Range("A1").PasteSpecial xlPasteFormats
MaxRowConv = 2
Application.CutCopyMode = False
Else
WSConv.Rows("2:" & Rows.Count).EntireRow.ClearContents
MaxRowConv = WSConv.UsedRange.Rows.Count + 1
End If
On Error GoTo 0
BgnTime = WS.Range("I6")
EndTime = WS.Range("J6")

FirstLine = 2
IdxLine = 2

Do

Do While (TimeValue(WS.Range("A" & FirstLine)) < BgnTime Or _
TimeValue(WS.Range("A" & FirstLine)) > EndTime)
FirstLine = FirstLine + 1
If WS.Cells(FirstLine, 1) = "" Then
MsgBox "Done!"
Exit Sub
End If
Loop

IdxLine = FirstLine
Do While ((CLng(Mid(TimeValue(WS.Range("A" & IdxLine)), 4, 2)) + 1) Mod 15 <> 0 And _
TimeValue(WS.Range("A" & IdxLine)) < EndTime)
IdxLine = IdxLine + 1

Loop

WSConv.Cells(MaxRowConv, "A") = WS.Cells(IdxLine, "A")
WSConv.Cells(MaxRowConv, "B") = WS.Cells(FirstLine, "B")
WSConv.Cells(MaxRowConv, "C") = WS.Application.WorksheetFunction.Max(WS.Range("C" & FirstLine & ":C" & IdxLine))
WSConv.Cells(MaxRowConv, "D") = WS.Application.WorksheetFunction.Min(WS.Range("D" & FirstLine & ":D" & IdxLine))
WSConv.Cells(MaxRowConv, "E") = WS.Cells(IdxLine, "E")
WSConv.Cells(MaxRowConv, "F") = WS.Application.WorksheetFunction.Sum(WS.Range("F" & FirstLine & ":F" & IdxLine))
MaxRowConv = MaxRowConv + 1

FirstLine = IdxLine + 1
Loop While WS.Cells(FirstLine, 1) <> ""

Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
``````

Regards
0

LVL 8

Author Comment

ID: 39885293
@Rgonzo1971 I had change code but getting error message.
0

LVL 48

Expert Comment

ID: 39885323
HI

Pls try Example

I am not getting any error ( Could you explain Which error you are getting?)

Regards
Data-Convert-V05.xlsm
0

LVL 8

Author Comment

ID: 39885333

Thanks
0

LVL 29

Expert Comment

ID: 39885356
@Rgonzo1971

Sorry Just saw the post was at lunch.

I am sorry but you are wrong in your assumptions. The file I posted has only 1 sheet which is 1 Min and has only your code there.

Run it and you get an error.

I strongly suggest you test your code prior to posting as it is our duty to provide Askers a tested code as not always Askers knows how to test code.

Regards
gowflow
0

LVL 29

Expert Comment

ID: 39885380
This is the file you posted !!! with some more data at the end day

Same error !!!!
gowflow
Data-Convert-V05-rgonzo.xlsm
0

LVL 29

Expert Comment

ID: 39885399
Forgot to post the error

gowflow
0

LVL 48

Expert Comment

ID: 39885509
@gowflow

Thank you for your remarks but it works perfectly on my computer, I do not understand yet why it doesn't on yours or the itjockey's

I did test the code
I suppose TimeValue is the source of the problem but don't know why.

@itjockey
I won't post any comment on this question after this

Best Regards
0

LVL 29

Expert Comment

ID: 39885515
what version of Excel are you using ?
gowflow
0

LVL 8

Author Comment

ID: 39885525
Me Excel 2010 Professional 32 Bit.

Thanks
0

LVL 29

Expert Comment

ID: 39885529
I meant Rgonzo1971
gowflow
0

LVL 48

Expert Comment

ID: 39885555
@gowflow
Office Professional 2010 Plus
but it may be a localization problem (German version) however time is the same in EN version as in the German one ("hh:mm:ss")

Regards
0

LVL 29

Expert Comment

ID: 39886124
Well it is not localization it is your logic that is wrong. Well didn't want to go thru this but seems you insist that your logic is fine well here it is:

If I debug I get for this:
Mid(TimeValue(WS.Range("A" & IdxLine)), 4, 2) ---> "5:"

meaning that you went on a wrong foot assuming that the minutes are at the fourth character as it is clear in the whole file that you have
9:15
9:16
etc...
10:15
10:16
10:21
etc..

until 10 you are at position 3 for the minutes and after that you are at position 4

For your loop to work regardless you should force formatting and instead of this

``````Do While ((CLng(Mid(TimeValue(WS.Range("A" & IdxLine)), 4, 2)) + 1) Mod 15 <> 0 And _
TimeValue(WS.Range("A" & IdxLine)) < EndTime)
``````

you should have this:
``````Do While ((CLng(Mid(Format(TimeValue(WS.Range("A" & IdxLine)), "hh:mm"), 4, 2)) + 1) Mod 15 <> 0 And _
TimeValue(WS.Range("A" & IdxLine)) < EndTime)
``````

gowflow
0

LVL 48

Expert Comment

ID: 39886172
@gowflow
Thank you very much

The difference is due to the Windows Setting for Time

You have probably H:MM:SS and I have HH:MM:SS

By changing the setting I can recreate the error.

Learning every day...

Regards
0

LVL 8

Author Comment

ID: 39886229
Salutes both of you.....i like it.  :-)
0

LVL 29

Expert Comment

ID: 39886240
Well for sure we learn every day !!! that is my moto anyway.

Cheers
gowflow
0

LVL 48

Expert Comment

ID: 39886251
Just for reference my code using Split instead of Mid ( if only  I would have done it sooner)

``````Sub Convert_1to15() ' Version 5

Dim WS As Worksheet
Dim WSConv As Worksheet
Dim MaxRow As Long, MaxRowConv As Long, FirstLine As Long, IdxLine As Long
Dim StRow As Long, EndRow As Long
Dim BgnTime As Variant, EndTime As Variant

Application.ScreenUpdating = False
Set WS = Sheets("1 Min ")
MaxRow = WS.UsedRange.Rows.Count

'---> Check if Sheet 15 Min there
On Error Resume Next
Set WSConv = Sheets("15 Min")
If Err <> 0 Then
Set WSConv = ActiveSheet
WSConv.Name = "15 Min"
WS.Range("1:1").Copy WSConv.Range("A1")
WS.Range("A:A").Copy
WSConv.Range("A1").PasteSpecial xlPasteFormats
MaxRowConv = 2
Application.CutCopyMode = False
Else
WSConv.Rows("2:" & Rows.Count).EntireRow.ClearContents
MaxRowConv = WSConv.UsedRange.Rows.Count + 1
End If
On Error GoTo 0
BgnTime = WS.Range("I6")
EndTime = WS.Range("J6")

FirstLine = 2
IdxLine = 2

Do

Do While (TimeValue(WS.Range("A" & FirstLine)) < BgnTime Or _
TimeValue(WS.Range("A" & FirstLine)) > EndTime)
FirstLine = FirstLine + 1
If WS.Cells(FirstLine, 1) = "" Then
MsgBox "Done!"
Exit Sub
End If
Loop

IdxLine = FirstLine
Do While ((CLng(Split(TimeValue(WS.Range("A" & IdxLine)), ":")(1)) + 1) Mod 15 <> 0 And _
TimeValue(WS.Range("A" & IdxLine)) < EndTime)
IdxLine = IdxLine + 1

Loop

WSConv.Cells(MaxRowConv, "A") = WS.Cells(IdxLine, "A")
WSConv.Cells(MaxRowConv, "B") = WS.Cells(FirstLine, "B")
WSConv.Cells(MaxRowConv, "C") = WS.Application.WorksheetFunction.Max(WS.Range("C" & FirstLine & ":C" & IdxLine))
WSConv.Cells(MaxRowConv, "D") = WS.Application.WorksheetFunction.Min(WS.Range("D" & FirstLine & ":D" & IdxLine))
WSConv.Cells(MaxRowConv, "E") = WS.Cells(IdxLine, "E")
WSConv.Cells(MaxRowConv, "F") = WS.Application.WorksheetFunction.Sum(WS.Range("F" & FirstLine & ":F" & IdxLine))
MaxRowConv = MaxRowConv + 1

FirstLine = IdxLine + 1
Loop While WS.Cells(FirstLine, 1) <> ""

Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
``````
0

LVL 29

Expert Comment

ID: 39888213
@Rgonzo1971

While checking your routine, although found an error in your about the date due to windows setting, also do not hide that I found that you reported in sheet 15 Min 914 records while I was reporting only 913.

So looking further into this noticed that my loop was missing the last item !!! :(

I guess we are even !!

@ITjockey
If you decided to use my macro please disregard the previous version and use the below as it was missing the last item.

Tks Rgonzo1971, helped me catch a fault in mine.

Here is the new code and the attached file has the changes incorporated.

``````Sub Convert_1to15()
Dim WS As Worksheet
Dim WSConv As Worksheet
Dim MaxRow As Long, MaxRowConv As Long, I As Long, J As Long, Incr As Long, StandardIncr As Long
Dim StRow As Long, EndRow As Long

'---> Disable Events
With Application
.EnableEvents = False
.ScreenUpdating = False
End With

'---> CleanDataNotInTimeFrame
CleanDataNotInTimeFrame

Set WS = Sheets("Temp")
MaxRow = WS.Range("A" & WS.Rows.Count).End(xlUp).Row

'---> Check if Sheet 15 Min there
On Error Resume Next
Set WSConv = Sheets("15 Min")
If Err <> 0 Then
Set WSConv = ActiveSheet
WSConv.Name = "15 Min"
WS.Range("1:1").Copy WSConv.Range("A1")
WS.Range("A:A").Copy
WSConv.Range("A1").PasteSpecial xlPasteFormats
WSConv.Range("A1").Select
End If
MaxRowConv = 2

On Error GoTo 0

StandardIncr = 14
Incr = StandardIncr
'---> Start Process
For I = 2 + Incr To MaxRow + Incr

If I <> 2 + StandardIncr Then

'---> Check to see if Current set of dates falls all within the same date
If WS.Cells(I, "A") = "" Then
J = I
Do
J = J - 1
Incr = Incr - 1
Loop Until IsDate(WS.Cells(J, "A"))
I = J
Else

If DateValue(WS.Cells(I - Incr, "A")) <> DateValue(WS.Cells(I, "A")) Or WS.Cells(I, "A") = "" Then
J = I - Incr
Do
J = J + 1
Loop Until DateValue(WS.Cells(I - Incr, "A")) <> DateValue(WS.Cells(J, "A"))
Incr = J - 1 - (I - Incr)
I = I - StandardIncr
Else
Incr = StandardIncr
End If
End If
End If

WSConv.Cells(MaxRowConv, "A") = WS.Cells(I, "A")
WSConv.Cells(MaxRowConv, "B") = WS.Cells(I - Incr, "B")
WSConv.Cells(MaxRowConv, "C") = WS.Application.WorksheetFunction.Max(WS.Range("C" & I - Incr & ":C" & I))
WSConv.Cells(MaxRowConv, "D") = WS.Application.WorksheetFunction.Min(WS.Range("D" & I - Incr & ":D" & I))
WSConv.Cells(MaxRowConv, "E") = WS.Cells(I, "E")
WSConv.Cells(MaxRowConv, "F") = WS.Application.WorksheetFunction.Sum(WS.Range("F" & I - Incr & ":F" & I))
MaxRowConv = MaxRowConv + 1
Incr = StandardIncr
I = I + Incr
Next I

'---> Enable Events
With Application
.EnableEvents = True
.ScreenUpdating = True
End With

MsgBox ("Conversion 1 to 15 Done")

End Sub
``````

ITJockey sorry for inconvenience.
Gowflow
Data-Convert-V06.xlsm
0

LVL 8

Author Comment

ID: 39888345
In this question I learned term "Dedication" from both of you. As question is closed and still immense efforts I had seen.

Thank You
0

LVL 29

Expert Comment

ID: 39888433
"dedication" maybe, but rather "consciousness" as we are here not to gain points, but to provide solutions and when solutions are not 'correct' it is our duty to correct as end user not always knowledgeable (I always assume that end user don't even know how to type, with all due respect to end users not to diminish one's knowledge but in the contrary to force one to do more) that is the extreme and feel an obligation to provide as accurate as possible solution in the limit of one knowledge.

Tks your appreciation, this is the FUEL that get us going !
gowflow
0

LVL 29

Expert Comment

ID: 39909339
Any news on your projects ? Let me know if you need help.
gowflow
0

LVL 8

Author Comment

ID: 39909390
By the end of this month I have to finish any how. Working out on flow of process. As and when it finish I will surely let you know.

Thanks
0

LVL 29

Expert Comment

ID: 39909398
No problem take care.
gowflow
0

LVL 8

Author Comment

ID: 39911796
This Side line question from project - if you wish to - look in to this - Compounding

Thanks
0

LVL 29

Expert Comment

ID: 39911873
You already have Steeve on it !!!
gowflow
0

LVL 8

Author Comment

ID: 39911924
oh.....Yes but I don't know he still on the question or not... but you are right I have to Waite ... it is very bad manners if some one working for you & .....

I had this thought only bcoz of  I came to conclusion that it will get it through Code only.
I have respect for Sir The_Barman as he had solved many question of mine in past, I don't know he remembered or not.

Conclusion - Waite for Sir Steve to revert back.
Generally - I never asked other expert if some one working on it.

Thanks You
0

LVL 8

Author Comment

ID: 39952525
0

LVL 8

Author Comment

ID: 39955300
This my new question if you have spare time then pls look in to this Math Code Modification

Thanks
0

## Featured Post

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office â€¦
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a â€¦