Macro to copy and separate text

saitken

New Member
Joined
Jan 6, 2013
Messages
39
Office Version
  1. 365
Platform
  1. MacOS
Hello

I have a spreadsheet like the below
NameDate 1Date 2Date 3
X20/04/2013 21/04/201322/04/2013
Y21/05/201322/05/201323/05/2013
Z22/06/201323/06/201324/06/2013

<tbody>
</tbody><colgroup><col><col span="3"></colgroup>

I would like a macro to copy and separate the 3 dates on each row to end up like the below

NameDate 1Date 2Date 3
X20/04/2013
X21/04/2013
X22/04/2013
Y21/05/2013
Y22/04/2013
Y23/04/2013
Z22/06/2013
Z23/04/2013
Z24/04/2013

<tbody>
</tbody><colgroup><col><col span="3"></colgroup>

The actual spreadsheet is much longer so to do this manually will take a long time!

Any help would be appreciated.

Thanks
Stewart
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this code on a sample workbook. It will work as long as there are only and always 3 dates. If you have more dates it can be modified to work that way as well.

Code:
Sub MovinAround()


Dim ws As Worksheet
Dim x As Integer


x = 2


Set ws = Sheets("Sheet1")


Do


ws.Range("A" & x).Offset(1, 0).Insert Shift:=xlDown
ws.Range("A" & x).Offset(1, 0).Insert Shift:=xlDown
ws.Range("A" & x).Offset(0, 2).Insert Shift:=xlDown
ws.Range("A" & x).Offset(0, 3).Insert Shift:=xlDown
ws.Range("A" & x).Offset(0, 3).Insert Shift:=xlDown
ws.Range("A" & x).Offset(1, 1).Insert Shift:=xlDown
ws.Range("A" & x).Offset(1, 1).Insert Shift:=xlDown
ws.Range("A" & x).Offset(2, 2).Insert Shift:=xlDown


ws.Range("A" & x).Offset(1, 0).Value = ws.Range("A" & x)
ws.Range("A" & x).Offset(2, 0).Value = ws.Range("A" & x)


x = x + 3


Loop Until ws.Range("A" & x).Value = ""


End Sub

Hope this helps, let me know if it works.
 
Upvote 0
This should do a similar job for any number of date columns.
I've shown a 'Before' & 'After' screen shot where there is 4 columns of dates.
I have assumed that the data is not the result of formulas that need to be retained.

Excel Workbook
ABCDE
1NameDate 1Date 2Date 3Date 4
2X20/04/1321/04/1322/04/1323/04/13
3Y21/05/1322/05/1323/05/1324/05/13
4Z22/06/1323/06/1324/06/1325/06/13
Data (Before)


Excel Workbook
ABCDE
1NameDate 1Date 2Date 3Date 4
2X20/04/13
3X21/04/13
4X22/04/13
5X23/04/13
6Y21/05/13
7Y22/05/13
8Y23/05/13
9Y24/05/13
10Z22/06/13
11Z23/06/13
12Z24/06/13
13Z25/06/13
Data (After)



Code:
Sub Rearrange()
  Dim a, b
  Dim i As Long, j As Long, r As Long
  Dim currrows As Long, currcols As Long, newrows As Long
  
  With Range("A1").CurrentRegion
    a = .Value
    currrows = UBound(a, 1) - 1
    currcols = UBound(a, 2) - 1
    newrows = currrows * currcols
    ReDim b(1 To newrows, 1 To currcols + 1)
    For i = 1 To currrows
      For j = 1 To currcols
        r = (i - 1) * currcols + j
        b(r, 1) = a(i + 1, 1)
        b(r, j + 1) = a(i + 1, j + 1)
      Next j
    Next i
    .Offset(1).Resize(newrows).Value = b
  End With
End Sub
 
Upvote 0
I knew there had to be a cleaner way of doing it, but I couldn't figure it out. If you don't mind, can you explain how "UBound" works?
 
Upvote 0
I knew there had to be a cleaner way of doing it, but I couldn't figure it out. If you don't mind, can you explain how "UBound" works?
The built-in vba Help has a reasonable explanation and examples:
UBound Function

This page is specific to the Visual Basic for Applications (VBA) Language Reference for Office 2010.
Returns a Long containing the largest available subscript for the indicated dimension of an array.

Syntax

UBound(arrayname[, dimension])

The UBound function syntax has these parts:

Part ............. Description


arrayname ... Required. Name of the array variable; follows standard variable naming conventions.


dimension .... Optional; Variant (Long). Whole number indicating which dimension's upper bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.


Remarks

The UBound function is used with the LBound function to determine the size of an array. Use the LBound function to find the lower limit of an array dimension.


Example

This example uses the UBound function to determine the largest available subscript for the indicated dimension of an array.

Dim Upper
Dim MyArray(1 To 10, 5 To 15, 10 To 20) ' Declare array variables.
Dim AnyArray(10)
Upper = UBound(MyArray, 1) ' Returns 10.
Upper = UBound(MyArray, 3) ' Returns 20.
Upper = UBound(AnyArray) ' Returns 10.
 
Upvote 0
Hello, both macros work perfectly! Thanks. The one from Peter is ideal as I have a number of date columns and it seems to handle them whatever number. Thanks again. Stewart
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,038
Members
448,940
Latest member
mdusw

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