Creat a VBA or formula for (get person name next to the activity)

captain321

New Member
Joined
Sep 25, 2014
Messages
8
This is my original data, this data has more than thousand names and hundred activity. Maybe a VBA function will really help.
Name/Activity:Prod %Date
Smith (12345)Attn:
walk764-Mar
clean453-Mar
Name/Activity:
John (23456)
Name/Activity:
Rose (1314)Attn:
camp438-Aug
walk657-Jul
run786-Jun
clean768-Feb
drive454-Apr
Name/Activity:
Lucy (34567)Attn:
food232-Feb
clean458-Aug

<tbody>
</tbody>




This is the data formatted I need, In this way, I can pivot and compare them.

NameName/Activity:Prod %Date
Smith (12345)Smith (12345)Attn:
Smith (12345)walk76%4-Mar
Smith (12345)clean45%3-Mar
Name/Activity:
John (23456)John (23456)
Name/Activity:
Rose (1314)Rose (1314)Attn:
Rose (1314)camp43%8-Aug
Rose (1314)walk65%7-Jul
Rose (1314)run78%6-Jun
Rose (1314)clean76%8-Feb
Rose (1314)drive45%4-Apr
Name/Activity:
Lucy (34567)Lucy (34567)Attn:
Lucy (34567)food23%2-Feb
Lucy (34567)clean45%8-Aug

<tbody>
</tbody>

Thank you so much for help!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
captain321,

Welcome to the MrExcel forum.

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


Sample raw data in worksheet Sheet1:


Excel 2007
ABC
1Name/Activity:Prod %Date
2Smith (12345)Attn:
3walk764-Mar
4clean453-Mar
5Name/Activity:
6John (23456)
7Name/Activity:
8Rose (1314)Attn:
9camp438-Aug
10walk657-Jul
11run786-Jun
12clean768-Feb
13drive454-Apr
14Name/Activity:
15Lucy (34567)Attn:
16food232-Feb
17clean458-Aug
18
Sheet1


After the macro (using two arrays in memory) in a new worksheet Results:


Excel 2007
ABCD
1NameName/Activity:Prod %Date
2Smith (12345)Smith (12345)Attn:
3Smith (12345)walk76%4-Mar
4Smith (12345)clean45%3-Mar
5Name/Activity:
6John (23456)John (23456)Attn:
7Name/Activity:
8Rose (1314)Rose (1314)Attn:
9Rose (1314)camp43%8-Aug
10Rose (1314)walk65%7-Jul
11Rose (1314)run78%6-Jun
12Rose (1314)clean76%8-Feb
13Rose (1314)drive45%4-Apr
14Name/Activity:
15Lucy (34567)Lucy (34567)Attn:
16Lucy (34567)food23%2-Feb
17Lucy (34567)clean45%8-Aug
18
Results


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
2. Open your NEW 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
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 09/25/2014, ME807964
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, o As Variant
Dim i As Long, j As Long
Dim lr As Long, na As String
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  a = .Range(.Cells(1, 1), .Cells(lr, 3))
  ReDim o(1 To lr, 1 To 4)
End With
j = j + 1
o(j, 1) = "Name"
o(j, 2) = "Name/Activity:"
o(j, 3) = "Prod %"
o(j, 4) = "Date"
For i = 2 To lr
  If a(i, 1) = "Name/Activity:" Then
    j = j + 1
    o(j, 2) = "Name/Activity:"
  ElseIf InStr(a(i, 1), " (") Then
    j = j + 1
    na = a(i, 1)
    o(j, 1) = na
    o(j, 2) = na
    o(j, 3) = "Attn:"
  Else
    j = j + 1
    o(j, 1) = na
    o(j, 2) = a(i, 1)
    o(j, 3) = a(i, 2) & "%"
    o(j, 4) = Format(a(i, 3), "d-mmm")
  End If
Next i
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Worksheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(lr, 4) = o
  .Columns(1).Resize(, 4).AutoFit
  .Activate
End With
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.
 

captain321

New Member
Joined
Sep 25, 2014
Messages
8
I use Windows 7 and Excel 2010. Thank you so much for you help. It works like a charm.
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
captain321,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,037
Messages
5,856,966
Members
431,841
Latest member
jaybeem

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
Top