Reformatting Table in Excel

Michi96

New Member
Joined
Oct 19, 2020
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hello :) I have a database as:

IDStart dateEnd dateDivision
10015/01/9617/03/091
10018/03/0922/05/111
10025/05/112/05/182
1003/05/187/06/202
1007/06/2020/12/201
101.........

I would like to reformat the table so that for each ID there is only row with the start date and end date that ID has been in each division:
IDStart dateEnd dateDivision
10015/01/9622/05/111
10025/05/117/06/202
1007/06/2020/12/201
101.........

Do you have any idea on how to do this?

Thank you so much in advance.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I'd like to help you, but I haven't found obvious rules in your two tables For example, in the first table, I inferred from row 2:5 that B6 should be put with C7. In fact, you put B6 and C6 together in the second table
 
Upvote 0
Thank you for your reply. Yes indeed, this is because people could switch division from 1 to 2 and I need to respect the order of time. For ID 100 this means that: I need a row in which he has been in division 1, a row in which he has been in division 2, and a row showing that he has been in division 1 again.

Hope this clarifies a little bit my dilemma.
Thanks!!
 
Upvote 0
Hi,
Try this code:
VBA Code:
Sub ReformattingTable()
  
  Const SRC = "A1:D1"       ' The 1st row of the source range
  Const DEST = "F1"         ' The 1st destination cell
  Const ID = 1, EDATE = 3, DIV = 4
  
  Dim a(), b()
  Dim i As Long, j As Long, k As Long
  Dim key As String
  
  a() = Range(SRC, Cells(Rows.Count, Range(SRC).Column).End(xlUp))
  ReDim b(1 To UBound(a), 1 To UBound(a, 2))
  
  ' Populate b() by destination data
  With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(a)
      key = CStr(a(i, DIV))
      If Len(key) > 0 Then
        key = a(i, ID) & "_" & key
        If .Exists(key) Then
          b(.Item(key), EDATE) = a(i, EDATE)
          .Remove key
        Else
          .Item(key) = i
          j = j + 1
          For k = 1 To DIV
            b(j, k) = a(i, k)
          Next
        End If
      End If
    Next
  End With
  
  ' Clear destination range
  Range(DEST).Resize(Cells.SpecialCells(xlCellTypeLastCell).Row, DIV).ClearContents
  
  ' Put the destination data
  Range(DEST).Resize(j, DIV).Value = b()
  
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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