Columns to rows

TroyB

New Member
Joined
Nov 12, 2019
Messages
18
Hello all,

I am hoping somebody can help me fix and speed up the VBA below.

My intent is to make Columns "T" to "Last Column with data" become rows. and the VBA i have below is just not working very good and its very slow


Current:

Code:
Sub Columns_2_rows()



Dim rownum As Long

Dim rownum2 As Long

Dim colnum As Long

Dim wb As Workbook

Dim ws As Worksheet



Application.ScreenUpdating = False

Application.DisplayAlerts = False

  

Set ws = ThisWorkbook.Sheets.Add(After:= _

ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))

ws.Name = "Result"

Sheets("Raw_data").Select


Set wb = ActiveWorkbook

rownum = 2

rownum2 = 2



Sheets("Raw_data").Range("A1:S1").Copy Sheets("Result").Range("A1")

Sheets("Result").Range("T1").Value = "Date"

Sheets("Result").Range("U1").Value = "Value"



Do Until Sheets("Forecast Data").Cells(rownum, 1).Value = ""

colnum = 43

    Do Until colnum = 67

    Sheets("Raw_data").Range(Cells(rownum, 1), Cells(rownum, 19)).Copy Sheets("Result").Cells(rownum2, 1)

    Sheets("Forecast Data").Cells(1, colnum).Copy Sheets("Result").Cells(rownum2, 20)

    myval = Sheets("Raw_data").Cells(rownum, colnum).Value

        If myval <> "0" Then

        myval = myval / 1000

        Else

        myval = 0

        End If

    Sheets("Result").Cells(rownum2, 21) = myval

    rownum2 = rownum2 + 1

    colnum = colnum + 1

    Loop

rownum = rownum + 1

Loop



Application.DisplayAlerts = True

Application.ScreenUpdating = True

  

End Sub
 
Last edited by a moderator:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If you are willing to provide a quick preview (screen shots would do) of how the data looks initially and what you would need the output to be I could look into this for you.
 
Upvote 0
Hello Trunten,

Thank you very much for the reply

The original Data would look like this:
Heading 1Heading 2Customer #StateCountryCodeCust GroupCustomer NameItem#Heading 10Unit CostHeading 12Heading 13Heading 14Heading 15Heading 16Heading 17Heading 18Heading 19
10/1/2019​
10/2/2019​
11/19/2019​
11/19/2019​
12/19/2019​
12/19/2019​
1/1/2020​
1/1/2020​
2/1/2020​
2/1/2020​
3/1/2020​
3/1/2020​
4/1/2020​
4/1/2020​
5/1/2020​
5/1/2020​
6/1/2020​
6/1/2020​
7/1/2020​
7/1/2020​
8/1/2020​
8/1/2020​
9/1/2020​
9/1/2020​
########​
########​
########​
########​
########​
########​
1/1/2021​
1/1/2021​
2/1/2021​
2/1/2021​
3/1/2021​
3/1/2021​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
170​
31.71641791​
170​
31.71641791​
170​
31.71641791​
170​
31.71641791​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
0​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
31.71642​
170​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
86.1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
0​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
86.1​
1​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#2 REV 3Z293
0​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
1050.9​
Customer rep 2Sales Rep 2C007978CAUSAEPGroup 3Customer 2Item#2 REV 3Z263
34.89​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
Customer rep 1Sales Rep 1C007978ILUSAATGroup 3Customer 2Item#2 REV 3Z293
1.26​
50​
39.68253968​
50​
39.68253968​
50​
39.68253968​
50​
39.68253968​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
0​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
39.68254​
50​
39.68254​
Customer rep 1Sales Rep 1C007978CAUSAEPGroup 3Customer 2Item#2 REV 3Z255
3.68​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
7360​
2000​
Customer rep 1Sales Rep 1C007978CAUSAEPGroup 3Customer 2Item#1 REV BZ255
4.07​
0​
0​
0​
0​
8140​
2000​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
12210​
3000​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
Customer rep 1Sales Rep 1C007978CAUSAEPGroup 3Customer 2Item#5 REV BZ255
3.71​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​
1855​
500​


After running the VBA it would look like this:

Heading 1Heading 2Customer #StateCountryCodeCust GroupCustomer NameItem#Heading 10Unit CostHeading 12Heading 13Heading 14Heading 15Heading 16Heading 17Heading 18Heading 19ValueQty
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
10/1/2019​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
10/2/2019​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
11/19/2019​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
11/19/2019​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
12/19/2019​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
12/19/2019​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
1/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
1/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
2/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
2/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
3/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
3/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
4/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
4/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
5/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
5/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
6/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
6/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
7/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
7/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
8/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
8/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
9/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
9/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
10/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
10/1/2020​
0​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
11/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
11/1/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
12/1/2020​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
12/2/2020​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
1/1/2021​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
1/1/2021​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
2/1/2021​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
2/1/2021​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
3/1/2021​
1000​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
61.51​
3/1/2021​
16.25752​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
10/1/2019​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
10/2/2019​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
11/19/2019​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
11/19/2019​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
12/19/2019​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
12/19/2019​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
1/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
1/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
2/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
2/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
3/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
3/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
4/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
4/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
5/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
5/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
6/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
6/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
7/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
7/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
8/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
8/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
9/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
9/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
10/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
10/1/2020​
0​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
11/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
11/1/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
12/1/2020​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
12/2/2020​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
1/1/2021​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
1/1/2021​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
2/1/2021​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
2/1/2021​
31.71642​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
3/1/2021​
170​
Customer rep 1Sales Rep 1C023783MNUSAATGroup 1Customer 1Item#1 REV BZ293
5.36​
3/1/2021​
31.71642​
 
Upvote 0
with Power Query try Unpivot Only Selected (from T to the end) Columns
 
Last edited:
Upvote 0
I put a macro, it should be faster.

Change "Raw_data" and "Result" for the names of your sheets.

VBA Code:
Sub columns_to_rows()
  Dim sh1 As Worksheet, sh2 As Worksheet, lr As Long, lc As Long, n As Long
  Dim a As Variant, b As Variant, m As Long, i As Long, j As Long, k As Long
  '
  Set sh1 = Sheets("Raw_data")
  Set sh2 = Sheets("Result")
  '
  lr = sh1.Range("A" & Rows.Count).End(xlUp).Row
  lc = sh1.Cells(1, Columns.Count).End(xlToLeft).Column
  sh2.Rows("2:" & Rows.Count).ClearContents
  a = sh1.Range("A1", sh1.Cells(lr, lc)).Value
  n = (lr - 1) * (lc - 19)
  ReDim b(1 To n, 1 To 21)
  m = 1
  '
  For i = 2 To UBound(a)
    For j = 20 To lc
      For k = 1 To 19
        b(m, k) = a(i, k)
      Next
      b(m, 20) = a(1, j)
      b(m, 21) = a(i, j)
      m = m + 1
    Next
  Next
  sh2.Range("A2").Resize(UBound(b), 21).Value = b
End Sub
 
Last edited:
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top