+ Reply to Thread
Results 1 to 12 of 12

Extract Data from inconsistent number of rows

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Extract Data from inconsistent number of rows

    The attached data is what our Tracking System exports for items that have to be washed for a second time.

    I have been asked to see whether we can compile a report from this data in a relatively simple way.

    I want to be able to extract the following data:

    Accountable Operator
    Date Raised
    Time Raised
    Description (See lines H20 - H28)
    Description (See line E11) where there is no line similar to Row 19

    I imagine it will have something to do with whenever the Log Number changes, or something of that ilk, so was wondering if someone was able to point me in the right direction
    Attached Files Attached Files

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,290

    Re: Extract Data from inconsistent number of rows

    Please amend your sample file to show expected answers.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU

  3. #3
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Re: Extract Data from inconsistent number of rows

    Added. Thank you.
    Attached Files Attached Files
    Last edited by AliGW; 04-26-2024 at 10:44 AM. Reason: Please don't quote unnecessarily - use the Quick Reply button instead.

  4. #4
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,290

    Re: Extract Data from inconsistent number of rows

    If you wany the highlighted results returned... in the indicated format, I >>think<< this will be a nightmare if you're still using Excel 2016. It MIGHT be possible if the purple results werte all returned in a single row along with the yello header information... but it would still be a nightmare.

    Are you OK with VBA? I won't be able to help... but others may. I can move the thread if VBA is a runner.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    07-07-2014
    Location
    Nottingham
    MS-Off Ver
    Office 2016
    Posts
    397

    Re: Extract Data from inconsistent number of rows

    Hi Glenn

    Yes, I thought it would be a nightmare - just by the way the worksheet it laid out.

    Yes, More than happy for you to send this over to VBA.

  6. #6
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2406
    Posts
    44,290

    Re: Extract Data from inconsistent number of rows

    Moved. Good luck!!

  7. #7
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,403

    Re: Extract Data from inconsistent number of rows

    Option Explicit
    
    Sub demo()
    Dim a, b, hdr
    Dim lr As Long, r As Long, j As Long, n As Long
    Dim ws1 As Worksheet, ws2 As Worksheet
    hdr = Array("Accountable Operator", "Date", "Time", "Description")
    
    Application.ScreenUpdating = False
    
    Set ws1 = Sheets("Data")
    Set ws2 = Sheets("Report")
    
    ws1.Activate
    
    lr = Cells(Rows.Count, "L").End(xlUp).Row
    a = Range("A1:M" & lr)
    ReDim b(1 To lr, 1 To 5)
    
    For r = 1 To lr
        If a(r, 13) = "Accountable Operator" Then
            n = n + 1
            b(n, 1) = a(r + 1, 13)        ' Accountable Operator
            b(n, 2) = a(r + 1, 3)         ' Date
            b(n, 3) = a(r + 1, 4)         ' Time
        Else
           If a(r, 5) = "Decription" Then   ' DeScription ?)
              b(n, 4) = a(r + 1, 5)         ' Description
           Else
              If a(r, 8) = "Description" Then
                r = r + 1
                 Do
                    b(n, 5) = a(r, 8)
                    n = n + 1
                    r = r + 1
                 Loop Until a(r, 5) = "Total Contents:"
                 
              End If
           End If
        End If
    Next r
    
    With Sheets("Report")
        .Usedrange.clear
        .[A1].Resize(, 4) = hdr: .[A1].Resize(, 4).Font.Bold = True
        .[A2].Resize(n, 5) = b
        .Columns(1).Resize(, 5).AutoFit
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Input sheet named "Data" and output is "Report" which has "RUN" button.

    NOTE: heading in E is "Decription" not (as expected) "Description" so code (higlighted) needs amending for change in this title.
    Attached Files Attached Files
    Last edited by JohnTopley; 04-27-2024 at 03:17 AM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  8. #8
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,403

    Re: Extract Data from inconsistent number of rows

    Another version with "Description" plus aligned Descriptions in single column

    Option Explicit
    
    Sub demo2()
    Dim a, b, hdr
    Dim lr As Long, r As Long, j As Long, n As Long
    Dim ws1 As Worksheet, ws2 As Worksheet
    
    hdr = Array("Accountable Operator", "Date", "Time", "Description")
    
    Application.ScreenUpdating = False
    
    Set ws1 = Sheets("Data")                           ' Input worksheet
    Set ws2 = Sheets("Report")                         ' Output (Report) worksheet
    
    ws1.Activate
    
    lr = Cells(Rows.Count, "L").End(xlUp).Row          ' Last row of input data
    a = Range("A1:M" & lr)                             ' Input data to array
    ReDim b(1 To lr, 1 To 5)                           ' Dimension output array
    
    For r = 1 To lr                                    ' Loop through input array
        If a(r, 2) = "Raised:" Then
            n = n + 1
            b(n, 1) = a(r, 13)                         ' Accountable Operator
            b(n, 2) = a(r, 3)                          ' Date
            b(n, 3) = a(r, 4)                          ' Time
        Else
           If a(r, 5) = "Description" Then             ' Column E
              b(n, 4) = a(r + 1, 5)                    ' Description
           Else
              If a(r, 8) = "Description" Then          ' Column H
                r = r + 1: n = n + 1
                Do
                    b(n, 4) = a(r, 8)                   ' Descriptions
                    n = n + 1: r = r + 1
                 Loop Until a(r, 5) = "Total Contents:" ' End of "Description" data
                 n = n - 1
              End If
           End If
        End If
    Next r
    
    With Sheets("Report")
        .UsedRange.Clear                                 ' Clear output
        .[A1].Resize(, 4) = hdr                          ' Headings
        .[A1].Resize(, 4).Font.Bold = True               ' "bold" headinfs
        .[A2].Resize(n, 5) = b                           ' Output reprt data
        .Columns(1).Resize(, 5).AutoFit                  ' Autofil columns
        .Activate
    End With
    
    Application.ScreenUpdating = True
    
    End Sub
    Macro is "Demo2"
    Attached Files Attached Files
    Last edited by JohnTopley; 04-27-2024 at 08:39 AM.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,623

    Re: Extract Data from inconsistent number of rows

    try
    Sub test()
        Dim a, x, i As Long, ii As Long, n As Long, r As Range
        If Not [isref(myresult!a1)] Then Sheets.Add(, Sheets("sheet4")).Name = "MyResult"
        With Sheets("sheet4")
            x = Filter(.[transpose(if(m1:m10000="accountable operator",row(1:10000)))], False, 0)
            If UBound(x) = 0 Then MsgBox "somthing is wrong": Exit Sub
            ReDim Preserve x(UBound(x) + 1)
            x(UBound(x)) = .Range("b" & Rows.Count).End(xlUp).Row
            ReDim a(1 To .Rows.Count, 1 To 4)
            For i = 0 To UBound(x) - 1
                n = n + 1
                a(n, 1) = .Cells(x(i) + 1, "m")
                a(n, 2) = .Cells(x(i) + 1, "c")
                a(n, 3) = .Cells(x(i) + 1, "d")
                a(n, 4) = .Cells(x(i) + 4, "e")
                If x(i + 1) - x(i) > 7 Then
                    For ii = x(i) + 6 To x(i + 1) - 4
                        n = n + 1
                        a(n, 2) = "'" & .Cells(ii, "e")
                        a(n, 3) = "'" & .Cells(ii, "f")
                        a(n, 4) = .Cells(ii, "h")
                    Next
                End If
            Next
        End With
        With Sheets("myresult").[a1:d1].Resize(n + 1)
            .CurrentRegion.Clear
            .Rows(1).Font.Bold = True
            .Rows(1).Value = [{"Accountable Operator","Date Raised","Time Raised","Description"}]
            .Rows(2).Resize(n) = a
            .Columns("b:d").Borders.Weight = 2
            For Each r In .Columns(1).SpecialCells(2, 2)
                r.Resize(, 4).Font.Bold = True
                r.Resize(, 4).BorderAround Weight:=3
            Next
            .BorderAround Weight:=3
            .EntireColumn.AutoFit
            .Parent.Select
        End With
    End Sub
    Edit:
    Now I see what you meant by "Do not mind if this area is blank or if it repeats the line above"...
    Last edited by jindon; 04-27-2024 at 04:39 AM.

  10. #10
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,468

    Re: Extract Data from inconsistent number of rows

    Perhab
    Sub perhab()
    Dim a, ar, i&,j&,n&
    a = Sheets("Data").Range("A1:M" & Sheets("Data").cells(Rows.count,"L").End(3).row).value
    Redim ar(1 to Ubound(a,1),1 to 5)
    For i = 1 to Ubound(a,1)
     If a(i,13) = "Accountable Operator" then
      n = n + 1
      ar(n,1) = a(i+1,13)
      ar(n,2) = a(i+1,3)
      ar(n,3) = a(i+1,4)
     Elseif a(i,8) = "Description" then
       n = n + 1
       ar(n,4) = a(i+1,5)
       For j = i+1 to (i+1000)
         If j > Ubound(a,1) then exit for
         n = n + 1
         ar(n,4) = a(j,8)
         If a(j,5) = "Total Contents:" then exit for
       Next j
      n = n -1
     End if
    Next i
    With Sheets("Report")
    .[A1].resize(,4).value =Array("Accountable Operator", "Date", "Time", "Description")
    .[A2].resize(Ubound(ar,1),5).value = ar
    End with
    Erase ar
    End sub
    Last edited by daboho; 04-27-2024 at 07:15 AM.

  11. #11
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,336

    Re: Extract Data from inconsistent number of rows

    And so the options keep coming...
    Sub J3v16()
    Dim Data, Temp, i As Long, x As Long
    With Sheets("Data")
        Data = .Range("B7:M" & .Cells(.Rows.Count, 12).End(xlUp).Row)
        ReDim Temp(1 To UBound(Data), 1 To 5)
    End With
    For i = 1 To UBound(Data)
        If Data(i, 1) = "Raised:" Then
            x = x + 1: Temp(x, 1) = Data(i, 12)
            Temp(x, 2) = Data(i, 2)
            Temp(x, 3) = Data(i, 3)
            Temp(x, 4) = Data(i + 3, 4)
            i = i + 2
        ElseIf Data(i, 7) = "Description" Then
            Do Until Data(i + 1, 4) Like "*Contents:"
                Temp(x, 5) = Data(i + 1, 7)
                x = x + 1: i = i + 1
            Loop
        End If
    Next i
    With Sheets("Report")
        .Cells(1, 1).Resize(, 4) = [{"Accountable Operator","Date","Time","Description"}]
        .Cells(2, 1).Resize(x, 5) = Temp
    End With
    End Sub
    Attached Files Attached Files
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  12. #12
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,468

    Re: Extract Data from inconsistent number of rows

    Deleted ......

+ 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. [SOLVED] Extract specific strings that is next to a specific text - inconsistent data format
    By pbjgun in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-08-2022, 06:29 AM
  2. Replies: 3
    Last Post: 05-03-2022, 05:40 AM
  3. Macro inserting data after several blank rows - inconsistent
    By ashley72788 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2017, 08:17 AM
  4. [SOLVED] Conditional Formatting complete rows in data table - inconsistent
    By BuZZarD73 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-13-2014, 10:31 AM
  5. Replies: 3
    Last Post: 09-26-2014, 01:07 AM
  6. [SOLVED] Use a formula within a macro to extract data and copy down x number of rows
    By rdowney79 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-15-2013, 12:25 PM
  7. Inconsistent data rows
    By junada0 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-12-2010, 04:15 PM

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