Copy Excel data from columns to rows with VBA

后端 未结 6 1056
梦如初夏
梦如初夏 2021-01-21 20:16

I have a little experience with VBA, and I would really appreciate any help with this issue. In a basic sense, I need to convert 2 columns of data in sheet 1 to rows of data in

相关标签:
6条回答
  • 2021-01-21 20:39

    Try this:

    Sub TansposeRange()
     Dim InRange As Range
     Dim OutRange As Range
     Dim i As Long
    
     Set InRange = Sheet1.Range("B3:B10002")
     Set OutRange = Sheet2.Range("C2")
    
     InRange.Worksheet.Activate
     InRange.Select
     Selection.Copy
    
     OutRange.Worksheet.Activate
     OutRange.Select
    
     Selection.PasteSpecial Paste:=xlPasteValues, Transpose:=True
    
    End Sub
    
    0 讨论(0)
  • 2021-01-21 20:41

    This is a way to do it using a loop, here illustrated with a step of 2

    Notice that you have to specify the OutRange precisely the correct size (here NTR2 is the 10001's cell of the 2nd row).

    Sub TansposeRange()
     Dim InRange As Range
     Dim OutRange As Range
     Dim i As Long
    
     Set InRange = Sheet1.Range("B3:B10002")
     Set OutRange = Sheet2.Range("C2:NTR2")
    
     For i = 1 To 10000 Step 2
      OutRange.Cells(1, i) = InRange.Cells(i, 1)
     Next i
    
    End Sub
    
    0 讨论(0)
  • 2021-01-21 20:42
        'The following code is working OK
        Sub TansposeRange()
        '
        ' Transpose Macro
        '
        Dim wSht1 As Worksheet
        Dim rng1 As Range
        Dim straddress As String
        Set wSht1 = ActiveSheet
    
        On Error Resume Next
        Set rng1 = Application.InputBox(Prompt:="Select Columns or Rows to transpose", _
                                       Title:="TRANSPOSE", Type:=8)
        If rng1 Is Nothing Then
            MsgBox ("User cancelled!")
            Exit Sub
        End If
        straddress = InputBox(Prompt:="Full cell Address as Sheet2!A1", _
              Title:="ENTER Full Address", Default:="Sheet1!A1")
        If straddress = vbNullString Then
             MsgBox ("User cancelled!")
             Exit Sub
        End If      
    
        Application.ScreenUpdating = False
        rng1.Select
        rng1.Copy
    
        On Error GoTo 0
    
        'MsgBox straddress
        Range(straddress).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
        Application.ScreenUpdating = True
        End Sub
    
    0 讨论(0)
  • 2021-01-21 20:48

    This method leverages loops and arrays to transfer the data. It isn't the most dynamic method but it gets the job done. All the loops use existing constants, so if your data set changes you can adjust the constants and it should run just fine. Make sure to adjust the worksheet names to match the names you are using in your excel document. In effect, what this is doing is loading your data into an array and transposing it onto another worksheet.

    If your data set sizes change quite a bit, you will want to include some logic to adjust the loop variables and array size declarations. If this is the case, let me know and I'll figure out how to do that and post an edit.

    Sub moveTimeData()
    
    Set source = ThisWorkbook.Sheets("RawData")
    Set dest = ThisWorkbook.Sheets("TransposeSheet")
    
    Const dataSetSize = 15
    
    Const row15Start = 3
    Const row15End = 18
    Const row30Start = 21
    Const row30End = 36
    
    Const colStart = 2
    
    Const destColStart = 2
    Const dest15RowStart = 2
    Const dest30RowStart = 3
    
    Dim time15Array() As Integer
    Dim time30Array() As Integer
    ReDim time15Array(0 To dataSetSize)
    ReDim time30Array(0 To dataSetSize)
    
    Dim X As Integer
    Dim Y As Integer
    Dim c As Integer
    c = 0
    
    For X = row15Start To row15End
        time15Array(c) = source.Cells(X, colStart).Value
        c = c + 1
    Next X
    
    c = 0
    For X = row30Start To row30End
        time30Array(c) = source.Cells(X, colStart).Value
        c = c + 1
    Next X
    
    For X = 0 To dataSetSize
        dest.Cells(dest15RowStart, X + destColStart).Value = time15Array(X)
    Next X
    
    For X = 0 To dataSetSize
        dest.Cells(dest30RowStart, X + destColStart).Value = time30Array(X)
    Next X
    
    End Sub
    

    EDIT-> I think this is what you are looking for after reading your edits

    Sub moveTimeData()
    
    Set source = ThisWorkbook.Sheets("RawData")
    Set dest = ThisWorkbook.Sheets("TransposeSheet")
    
    Const numberDataGroups = 4
    Const dataSetSize = 15
    Const stepSize = 18
    
    Const sourceRowStart = 3
    
    Const sourceColStart = 2
    
    Const destColStart = 2
    Const destRowStart = 2
    
    
    
    Dim X As Integer
    Dim Y As Integer
    Dim currentRow As Integer
    currentRow = destRowStart
    
    
    
    For X = 0 To numberDataGroups
        For Y = 0 To dataSetSize
            dest.Cells(currentRow, Y + destColStart).Value = source.Cells((X * stepSize) + (Y    + sourceRowStart), sourceColStart)
        Next Y
        currentRow = currentRow + 1
    Next X
    
    
    End Sub
    

    Now the key to this working is knowing how many groups of data you are dealing with after the data dump. You either need to include logic for detecting that or adjust the constant called numberDataGroups to reflect how many groups you have. Note: I leveraged a similar technique for traversing arrays that have their data stored in Row Major format.

    0 讨论(0)
  • 2021-01-21 20:53

    Try this code:

    Dim X() As Variant
    Dim Y() As Variant
    X = ActiveSheet.Range("YourRange").Value
    Y = Application.WorksheetFunction.Transpose(X)
    

    Also check out this link: Transpose a range in VBA

    0 讨论(0)
  • 2021-01-21 21:02

    Use Copy, then Paste Special+Transpose to turn your columns into rows:
    Selection.PasteSpecial Paste:=xlPasteValues, Transpose:=True

    0 讨论(0)
提交回复
热议问题