VBA Copy Sort

charly1

Board Regular
Joined
Jul 18, 2023
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi all, and thanks in advance for any help that may be offered.
[I actually posted this q, a couple days ago, but not having received any reply, i believe it may have to do with me not being entirely clear as to what exactly it was that I was asking].
I have a couple of workers who clock in and out when they begin and finish working, by swiping a fob/chip. I then receive at the end of each week an excel sheet with a single column of data containing a long list of date and times [each cell containing the date and time of when the fob was swiped]. I receive a separate list for each worker. The first cell contains the time clocked in, and the second cell the time when he clocked out. The third, when he clocked in and the fourth when he clocked out, and so on and so forth. The attached macro code, allows me to select the entire list of data and auto copy it into a table with 2 separate columns, one for start times and one for finish, copying every second cell into the first column and every other cell into the adjacent corresponding cell, thus giving me a complete list of all the times when the work began and in the next column a list of when each working session ended.
My problem though is that the list i receive, lists all the times in the reverse, (since each new clock in/out gets added to the top of the list). Meaning that the very last cell contains the first clock in time, and the second to last the first clock out. The thirst to last cell contains the second clock in.... and so on and so forth.
As I result i need to amend the attached macro to first flip the entire row of data from last to first, and only then continue running and copying the data into the new cell as explained above.
Considering the even this first code someone kindly helped me write and I don't fully understand it either, I am totally at a loss of how to go about amending it to serve my needs.
If someone would be so kind as to help me with my issue I would be extremely grateful.
Thank you all so much.
VBA Code:
Sub MoveRange()
Dim rng As Range
Dim InputRng As Range, OutRng As Range
On Error GoTo ErrorMessage
Dim xTitleId As String
Dim i As Long
xTitleId = "Hello"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set InputRng = InputRng.Columns(1)
For i = 1 To InputRng.Rows.Count Step 2
    OutRng.Resize(1, 2).Value = Array(InputRng.Cells(i, 1).Value, InputRng.Cells(i + 1, 1).Value)
    Set OutRng = OutRng.Offset(1, 0)
Next
ErrorMessage:
Exit Sub
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Do you mean something like this?
VBA Code:
Sub MoveRange()
Dim rng As Range
Dim InputRng As Range, OutRng As Range
On Error GoTo ErrorMessage
Dim xTitleId As String
Dim i As Long
xTitleId = "Hello"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)

Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)
Set InputRng = InputRng.Columns(1)
InputRng.Select
Call FlipRange ' Call FlipRange to flip values of selected range
For i = 1 To InputRng.Rows.Count Step 2
    OutRng.Resize(1, 2).Value = Array(InputRng.Cells(i, 1).Value, InputRng.Cells(i + 1, 1).Value)
    Set OutRng = OutRng.Offset(1, 0)
Next
ErrorMessage:
Exit Sub
End Sub

Sub FlipRange() ' This will reverse the values of the selected cells from bottom to top.

Dim DataRNG As Range: Set DataRNG = Selection
Dim DataARR As Variant, DataFlipARR() As Variant
Dim i As Long, j As Long: j = 1

DataARR = DataRNG.Value
ReDim DataFlipARR(1 To UBound(DataARR), 1 To 1)

For i = UBound(DataARR) To 1 Step -1
    DataFlipARR(j, 1) = DataARR(i, 1)
    j = j + 1
Next i

DataRNG.Value = DataFlipARR

End Sub

My apologies for any quirks, English is not my native language. "So I blame Google translate for all my goofs." :devilish:
 
Upvote 0
First all, thank you very much for your effort and time in helping me out. I really appreciate it.
However a couple minutes before i saw your reply I think I figured out the answer myself. [Although your way of doing things may be better - you tell me!]
Where the macro has the "for" "to" line, I just flipped the "for" argument to be the last line of the selected data and the "to" argument to number 1. I also changed the step 2, to minus 2. And it all gets flipped beautifully.
Original line of relevant code:
VBA Code:
For i = 1 To InputRng.Rows.Count Step 2
Updated to:
VBA Code:
For i = InputRng.Rows.Count To 1  Step -2

Tell me if I'm making a mistake.
 
Upvote 0
so here is my vba
(it is a lot less complex and perhaps faster - you decide)

VBA Code:
Sub sort01a()
Dim iRng As Range, oRng As Range
Dim lRow As Long, lRow2 As Long, i As Long, j As Long
Dim cWS As Worksheet

Set cWS = ThisWorkbook.ActiveSheet

lRow = cWS.Cells(cWS.Rows.Count, 1).End(xlUp).Row
lRow2 = cWS.Cells(cWS.Rows.Count, 1).End(xlUp).Row
Set iRng = cWS.Range("b1:b" & lRow)

'flip column for clock in to be first
For i = 1 To lRow
Cells(i, 2).Value = Cells(lRow, 1).Value
lRow = lRow - 1
Next i

'sort clock in time to column 5
For i = 1 To iRng.Rows.Count Step 2
    iRng.Cells(i, 1).Offset(0, 3) = iRng.Cells(i, 1)
Next i

'sort clock out time to column 7
Set oRng = cWS.Range("b2:b" & lRow2)
For i = 1 To oRng.Rows.Count Step 2
    oRng.Cells(i, 1).Offset(-1, 5) = oRng.Cells(i, 1)
Next i

'clear helper column
Range("b1").EntireColumn.Delete
Range("b1").EntireColumn.Insert


End Sub

what it does is counts the number of entries,
then flips the column (from last entry to first entry)
then sorts clock in date/time
then sorts clock out date/time
then cleans up the helper column

hope this helps
here is the worksheet to see
(mrexcel).xlsm
ABCDEFGHI
115/3/2024 170513/3/2024 080113/3/2024 080113/3/2024 1659
215/3/2024 075513/3/2024 1659
314/3/2024 170014/3/2024 080014/3/2024 080014/3/2024 1700
414/3/2024 080014/3/2024 1700
513/3/2024 165915/3/2024 075515/3/2024 075515/3/2024 1705
613/3/2024 080115/3/2024 1705
Sheet1


with this version
there is no need to know how many times the employee clocked in/out
and it just sorts it pretty fast.

also, you can change or modify it as you need
(mrexcel).xlsm
ABCDEFGHI
115/3/2024 170513/3/2024 080113/3/2024 1659
215/3/2024 0755
314/3/2024 170014/3/2024 080014/3/2024 1700
414/3/2024 0800
513/3/2024 165915/3/2024 075515/3/2024 1705
613/3/2024 0801
Sheet1
 
Last edited:
Upvote 0
i forgot to delete the j as long (it was not needed)
sorry
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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