Results 1 to 7 of 7
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Northampton, Northamptonshire, England
    Posts
    1,951
    Thanks
    2
    Thanked 1 Time in 1 Post

    Transpose in VBA problem (Excel 2002)

    Hi

    I am very early into learning VBA and I have tried to create a basic transpose data module. but I can' t work out my error here.

    Sub Transpose()
    For Each Window In Windows
    Set mysheets = Window.SelectedSheets
    For Each Worksheet In Window.SelectedSheets
    tempbook = ActiveWorkbook.Name
    Worksheet.Select
    Worksheet.Range(Application.Selection.Address).Cop y

    VBAProject.Sheet1.Range(Application.ActiveCell.Add ress).PasteSpecial
    VBAProject.Sheet1.Range(Application.Selection.Addr ess).Copy

    Workbooks(tempbook).Activate
    temp = Application.ActiveCell.Address
    For Each Cell In Application.Selection
    Cell.Value = ""
    Next Cell
    Worksheet.Range(temp).PasteSpecial Transpose:=True <------ debug highlights this row
    Next Worksheet
    Next Window
    mysheets.Select

    End Sub

    If some one could help please

    Braddy
    If you are a fool at forty, you will always be a fool

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Transpose in VBA problem (Excel 2002)

    Please try to explain as clearly as you can what you're trying to accomplish.

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Northampton, Northamptonshire, England
    Posts
    1,951
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Transpose in VBA problem (Excel 2002)

    Hi Hans

    It's a basic learning tool for me.

    1. In sheet 1 I have in cell A1 1 in B1 2 in C1 3
    A2 4 i n B2 5 in C2 6 I want to transpose these cells to read.

    A1 1 B1 4
    A2 2 B2 5
    A3 3 B3 6

    I hope this is clear

    Braddy
    If you are a fool at forty, you will always be a fool

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Transpose in VBA problem (Excel 2002)

    I have no idea what you need those two nested loops for if you want to transpose a range.

    Transposing "in place" is not possible directly. You must paste and transpose the data elsewhere, clear the original data, then paste back. In the code below, a temporary worksheet is used for this.

    Sub TransposeIt()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet

    On Error GoTo ErrHandler

    ' Don't let user see what's happening
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    ' Set reference to active sheet
    Set ws1 = ActiveSheet
    ' Copy range containing A1
    ws1.Range("A1").CurrentRegion.Copy
    ' Create temporary worksheet
    Set ws2 = Sheets.Add
    ' Paste transposed
    ws2.Range("A1").PasteSpecial Transpose:=True
    ' Clear original range
    ws1.Range("A1").CurrentRegion.Clear
    ' Paste back
    ws2.Range("A1").CurrentRegion.Copy Destination:=ws1.Range("A1")
    ' Delete temporary sheet
    ws2.Delete

    ExitHandler:
    ' Clean our act
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Set ws2 = Nothing
    Set ws1 = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Northampton, Northamptonshire, England
    Posts
    1,951
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Transpose in VBA problem (Excel 2002)

    Hi Hans

    I have no idea why the loops, I was using and instrruction from Richard Shepherds VBA Macro Programing.

    Thanks for the code you posted of course it's just what I needed, perhaps I should buy another book <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Braddy
    If you are a fool at forty, you will always be a fool

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Transpose in VBA problem (Excel 2002)

    Hi Braddy,
    Since this is a programming exercise for you, here's another option. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    <pre>Sub TransposeInPlace()
    Dim rngCurrent As Range
    Dim lngRows As Long, lngColumns As Long
    Dim varOutput As Variant
    Set rngCurrent = Selection.CurrentRegion
    lngRows = rngCurrent.Rows.Count
    lngColumns = rngCurrent.Columns.Count
    varOutput = Application.WorksheetFunction.Transpose(rngCurrent )
    rngCurrent.Clear
    Set rngCurrent = rngCurrent.Cells(1, 1)
    Range(rngCurrent, rngCurrent.Offset(lngColumns - 1, lngRows - 1)) = varOutput
    Set rngCurrent = Nothing
    End Sub
    </pre>

    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Northampton, Northamptonshire, England
    Posts
    1,951
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Transpose in VBA problem (Excel 2002)

    Hi Rory

    Thanks very much I try to analize all the code that I am so kindly given via the lounge, sometimes with success and sometimes not.

    Braddy
    If you are a fool at forty, you will always be a fool

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •