VBA Consolidate Data

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,180
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have attendace that comes out of our system in different date ranges like below (This is why there is multiple lines of data). I am trying to consolidate it like the last table in the post. This is just a small example I have over 100,000 rows of data. Could someone please help me with. Thank you in advance Stephen!
Book2
ABCDEFGHIJKLMNOPQ
1ID8/258/268/278/308/319/19/29/39/79/89/109/139/149/159/169/17
2102A
3102A
4102
5102
6102
7102
8102
9102
10103A
11103
12103
13103A
14103
15103
16103
17103
18103AA
19103
20103
Sheet1



Into this,

Book2
ABCDEFGHIJKLMNOPQ
1Employee ID8/258/268/278/308/319/19/29/39/79/89/109/139/149/159/169/17
2102AA
3103AAAA
Sheet2
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Stephen_IV,


Sample worksheets before the macro:


Excel Workbook
ABCDEFGHIJKLMNOPQ
1ID8-258-268-278-308-319-19-29-39-79-89-109-139-149-159-169-17
2102A
3102A
4102
5102
6102
7102
8102
9102
10103A
11103
12103
13103A
14103
15103
16103
17103
18103AA
19103
20103
21
Sheet1





Excel Workbook
ABCDEFGHIJKLMNOPQ
1
2
3
4
Sheet2





After the macro:


Excel Workbook
ABCDEFGHIJKLMNOPQ
1Employee ID8-258-268-278-308-319-19-29-39-79-89-109-139-149-159-169-17
2102AA
3103AAAA
4
Sheet2





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 07/07/2011
' http://www.mrexcel.com/forum/showthread.php?t=562452
Dim w1 As Worksheet, w2 As Worksheet
Dim LR As Long, LC As Long, SR As Long, ER As Long
Dim a As Long, aa As Long, b As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
Set w2 = Worksheets("Sheet2")
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
LC = w1.Cells(1, Columns.Count).End(xlToLeft).Column
w1.Range(w1.Cells(2, 1), w1.Cells(LR, LC)).Sort Key1:=w1.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
  OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
w2.UsedRange.ClearContents
w1.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=w2.Columns(1), Unique:=True
w1.Range(w1.Cells(1, 1), w1.Cells(1, LC)).Copy w2.Range("A1")
LR = w2.Cells(Rows.Count, 1).End(xlUp).Row
For a = 2 To LR Step 1
  SR = Application.Match(w2.Cells(a, 1), w1.Columns(1), 0)
  ER = Application.Match(w2.Cells(a, 1), w1.Columns(1), 1)
  For aa = SR To ER Step 1
    For b = 2 To LC Step 1
      If w1.Cells(aa, b) <> "" Then w2.Cells(a, b).Value = w1.Cells(aa, b).Value
    Next b
  Next aa
Next a
w2.Range("A1") = "Employee ID"
w2.UsedRange.Columns.AutoFit
w2.UsedRange.HorizontalAlignment = xlCenter
w2.Activate
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the ReorgData macro.
 
Upvote 0
hiker95,

You are a lifesaver! Thanks you so so much! It works great and saved me a tone of time. I appreciate it! Thank you again!
 
Upvote 0
Stephen_IV,

You are very welcome.

Glad I could help.

Thanks for the feedback.

Come back anytime.


For my own personal information: How much time in hours do you think my macro will save you per day/week/month?

Please send your company name, and web site.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,856
Members
452,948
Latest member
UsmanAli786

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