Convert horizontal table and data to vertical table

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

Pretty sure this is impossible but Is there a way to convert the below exported table to something that looks like the table in example 2? The month does not show in the table data but is always in cell AH as the following:

Call month :2023 October


Exported Table

Hour/DAY12345678910111213141516171819202122232425262728293031
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:0011
09:0013213111111132
10:00111112111132152131122122
11:0012111152321222321331122
12:0011331212132211412
13:00115111333114111112232
14:001111121111121332422321
15:001131411134312112151152
16:002211111111113211133232
17:002311154111322
18:0011
19:00
20:00
21:00
22:00
23:00
TOTAL
3​
9​
12​
9​
13​
11​
7​
4​
16​
9​
11​
6​
15​
11​
6​
17​
14​
11​
13​
9​
7​
7​
10​
7​
18​
14​
12​
7​
5​
18​
12​

Example 2

DayTimeMonthYearDay Time
110:00October2023
01/10/2023 10:00​
113:00October2023
01/10/2023 13:00​
114:00October2023
01/10/2023 14:00​
210:00October2023
02/10/2023 10:00​
212:00October2023
02/10/2023 12:00​
213:00October2023
02/10/2023 13:00​
214:00October2023
02/10/2023 14:00​
215:00October2023
02/10/2023 15:00​
216:00October2023
02/10/2023 16:00​
216:00October2023
02/10/2023 16:00​
217:00October2023
02/10/2023 17:00​
217:00October2023
02/10/2023 17:00​
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:
VBA Code:
Sub test()
  Dim i As Long, j As Long, r As Long, c As Long, myRange As Range
  With Worksheets("Sheet1")
  Set myRange = .Range("A1").CurrentRegion
  End With
  j = 1
  With Worksheets("Sheet2")
  For c = 2 To myRange.Columns.Count
    For r = 2 To myRange.Rows.Count - 1
      If myRange.Cells(r, c).Value <> "" Then
        For i = 1 To myRange.Cells(r, c).Value
          .Cells(j, 1).Value = myRange.Cells(1, c).Value
          .Cells(j, 2).Value = myRange.Cells(r, 1).Value
          .Cells(j, 3).Value = MonthName(Month(Worksheets("Sheet1").Range("AH1").Value))
          .Cells(j, 4).Value = Year(Worksheets("Sheet1").Range("AH1").Value)
          .Cells(j, 4).Value = Worksheets("Sheet1").Range("AH1").Value + (myRange.Cells(1, c).Value - 1) + myRange.Cells(r, 1).Value
          j = j + 1
        Next
      End If
    Next
  Next
  End With
End Sub
 
Upvote 0
Thanks for this. I am getting a runtime error on the below at the moment. The data in sheet1 is in A1 to AF26 and 'Call month :2023 October' is in AH1


.Cells(j, 3).Value = MonthName(Month(Worksheets("Sheet1").Range("AH1").Value))
 
Upvote 0
OK, your date is a string then. This should work:
VBA Code:
Sub test()
  Dim i As Long, j As Long, r As Long, c As Long, myRange As Range
  Dim myYear As String, myMonth As String
 
  With Worksheets("Sheet1")
  Set myRange = .Range("A1").CurrentRegion
  myMonth = Mid(.Range("AH1"), InStrRev(.Range("AH1"), " ") + 1, Len(.Range("AH1")))
  myYear = Mid(.Range("AH1"), InStrRev(.Range("AH1"), " ") - 4, 4)
  End With
 
  j = 2
  With Worksheets("Sheet2")
  For c = 2 To myRange.Columns.Count
    For r = 2 To myRange.Rows.Count - 1
      If myRange.Cells(r, c).Value <> "" Then
        For i = 1 To myRange.Cells(r, c).Value
          .Cells(j, 1).Value = myRange.Cells(1, c).Value
          .Cells(j, 2).Value = myRange.Cells(r, 1).Value
          .Cells(j, 3).Value = myMonth
          .Cells(j, 4).Value = myYear
          .Cells(j, 4).Value = DateValue(myRange.Cells(1, c).Value & " " & myMonth & " " & myYear) + myRange.Cells(r, 1).Value
          j = j + 1
        Next
      End If
    Next
  Next
  End With
End Sub
 
Upvote 0
Solution
An alternative to VBA with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Hour/DAY", type time}}),
    #"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type", {"Hour/DAY"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Errors", {"Hour/DAY"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Date"}}),
    #"Added Prefix" = Table.TransformColumns(#"Renamed Columns", {{"Date", each "Oct " & _, type text}}),
    #"Sorted Rows" = Table.Sort(#"Added Prefix",{{"Date", Order.Ascending}}),
    #"Added Suffix" = Table.TransformColumns(#"Sorted Rows", {{"Date", each _ & " 2023", type text}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Suffix",{"Value", "Hour/DAY", "Date"}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Reordered Columns", {{"Hour/DAY", type text}}, "en-US"),{"Hour/DAY", "Date"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Date Time")
in
    #"Merged Columns"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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