How do i re-format the presentation of large amounts of data?

jackpollek

New Member
Joined
Jul 29, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
hello,
I am downloading some data from a database to perform panel regression on.
When i download the data it comes in the format shown in rows A1:M10.
However I would like to rearrange the data to the format shown In O1:R28
I will be dealing with large amount of data so am looking for a way to do this quickly.

I hope this question makes sense, I will greatly appreciate any help


Example sheet.xlsx
ABCDEFGHIJKLMNOPQR
1YearFirm 1 - EPSFirm 1 - ESGFirm 2 - EPSFirm 2 - ESG Firm 3 - EPSFirm 3 - ESG Firm 4 - EPSFirm 4 - ESGFirm 5 - EPSFirm 5 - ESGFirm 6 - EPSFirm 6 - ESGFirmYear EPSESG
220121.57957.071.81688.35-0.00442.63-0.73824.125.01674.310.22563.46Firm 120121.57957.07
320131.44660.542.70592.340.02945.92-0.12427.144.8877.60.18575.87Firm 120131.44660.54
420141.86156.782.49692.32-0.02649.58-0.47228.595.04677.910.2878.31Firm 120141.86156.78
520152.36154.181.43692.820.06250.67-1.38628.054.56878.830.2774.67Firm 120152.36154.18
620162.09361.92.12891.30.24565.59-0.93632.814.38477.850.64273.36Firm 120162.09361.9
720172.56368.123.25390.720.22872.21-1.49538.393.2176.731.15376.62Firm 120172.56368.12
820182.99970.014.3293.280.99285.08-1.14548.572.41573.931.50975.68Firm 120182.99970.01
920193.17470.335.741931.1588.56-0.97458.25.18980.311.13174.14Firm 120193.17470.33
1020203.70676.456.71393.512.09186.890.63763.154.74582.211.72579.19Firm 120203.70676.45
11Firm 220121.81688.35
12Firm 220132.70592.34
13Firm 220142.49692.32
14Firm 220151.43692.82
15Firm 220162.12891.3
16Firm 220173.25390.72
17Firm 220184.3293.28
18Firm 220195.74193
19Firm 220206.71393.51
20Firm 32012-0.00442.63
21Firm 320130.02945.92
22Firm 32014-0.02649.58
23Firm 320150.06250.67
24Firm 320160.24565.59
25Firm 320170.22872.21
26Firm 320180.99285.08
27Firm 320191.1588.56
28Firm 320202.09186.89
Sheet1
Cells with Data Validation
CellAllowCriteria
A1Any value
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
This might work for you. Output is in columns O:R like in your example, but you can put it in another range or worksheet. (You'd want to reference sheet names in that case).
VBA Code:
Sub jackp()
Dim i As Long, lastrow As Long, lastcol as Long, nextrow As Long, cntRows As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
lastcol = Range("A" & lastrow).End(xlToRight).Column
cntRows = lastrow - 1
nextrow = 2

For i = 2 To lastcol Step 2
    Cells(nextrow, "O").Resize(cntRows).Value = Left(Cells(1, i), InStr(1, Cells(1, i), " -") - 1)
    Cells(nextrow, "P").Resize(cntRows).Value = Cells(2, 1).Resize(cntRows).Value
    Cells(nextrow, "Q").Resize(cntRows, 2).Value = Cells(2, i).Resize(cntRows, 2).Value
    nextrow = nextrow + cntRows
Next i

End Sub
 
Upvote 0
No response, so I came up with an alternative approach via Arrays:

VBA Code:
Sub FormatData()                                                                    ' 0.0375 average seconds
'
    Dim StartTime           As Double
    StartTime = Timer                                                               ' Start the stopwatch
'
    Dim ArrayColumnNumber   As Long, ArrayRow           As Long
    Dim Counter             As Long
    Dim FirmCount           As Long
    Dim lastrow             As Long
    Dim NumberOfFirms       As Long
    Dim NumberOfYears       As Long
    Dim ResultOffset        As Long
    Dim StartRowOfData      As Long
    Dim Cel                 As Range
    Dim LastColumn          As String
    Dim SourceArray         As Variant, OutputArray()   As Variant
    Dim WS                  As Worksheet
'
    Set WS = Sheets("Sheet1")                                                       ' <--- Set this to the proper sheet name
    StartRowOfData = 2                                                              ' <--- Set this to the row # that the data starts on
    ResultOffset = 2                                                                ' <--- Set this to the # of columns to offset the displayed results
'
    lastrow = WS.Range("A" & WS.Rows.Count).End(xlUp).Row                           ' Get last row # used in Column A
    LastColumn = Split(Cells(1, (WS.Cells.Find("*", , xlFormulas, _
            , xlByColumns, xlPrevious).Column)).Address, "$")(1)                    ' Get last column letter used in sheet
    LastColumnNumber = WS.Cells.Find("*", , xlFormulas, _
            , xlByColumns, xlPrevious).Column                                       ' Get last column number used in sheet
'
    SourceArray = WS.Range("A" & StartRowOfData & ":" & _
            LastColumn & lastrow)                                                   ' Save data into SourceArray
'
    NumberOfFirms = (UBound(SourceArray, 2) - 1) / 2                                ' Determine # of Firms we have
    NumberOfYears = lastrow - StartRowOfData + 1                                    ' Determine how many years we are dealing with
'
    ReDim OutputArray(1 To NumberOfFirms * NumberOfYears, 1 To 4)                   ' Set the # of rows & columns for the OutputArray
'
    Counter = 0                                                                     ' Reset Counter
    For FirmCount = 1 To NumberOfFirms                                              ' Loop through the NumberOfFirms
        For ArrayRow = 1 To NumberOfYears                                           '   Loop through the NumberOfYears
            Counter = Counter + 1                                                   '       Increment Counter
            OutputArray(Counter, 1) = "Firm " & FirmCount                           '       Save the Firm # into OutputArray
            OutputArray(Counter, 2) = SourceArray(ArrayRow, 1)                      '       Save the Year into OutputArray
        Next                                                                        '   Loop back
    Next                                                                            ' Loop back
'
    Counter = 0                                                                     ' Reset Counter
    For ArrayColumnNumber = 2 To UBound(SourceArray, 2) Step 2                      ' Loop through the columns of SourceArray
        For ArrayRow = 1 To NumberOfYears                                           '   Loop through the NumberOfYears
            Counter = Counter + 1                                                   '       Increment Counter
            OutputArray(Counter, 3) = SourceArray(ArrayRow, ArrayColumnNumber)      '       Save the EPS into OutputArray
            OutputArray(Counter, 4) = SourceArray(ArrayRow, ArrayColumnNumber + 1)  '       Save the ESG into OutputArray
        Next                                                                        '   Loop back
    Next                                                                            ' Loop back
'
    WS.Cells(StartRowOfData - 1, LastColumnNumber + ResultOffset).Resize(1, 4) = _
            Array("Firm", "Year", "EPS", "ESG")                                     ' Display Result Headers to sheet
    WS.Cells(StartRowOfData - 1, LastColumnNumber + _
            ResultOffset).Resize(1, 4).Font.FontStyle = "Bold"                      ' Bold the Result Headers
    WS.Cells(StartRowOfData, LastColumnNumber + ResultOffset).Resize(UBound(OutputArray, _
            1), UBound(OutputArray, 2)) = OutputArray                               ' Display results to sheet
'
' Not sure how to no loop this part
    For Each Cel In WS.Range(WS.Cells(StartRowOfData, LastColumnNumber + ResultOffset), _
            WS.Cells(UBound(OutputArray, 1) + 1, LastColumnNumber + ResultOffset))  ' Loop through the 'Firm' column
        If Right(Cel.Value, 1) Mod 2 = 0 Then Cel.Font.Color = 255                  '   Make the cell font red if the firm is an even number
    Next                                                                            ' Loop back
'
    Debug.Print "Time to complete = " & Timer - StartTime & " seconds."             ' Display elapsed time to the 'Immediate' window (CTRL-G) in VBE
    MsgBox "Completed in " & Timer - StartTime & " seconds."                        ' Display message box to user to inform them the script has completed
End Sub

That 'should be' faster.
 
Upvote 0
VBA Code:
Sub DoSomething()
    Dim WS As Worksheet
    Dim DataStart As Range, ResultStart As Range, rng As Range
    Dim I As Long, LastRow As Long, RCnt As Long
    Dim S As String
    Dim VA, SA
    
    Set WS = ActiveSheet
    
    Set DataStart = WS.Range("A1")
    Set rng = DataStart.CurrentRegion
    Set ResultStart = DataStart.Offset(0, rng.Columns.Count + 1)
    
    Application.ScreenUpdating = False
    With WS
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
    
    With ResultStart
        .Resize(1, 4).EntireColumn.ClearContents
        .Offset(0, 0).Value = "Firm"
        .Offset(0, 1).Value = "Year"
        .Offset(0, 2).Value = "EPS"
        .Offset(0, 3).Value = "ESG"
    End With
    
    VA = rng.Offset(1, 0).Resize(rng.Rows.Count - 1, 1).Value
    LastRow = 0
    For I = 1 To rng.Columns.Count - 1 Step 2
        With rng.Offset(0, I).Resize(, 2)
            SA = .Offset(1).Value
            S = Trim(Split(.Range("A1").Value, "-")(0))
            RCnt = .Rows.Count
        End With
        
        With ResultStart
            .Offset(LastRow + 1, 0).Resize(RCnt - 1, 1).Value = S
            .Offset(LastRow + 1, 1).Resize(RCnt - 1, 1).Value = VA
            .Offset(LastRow + 1, 2).Resize(RCnt - 1, 2).Value = SA
        End With
        
        With WS
            LastRow = ResultStart.Offset(0, 1).Offset(.Rows.Count - 1).End(xlUp).Row - 1
        End With
    Next I
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
VBA Code:
Sub DoSomething_rlv01_ModV1()                                                       ' Original = 0.0125 average seconds
'                                                                                   ' 0.0199951171875 average seconds w/ added stipulations
'
    Dim StartTime           As Double
    StartTime = Timer                                                               ' Start the stopwatch
'
    Dim I           As Long, LastRow    As Long, RCnt   As Long
    Dim rng         As Range
    Dim S           As String
    Dim VA          As Variant, SA      As Variant
    Dim WS          As Worksheet
'
    Set WS = ActiveSheet                                                            ' <--- Set this to the proper sheet name
'
    Application.ScreenUpdating = False
'
    Set rng = WS.Range("A1").CurrentRegion
'
    With WS.Range("A1").Offset(0, rng.Columns.Count + 1)
        .Resize(1, 4).EntireColumn.ClearContents
        .Resize(1, 4) = Array("Firm", "Year", "EPS", "ESG")
        .Resize(1, 4).Font.FontStyle = "Bold"                                       ' Bold the Result Headers
    End With
'
    VA = rng.Offset(1, 0).Resize(rng.Rows.Count - 1, 1).Value                       ' Load years into VA array
'
    LastRow = 0
    For I = 1 To rng.Columns.Count - 1 Step 2
        With rng.Offset(0, I).Resize(, 2)
            SA = .Offset(1).Value
            S = Trim(Split(.Range("A1").Value, "-")(0))
            RCnt = .Rows.Count
        End With
        
        With WS.Range("A1").Offset(0, rng.Columns.Count + 1)
            .Offset(LastRow + 1, 0).Resize(RCnt - 1, 1).Value = S
            .Offset(LastRow + 1, 1).Resize(RCnt - 1, 1).Value = VA
            .Offset(LastRow + 1, 2).Resize(RCnt - 1, 2).Value = SA
        End With
'
        LastRow = WS.Range("A1").Offset(0, rng.Columns.Count + 1).Offset(0, 1).Offset(WS.Rows.Count - 1).End(xlUp).Row - 1
    Next
'
    For Each Cel In Range("O2:O55")  ' Loop through the 'Firm' column
        If Right(Cel.Value, 1) Mod 2 = 0 Then Cel.Font.Color = 255                  '   Make the cell font red if the firm is an even number
    Next                                                                            ' Loop back
'
    Application.ScreenUpdating = True
'
    Debug.Print "Time to complete = " & Timer - StartTime & " seconds."             ' Display elapsed time to the 'Immediate' window (CTRL-G) in VBE
    MsgBox "Completed in " & Timer - StartTime & " seconds."                        ' Display message box to user to inform them the script has completed
End Sub
 
Upvote 0
VBA Code:
Sub DoSomething()
    Dim WS As Worksheet
    Dim DataStart As Range, ResultStart As Range, rng As Range
    Dim I As Long, LastRow As Long, RCnt As Long
    Dim S As String
    Dim VA, SA
   
    Set WS = ActiveSheet
   
    Set DataStart = WS.Range("A1")
    Set rng = DataStart.CurrentRegion
    Set ResultStart = DataStart.Offset(0, rng.Columns.Count + 1)
   
    Application.ScreenUpdating = False
    With WS
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
   
    With ResultStart
        .Resize(1, 4).EntireColumn.ClearContents
        .Offset(0, 0).Value = "Firm"
        .Offset(0, 1).Value = "Year"
        .Offset(0, 2).Value = "EPS"
        .Offset(0, 3).Value = "ESG"
    End With
   
    VA = rng.Offset(1, 0).Resize(rng.Rows.Count - 1, 1).Value
    LastRow = 0
    For I = 1 To rng.Columns.Count - 1 Step 2
        With rng.Offset(0, I).Resize(, 2)
            SA = .Offset(1).Value
            S = Trim(Split(.Range("A1").Value, "-")(0))
            RCnt = .Rows.Count
        End With
       
        With ResultStart
            .Offset(LastRow + 1, 0).Resize(RCnt - 1, 1).Value = S
            .Offset(LastRow + 1, 1).Resize(RCnt - 1, 1).Value = VA
            .Offset(LastRow + 1, 2).Resize(RCnt - 1, 2).Value = SA
        End With
       
        With WS
            LastRow = ResultStart.Offset(0, 1).Offset(.Rows.Count - 1).End(xlUp).Row - 1
        End With
    Next I
    Application.ScreenUpdating = True
End Sub
Thank you very much for everyone's help! I really appreciate it
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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