Copy ID Data based on converting date series into start date and end date

decent_boy

Board Regular
Joined
Dec 5, 2014
Messages
130
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have data sheet with Numbers in col A , ID in Col B and Dates in Col C I Just need a macro to copy each Number and convert its dated into start date and end date. I am attached here images of Data Sheet and Result Sheet.
 

Attachments

  • Data.JPG
    Data.JPG
    51.3 KB · Views: 22
  • Result.JPG
    Result.JPG
    37.9 KB · Views: 21

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
It is hard to work with pictures. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Do you want the result to replace the current data or do you want it on a separate sheet?
 
Upvote 0
It is hard to work with pictures. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Do you want the result to replace the current data or do you want it on a separate sheet?
Please find below as required

Book5
ABC
1NumbersIDDate
200012345221/3/2022
300012345221/3/2022
400012345221/3/2022
500012345221/3/2022
600012345221/5/2022
700012345221/5/2022
800012345221/6/2022
900012345221/6/2022
1000012345221/6/2022
1100012345221/6/2022
1200012345221/6/2022
1300012345221/6/2022
1400012345221/6/2022
1500023456221/3/2022
1600023456221/3/2022
1700023456221/3/2022
1800023456221/3/2022
1900023456221/4/2022
2000023456221/4/2022
Data


Book5
ABCD
1NumberIDStart DateEnd Date
200012345221/3/20221/6/2022
300023456221/3/20221/4/2022
Result
 
Upvote 0
Try:
VBA Code:
Sub CopyIDData()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, srcWS As Worksheet, desWS As Worksheet, sDate As Range, eDate As Range
    Set srcWS = Sheets("Data")
    Set desWS = Sheets("Result")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(v)
            If Not .exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Set sDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlNext).Offset(, 2)
                Set eDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlPrevious).Offset(, 2)
                desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 4) = Array(v(i, 1), v(i, 2), sDate, eDate)
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopyIDData()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, srcWS As Worksheet, desWS As Worksheet, sDate As Range, eDate As Range
    Set srcWS = Sheets("Data")
    Set desWS = Sheets("Result")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(v)
            If Not .exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Set sDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlNext).Offset(, 2)
                Set eDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlPrevious).Offset(, 2)
                desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 4) = Array(v(i, 1), v(i, 2), sDate, eDate)
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
Thanks Mumps for your reply and given above codes are working fine but I have just 3 issues on result sheet that 1 is that i have got 5 lacs rows data where these codes are taking too much to show result and 2nd is start and end date format and third one is that 000 are removed from numbers in result sheet
 
Upvote 0
i have got 5 lacs rows data
What do you mean by this?
Format columns C and D in the Result sheet to match your desired format.
Try this version:
VBA Code:
Sub CopyIDData()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, srcWS As Worksheet, desWS As Worksheet, sDate As Range, eDate As Range
    Set srcWS = Sheets("Data")
    Set desWS = Sheets("Result")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(v)
            If Not .exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Set sDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlNext).Offset(, 2)
                Set eDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlPrevious).Offset(, 2)
                desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 4) = Array("'" & v(i, 1), v(i, 2), sDate, eDate)
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
What do you mean by this?
Format columns C and D in the Result sheet to match your desired format.
Try this version:
VBA Code:
Sub CopyIDData()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, srcWS As Worksheet, desWS As Worksheet, sDate As Range, eDate As Range
    Set srcWS = Sheets("Data")
    Set desWS = Sheets("Result")
    v = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(v)
            If Not .exists(v(i, 1)) Then
                .Add v(i, 1), Nothing
                Set sDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlNext).Offset(, 2)
                Set eDate = srcWS.Range("A:A").Find(v(i, 1), LookIn:=xlValues, lookat:=xlWhole, SearchDirection:=xlPrevious).Offset(, 2)
                desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 4) = Array("'" & v(i, 1), v(i, 2), sDate, eDate)
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
Thank so much Momps for your support.
 
Upvote 0
Mumps. I am facing following issue in result can you please check this

Book8
ABC
1NumbersIDDate
212345221/3/2022
312345221/3/2022
412345221/3/2022
512345221/3/2022
612345221/5/2022
712345221/5/2022
812345221/6/2022
912345221/6/2022
1012345224/2/2022
1112345224/2/2022
1212345224/2/2022
1312345224/3/2022
1412345224/3/2022
1512345224/4/2022
1623456221/3/2022
1723456221/3/2022
1823456221/3/2022
1923456221/4/2022
2023456221/4/2022
Data


Book8
ABCDE
1NumberIDStart DateEnd Date
212345221/3/20224/4/2022Incorrect
323456221/3/20221/4/2022
4
5
6
712345221/3/20221/6/2022Correct
812345224/2/20224/4/2022
Result
 
Upvote 0

Forum statistics

Threads
1,223,367
Messages
6,171,669
Members
452,416
Latest member
johnog

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