We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

Microsoft excel query

2»

Comments

  • Follow these instructions exactly:

    1. With this sheet containing the values you want to 'transpose' visible in Excel, select the cell that is at the top left of your data (for example, in your original example, if Item1 was in A1 then select A1, if it was in C4 you would select C4 etc).

    2. Open up the Visual Basic Editor (VBE) with Alt+F11 (hold down the Alt key and tap the F11 key)

    3. Insert a standard module by going to the menu at the top of the VBE and selecting Insert and then Module

    4. In the module that will open up on the right hand side of the screen, copy and paste in the following code (include everything from the "Sub Normalise_Data() line to the "End Sub" line):
    Sub Normalise_Data()
    Dim vIn As Variant, vOut As Variant
    Dim i As Long, j As Long, cnt As Long
    Dim rLastRow As Range, rLastColumn As Range
    Set rLastRow = Cells.Find(What:="*", After:=[A1], LookIn:=xlFormulas, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlPrevious)
    Set rLastColumn = Cells.Find(What:="*", After:=[A1], LookIn:=xlFormulas, lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlPrevious)
    
    vIn = Range(ActiveCell, Cells(rLastRow.Row, rLastColumn.Column)).Value2
    
    ReDim vOut(1 To UBound(vIn, 1) * (UBound(vIn, 2) - 1), 1 To 2)
    cnt = 0
    For i = 1 To UBound(vIn, 1)
        For j = UBound(vIn, 2) To 2 Step -1
            If Not IsEmpty(vIn(i, j)) Then
                cnt = cnt + 1
                vOut(cnt, 1) = vIn(i, 1)
                vOut(cnt, 2) = vIn(i, j)
            End If
        Next j
    Next i
    Worksheets.Add
    [A1].Resize(cnt, 2).Value = vOut
    End Sub
    

    5. Run the code by clicking the cursor anywhere in the routine you have just pasted in and then hitting the F5 key

    This should, assuming you have followed the steps above, generate a new worksheet with the data in the format you need.
  • heppy23
    heppy23 Posts: 478 Forumite
    Part of the Furniture Combo Breaker
    Thanks, that is brilliant.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 352.1K Banking & Borrowing
  • 253.6K Reduce Debt & Boost Income
  • 454.2K Spending & Discounts
  • 245.2K Work, Benefits & Business
  • 600.8K Mortgages, Homes & Bills
  • 177.5K Life & Family
  • 259K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.