Conditional sorting columns based on time

Showhoff

New Member
Joined
Mar 1, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi, I've worked all weekend to learn VBA. I came close, but code is probably what a proper coder would call (very) ugly. I would really appreciate tips on how to make a more streamlined code in addition to the key question:
A rather big set of data are collected on sheet 1 ("Disorderly"). Column A, C, E and so forth are timestamps of when the the measurement is done. Column B, D, F and so forth are the corresponding value of the measurement to the timestamp. I want to make a time-series analysis using the variables, hence it is important that the variables origin from the same time of measure. Some sensor have a malfunction a might skip a measurement or two. The code should collect the sensor vales which belongs to the same timestamp onto the same row in the next sheet ("Organized"). It get's a bit more complicated because there are two kinds of sensors, so it would be beneficial if the time value the code is searching for is a timestamp is within an interval (let's say 45 sec).

Any help will be much appreciated
, hopefully I will learn enough VBA to help other too eventually (gotta start somewhere though).

Here's my filthy code (it was a test, so not every row is included because it takes rather long time to compile all the rows. Also this is without any kind of checking if the "what" part of the range.find function is within a time interval based on the cell which is the one we search for. The code does what it should, but there is a long way to go still. I am going to include code which ensures deleting of rows with empty cell and the empty columns in the "Organized" sheet ):
VBA Code:
Private Sub CommandButton1_Click()

Dim rH As Long, Row As Long, Col As Long, CurrentRow As Long, NextCellValue As Double
Dim RowSize As Integer, ColSize As Integer
Dim foundRng As Range
Set DataArk = Worksheets("Disorderly")
Set ResArk = Worksheets("Organized")
'Copy the first two rows
rH = 1
    For Row = 1 To 9
        ResArk.Rows(Row).EntireRow.Value = DataArk.Rows(rH).EntireRow.Value
        rH = rH + 1
    Next Row
'Copy column 1 and 2 in DataArk
    DataArk.Columns(1).Copy Destination:=ResArk.Columns(1)
    DataArk.Columns(2).Copy Destination:=ResArk.Columns(2)
'Search through every second column for the associated timestamp in column 1
ColSize = DataArk.Cells(3, Columns.Count).End(xlToLeft).Column
  For Row = 10 To 20
    For Col = 3 To ColSize Step 2
            RowSize = DataArk.Cells(Rows.Count, Col).End(xlUp).Row
            Set foundRng = Range(DataArk.Cells(10, Col), DataArk.Cells(RowSize, Col)).Find(DataArk.Cells(Row, 1)) ', After:=DataArk.Cells(CurrentRow - 1, Col), SearchOrder:=xlByRows, SearchDirection:=xlNext)
            If foundRng Is Nothing Then
            ResArk.Activate
            ResArk.Cells(Row, Col + 1) = ""
            DataArk.Activate
            Else
            NextCellValue = DataArk.Cells(foundRng.Row, foundRng.Column + 1).Value
            ResArk.Activate
            ResArk.Cells(Row, Col + 1).Value = NextCellValue
            DataArk.Activate
            End If
   Next Col
Next Row

End Sub
 
Last edited by a moderator:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Some minor spelling mistakes here and there, ups. I added Application.Screenupdate = False/True. Sped up the testing quite much.
 
Upvote 0
Welcome to the MrExcel board!

Could we see a small set of dummy sample data and the expected results? XL2BB

Also, when posting code, it needs to go like this
[CODE=vba]
Code goes here
[/CODE]

Not like this
[CODE=vba][/CODE]
Code goes here
[CODE=vba][/CODE]

I have fixed it for you in post #1
 
Upvote 0
Excel VBA small sample.png

And the expected result would be similar times for each value. For example values from cells (10,1) : (9,2) : (10, 3) are put on the same row in the sheet we are trying to obtain with sorted data..

Thank you very much, I understand what I did wrong :)
 
Upvote 0
Sorry, not those cells, I mean (10,1) : (9, 3) and (10, 5) with the corresponding values of 78.8, 7.3 and 8.3.
 
Upvote 0
Sorry, I'm not able to copy/paste that data to test your code with (or consider possible changes to it).
 
Upvote 0
Not for visualization, but for testing. I see. A new day, hopefully thinking more straight today.
25.07.2019 01:23:00​
266.9​
25.07.2019 01:23:00​
531.1​
25.07.2019 01:23:00​
488.1​
25.07.2019 01:27:00​
266​
25.07.2019 01:26:00​
531.6​
25.07.2019 01:26:00​
486.4​
25.07.2019 01:30:00​
266.3​
25.07.2019 01:27:00​
530.3​
25.07.2019 01:27:00​
486.6​
 
Upvote 0
And is this the result you would be looking for for that data? If not, please provide results you would want.

Showhoff 2020-03-02 1.xlsm
ABCDEF
1
225.07.2019 01:23:00266.925.07.2019 01:23:00531.125.07.2019 01:23:00488.1
325.07.2019 01:26:00531.625.07.2019 01:26:00486.4
425.07.2019 01:27:0026625.07.2019 01:27:00530.325.07.2019 01:27:00486.6
525.07.2019 01:30:00266.3
Sheet2
 
Upvote 0
I don't include the timestamps in my code (except the the ones in Column A) as they provide just the same data. The important thing is that the values from with the same timestamps are aligned in the same row. I'm making sub deleting the columns and and a sub deleting the rows with blanks as we speak. I'm really interested in how to do all this with well executed coding, so even if I'm able to do it, I'm very grateful nevertheless.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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