Copy to another sheet

asdfgh

New Member
Joined
May 6, 2016
Messages
11
Hi,

I'm new to VBA and I know that this might be a big question, but here you go.

I have a sheet with data that look a bit like this and the columns continues with the next date:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A1[/TD]
[TD]B1[/TD]
[TD]C1[/TD]
[TD]D1[/TD]
[TD]E1[/TD]
[TD]F1[/TD]
[TD]G1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Name[/TD]
[TD]Info1[/TD]
[TD]Info2[/TD]
[TD]Info3[/TD]
[TD]Info4[/TD]
[TD]12-09-2017[/TD]
[TD]13-09-2017[/TD]
[TD]....[/TD]
[/TR]
[TR]
[TD]Simon[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]1[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]John[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]2[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I want this data to be copied in to a new sheet like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]Info1[/TD]
[TD]Info2[/TD]
[TD]Info3[/TD]
[TD]Info4[/TD]
[TD]Date[/TD]
[TD]Data from that date[/TD]
[/TR]
[TR]
[TD]Simon[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]12-09-2017[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Simon[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]13-09-2017[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]John[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]12-09-2017[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]John[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]XX[/TD]
[TD]13-09-2017[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]

I can't figure out how to copy the first part of the row directly but 'split up' the other part. At least not in a 'simple' code.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
asdfgh,

Here is a macro solution for you to consider that is based on your two flat screen displays, that uses two arrays in memory.

I assume that your raw data is in worksheet Sheet1, and, the results will be written to a new worksheet Results.

You can change the raw data worksheet name in the macro.

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 ReorganizeData()
' hiker95, 09/12/2017, ME1022479
Application.ScreenUpdating = False
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, lr As Long, lc As Long, n As Long, i As Long, c As Long
Dim o As Variant, j As Long, t As Variant
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  n = Application.Count(.Range(.Cells(2, 6), .Cells(lr, lc)))
  ReDim o(1 To n, 1 To 7)
End With
For i = 2 To UBound(a, 1)
  For c = 6 To UBound(a, 2)
    j = j + 1
    o(j, 1) = a(i, 1): o(j, 2) = a(i, 2): o(j, 3) = a(i, 3)
    o(j, 4) = a(i, 4): o(j, 5) = a(i, 5)
    o(j, 6) = a(1, c): o(j, 7) = a(i, c)
  Next c
Next i
t = Array("Name", "Info1", "Info2", "Info3", "Info4", "Date", "Data from that date")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Worksheets("Results")
With wr
  .UsedRange.Clear
  With .Cells(1, 1).Resize(, UBound(t) + 1)
    .Value = t
    .Font.Bold = True
  End With
  .Cells(2, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.Columns.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, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorganizeData macro.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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