views:

200

answers:

1

Hi all,

I have some Excel data that looks like the following (top two rows):

and I need to get it looking like the data on the bottom rows.

I've not really used Excel VBA before, so can someone point me in the right direction of where to start with this please?

Many thanks in advance

leddy

+1  A: 

A very easy way of doing this is by using the transpose option of Paste Special, depending on how much data you have. For a small amount it's worth doing it this way.

  1. Select B1:E1
  2. Copy.
  3. Select where you want it pasted.
  4. Go to Edit, Paste Special and choose transpose
  5. It will now be shown vertically. Just fill in the name Joe Bloggs and fill it down.

If you have a lot of different people, Joe Bloggs, Jane Doe and many more it would be a chore to transpose each individual person so a quick bit of VB code like horrible thing below should do the trick.

Public Sub test()
    Dim rowFound As Boolean, columnFound As Boolean, y As Long, x As Long, rowCounter As Long
    Dim thisSheet As Excel.Worksheet, resultSheet As Excel.Worksheet
    Set thisSheet = ActiveWorkbook.Sheets("Sheet1")
    Set resultSheet = ActiveWorkbook.Sheets("Sheet2")
    rowFound = True
    y = 0
    rowCounter = 0
    Do While rowFound
        columnFound = True
        Dim foundName As String
        foundName = thisSheet.Range("A1").Offset(y).Value
        If foundName = "" Then
            rowFound = False
        Else
            x = 0
            Do While columnFound
                If thisSheet.Range("B1").Offset(y, x).Value = "" Then
                    columnFound = False
                Else
                    resultSheet.Range("A1").Offset(rowCounter).Value = foundName
                    resultSheet.Range("B1").Offset(rowCounter).Value = thisSheet.Range("B1").Offset(y, x).Value
                    rowCounter = rowCounter + 1
                End If
                x = x + 1
            Loop
        End If
        y = y + 1
    Loop
End Sub

x and y are used like a set of graph coordinates. For every row it scans through the columns in the row, and adds it to the list below.

Edit:
I've updated the code, Integers are now Long and it writes the results to sheet2.

Andy
Hi and thanks for the reply. Yes, I have hundreds of rows, all with a variable number of columns, so doing Paste Special..Transpose would take forever.I ran the code above, but it had an 'overflow' error as the rowCounter variable went above int limits - it was also overwriting the rows beneath each name - is there any way to get it to add to a different sheet, "data" for example, and to add to the next empty row?Thanks again for your help - leddy
leddy
Updated. It now uses sheet2 for the results, and the Long type for the rowcounter. You can customise the sheet names.
Andy
Think I've got it - just added in 'dataSheet' and tweaked code to add to that:Dim dataSheet As Excel.WorksheetSet dataSheet = ActiveWorkbook.Sheets("data") Do While columnFound If thisSheet.Range("B1").Offset(y, x).Value = "" Then columnFound = False Else dataSheet.Range("A1").Offset(rowCounter).Value = foundName dataSheet.Range("B1").Offset(rowCounter).Value = thisSheet.Range("B1").Offset(y, x).Valuethanks again for your help!
leddy