Wrox Programmer Forums
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 20th, 2007, 05:32 AM
Registered User
Join Date: Feb 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Column to Row

I will be very grateful if anyone can help me to convert This,


MI 1 2 1
MI 2 1 1

Into This,

FilmName ViewerName Viwed


Old March 20th, 2007, 11:59 AM
Friend of Wrox
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

If you were just flipping rows and columns you could copy the range and paste special into another sheet, checking 'transpose'.

What you want to do is take the data given and make a new combined list out of it. This would have to be done with code. Create a button on the source workbook.

This code assumes source sheet is named Sheet1 and target sheet is named Sheet2.

Paste this code into the button's routine:
'This routine takes given table data and formulates a combined adjusted record on new sheet
  Dim oSource As Worksheet, oTarget As Worksheet
  Dim iRowOnSrc As Long, iRowOnTgt As Long, iColOn As Integer
  Set oSource = ActiveWorkbook.Worksheets("Sheet1")
  Set oTarget = ActiveWorkbook.Worksheets("Sheet2")
  'oTarget.Range("A:IV").Delete 'Remove ' at beginning if you first want to clear target sheet

  With oTarget
' Writes out headings
    .Range("1:1").Font.Bold = True
    .Cells(1, 1).Value = "Film Name"
    .Cells(1, 2).Value = "Viewer Name"
    .Cells(1, 3).Value = "Viewed"

    iColOn = 2 'Initializes Column on for source.
    iRowOnTgt = 3 'Initializes target row.

' Processes current records. Assumes Heading Row is Row 1.
    Do While .Cells(1, iColOn).Value <> ""
      iRowOnSrc = 3 'Resets row for source for each name
      Do While oSource.Cells(iRowOnSrc, 1).Value <> ""
        .Cells(iRowOnTgt, 1).Value = oSource.Cells(iRowOnSrc, 1).Value
        .Cells(iRowOnTgt, 2).Value = oSource.Cells(1, iColOn).Value
        .Cells(iRowOnTgt, 3).Value = oSource.Cells(iRowOnSrc, iColOn).Value
        iRowOnSrc = iRowOnSrc + 1
        iRowOnTgt = iRowOnTgt + 1
      iColOn = iColOn + 1
  End With
This code does what is desired with the given source data resulting in desired output.

Hope this helps.
Old March 23rd, 2007, 04:16 AM
Registered User
Join Date: Feb 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

Thank you for your help Allen.

Similar Threads
Thread Thread Starter Forum Replies Last Post
find the FIRST USED row/column? crmpicco Excel VBA 3 July 23rd, 2013 12:52 PM
Column to Row [email protected] SQL Server DTS 0 March 1st, 2007 10:57 AM
ROW COUNTER COLUMN ricespn SQL Server 2000 5 October 20th, 2006 06:36 PM
How to get the last row and last column value ramk_1978 SQL Server 2000 1 April 4th, 2005 06:34 PM
Read a column and Search Row by Row in another col AyatKh Excel VBA 2 January 26th, 2005 12:02 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.