Special transpose

natkatten

New Member
Joined
May 20, 2011
Messages
5
Hi VBA experts


I have a table with an identifier in column A and values in columns B to Z.


The structure is like this:
IdentifierAvalue1value2value3value4
IdentifierBvalue5value6
IdentifierCvalue7value8value9value10value11value12
IdentifierD
IdentifierEvalue13value14

<tbody>
</tbody>


There is 3.000 rows of data. As you can see some of the identifiers have no values.


On another sheet I need the following table to be generated in this format (two columns):


IdentifierA Value1
IdentifierA Value2
IdentifierA Value3
IdentifierA Value4
IdentifierB Value5
IdentifierB Value6
IdentifierC Value7
IdentifierC Value8
IdentifierC Value9
IdentifierC Value10
IdentifierC Value11
IdentifierC Value12
IdentifierE Value13
IdentifierE Value14


Note that as IdentifierD has no values it shouldn't be in the new table.


Your help will be highly valued.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
natkatten,

Welcome to the MrExcel forum.


Sample raw data in worksheet Sheet1:


Excel Workbook
ABCDEFGH
1IdentifierAvalue1value2value3value4
2IdentifierBvalue5value6
3IdentifierCvalue7value8value9value10value11value12
4IdentifierD
5IdentifierEvalue13value14
6
Sheet1





After the macro in a new worksheet Results:


Excel Workbook
AB
1IdentifierAvalue1
2IdentifierAvalue2
3IdentifierAvalue3
4IdentifierAvalue4
5IdentifierBvalue5
6IdentifierBvalue6
7IdentifierCvalue7
8IdentifierCvalue8
9IdentifierCvalue9
10IdentifierCvalue10
11IdentifierCvalue11
12IdentifierCvalue12
13IdentifierEvalue13
14IdentifierEvalue14
15
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, 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, 10/06/2012
' http://www.mrexcel.com/forum/excel-questions/663115-special-transpose.html
Dim w1 As Worksheet, wR As Worksheet
Dim i As Variant, o As Variant
Dim r As Long, lr As Long, c As Long, lc As Long, n As Long, nr As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
lr = w1.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
lc = w1.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
i = w1.Range(w1.Cells(1, 1), w1.Cells(lr, lc))
n = Application.CountA(w1.Range(w1.Cells(1, 2), w1.Cells(lr, lc)))
ReDim o(1 To n, 1 To 2)
nr = 0
For r = 1 To UBound(i, 1)
  For c = 2 To UBound(i, 2)
    If i(r, c) <> "" Then
      nr = nr + 1
      o(nr, 1) = i(r, 1)
      o(nr, 2) = i(r, c)
    End If
  Next c
Next r
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
wR.Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
wR.Cells.EntireColumn.AutoFit
wR.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.
 
Last edited:
Upvote 0
Hi hiker95

Thanks a lot. I really appreciate this. It worked really well after I renamed my sheet with the source data.

Thanks again from natkatten, Denmark
 
Upvote 0
natkatten,

You are very welcome. Glad I could help.

Thanks for the feedback.

Come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

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