+ Reply to Thread
Results 1 to 4 of 4

Help converting table of data into new layout

Hybrid View

PFDave Help converting table of data... 11-29-2016, 11:11 AM
Olly Re: Help converting table of... 11-29-2016, 12:43 PM
mike7952 Re: Help converting table of... 11-29-2016, 01:37 PM
PFDave Re: Help converting table of... 11-30-2016, 04:52 AM
  1. #1
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Help converting table of data into new layout

    Hi,

    I can't really explain enough in the subject so here's hoping I get the right audience

    I run an SQL query, I export this to Excel which gives me a list of different locations in column A and then the start and end hours of those locations in Columns B through O

    What I require as an output is on Desired Output sheet.

    I want to know the start and end hours for each different day, and then from that I want to establish which of those days is BDH (late opening) by looking at the average start time only including M-F.

    The range will be ever changing so the number of locations changes when new ones are created.

    Could anyone offer some help on this please?

    I use the below code to create 7 values of each entry in column A of sheet 1, but I can't work out what to do to continue the process for the other fields as they are not simply drag down formulae.

    
    Sub insertit()
        Dim i As Long
        Application.ScreenUpdating = False
        For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Cells(i, 1) <> Cells(i + 1, 1) Or Cells(i, 2) <> Cells(i + 1, 2) Then
                Cells(i + 1, 1).Resize(6, 1).EntireRow.Insert
                Cells(i + 1, 1).Resize(6, 2).Value = Cells(i, 1).Resize(1, 2).Value
            End If
        Next
        Application.ScreenUpdating = True
    End Sub
    Thanks in advance

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Help converting table of data into new layout

    Try:
    Sub foo()
        Dim wsInput As Worksheet
        Dim wsOutput As Worksheet
        Dim lInRow As Long
        Dim lInCol As Long
        Dim sItem As String
        Dim dtModeOpen As Date
        
        On Error GoTo Terminate
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        
        Set wsInput = Worksheets("SQL export")
        Set wsOutput = Worksheets.Add(after:=Worksheets(Worksheets.Count))
        
        wsOutput.Range("A1:F1") = Array("Concatenate", "Expr2", "Weekday", "Open", "Close", "BDH")
        
        lInRow = 2
        
        With wsInput
            Do Until IsEmpty(.Cells(lInRow, 1))
                sItem = .Cells(lInRow, 1).Value
                dtModeOpen = RealTime(Application.WorksheetFunction.Mode(.Cells(lInRow, 2).Resize(1, 7)))
                For lInCol = 1 To 7
                    With wsOutput.Cells(Rows.Count, 1).End(xlUp)
                        .Offset(1, 0).Value = sItem & lInCol
                        .Offset(1, 1).Value = sItem
                        .Offset(1, 2).Value = lInCol
                        With .Offset(1, 3)
                            .Value = RealTime(wsInput.Cells(lInRow, 1 + lInCol).Value)
                            .NumberFormat = "h:mm:ss"
                        End With
                        With .Offset(1, 4)
                            .Value = RealTime(wsInput.Cells(lInRow, 8 + lInCol).Value)
                            .NumberFormat = "h:mm:ss"
                        End With
                        If .Offset(1, 3).Value <> dtModeOpen And .Offset(1, 3).Value <> 0 Then
                            .Offset(1, 5).Value = "BDH"
                        End If
                    End With
                Next lInCol
                lInRow = lInRow + 1
            Loop
        End With
        
    Terminate:
        If Err Then
            Debug.Print "ERROR", Err.Number, Err.Description
            Err.Clear
        End If
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    
    
    Function RealTime(ByVal Minutes As Long)
        RealTime = Minutes / 60 / 24
    End Function
    That will add a new worksheet, containing your required output.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Help converting table of data into new layout

    Another solution

    Sub abc()
     Dim arrOut, i As Long, ii As Long, iii As Long
     
     
     With Worksheets("SQL export")
        ReDim arrOut(1 To (.Cells(Rows.Count, "a").End(xlUp).Row - 1) * 7, 1 To 5)
        For i = 2 To .Cells(Rows.Count, "a").End(xlUp).Row
            For ii = 1 To 7
                iii = iii + 1
                arrOut(iii, 1) = .Cells(i, 1) & ii
                arrOut(iii, 2) = .Cells(i, 1)
                arrOut(iii, 3) = ii
                arrOut(iii, 4) = VBA.Format(.Cells(i, 1 + ii) / 60 / 24, "h:mm AM/PM")
                arrOut(iii, 5) = VBA.Format(.Cells(i, 8 + ii) / 60 / 24, "h:mm AM/PM")
            Next
        Next
     End With
     With Worksheets("Desired output")
        .Cells(1).Resize(, 6) = Array("Concatenate", "Expr2", "Weekday", "Open", "Close", "BDH [?]")
        .Cells(2, 1).Resize(UBound(arrOut), 5) = arrOut
        With .Cells(2, 6).Resize(UBound(arrOut))
            .Formula = "=IF(C2<6,IF(D2>AVERAGEIFS(D:D,B:B,B2,C:C,""<6""),""BDH"",""""),"""")"
            .Value = .Value
        End With
        .Cells.EntireColumn.AutoFit
     End With
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  4. #4
    Valued Forum Contributor PFDave's Avatar
    Join Date
    05-17-2012
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2013
    Posts
    1,067

    Re: Help converting table of data into new layout

    Thank you both for your great solutions. Both fit for purpose and I really do appreciate your help on this.

    Thanks again

    Dave

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Organizing Data Table in a Bar Chart Layout 5
    By dco223 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 02-03-2014, 09:57 AM
  2. Rearranging a data table into a different layout
    By pauldaddyadams in forum Excel General
    Replies: 10
    Last Post: 10-28-2013, 03:37 PM
  3. [SOLVED] Converting data from excel to a difficult text layout
    By djfscouse in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-15-2013, 12:48 PM
  4. Survey Data -- Table Layout
    By Dmerric1 in forum Excel General
    Replies: 0
    Last Post: 08-27-2012, 10:39 AM
  5. Converting one table layout to another
    By copypaste in forum Excel General
    Replies: 2
    Last Post: 05-11-2010, 02:22 PM
  6. Replies: 1
    Last Post: 12-12-2008, 11:38 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1