Home » excel » Create columns in sheet2 depending on number of rows in sheet1 in excel

Create columns in sheet2 depending on number of rows in sheet1 in excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to create columns in sheet2 depending on number of rows in sheet1 in excel.
e.g.
i have data in sheet1

ROW1:rowvalue1
ROW2:rowvalue2
ROW3:rowvalue3

i would like to have it in sheet2 as

column1       column2      column3
rowvalue1     rowvalue2    rowvalue3
How to&Answers:

use copy & paste special ‘transpose’ – you can do this manually.

If you want to use VBA:

Sub Macro1()

    Sheets(1).Activate 'open the first sheet
    Sheets(1).Range("A1:A" & Sheets(1).Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row).Select  'select all values in column
    Selection.Copy
    Sheets(2).Select
    ActiveSheet.Range("A1").Select   'this is where your data will be pasted
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True  'transpose=true flips your data around

End Sub

Where sheets(1) refers to the first sheet and sheets(2) to the second you can replace these with the index or name of your sheet (if you use name, use inverted commas around it), and I’m assuming your data starts in A1, the code:

    Sheets(1).Range("A1:A" & Sheets(1).Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row).Select

selects all of the data to the last cell in that column.

To make this work over multiple columns, if they are all in a row, you can just transpose the whole block. eg:
Sub Macro1()

    Sheets(1).Activate 'open the first sheet
    Sheets(1).Range("A1:X" & Sheets(1).Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row).Select  'select all values in column
    Selection.Copy
    Sheets(2).Select
    ActiveSheet.Range("A1").Select   'this is where your data will be pasted
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True  'transpose=true flips your data around

End Sub

If you only want to do some columns rows, then you’ll need to create a loop over the original code.

Sub Macro1()
    y = Array(1, 3, 5, 9) 'this is the index of columns A,C,E,F
    For x = 0 To 3
    Sheets(1).Activate 'open the first sheet
    RowCount = Sheets(1).Range(Cells(ActiveSheet.Rows.Count, y(x)).End(xlUp), Cells(ActiveSheet.Rows.Count, y(x)).End(xlUp)).Row
    Sheets(1).Range(Cells(1, y(x)), Cells(RowCount, y(x))).Select 'select all values in column
    Selection.Copy
    Sheets(2).Activate
    ActiveSheet.Range(Cells(x + 1, 1), Cells(x + 1, 1)).Select 'this is where your data will be pasted
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True  'transpose=true flips your data around
Next
End Sub

Answer:

Try the following code

Sub PerformAction()
    Application.ScreenUpdating = False
    With Worksheets(Sheet1.Name)
     LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    End With
    For i = 1 To LastRow
     Sheet2.Cells(1, i).Value = Sheet1.Range("A" & i).Value
    Next
    Application.ScreenUpdating = True
End Sub