+ Reply to Thread
Results 1 to 10 of 10

Analyze data in specific way

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Analyze data in specific way

    Hello everyone
    I need to analyze and split data in specific way
    In fact it is hard to describe my issue ... so I have attached the expected output in "Final Sheet" starting in Column K till column Y
    Hope it is clear (see the attachment)
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Analyze data in specific way

    See attached file, this the code that I used:
    Sub Maco1()
       Dim rs As Object, lastRow As Long
       Dim r As Long
       Dim sourceSh As Worksheet
       Dim finalSh As Worksheet
       Dim myEvent As String, myGender As String, myLcMeter As Single
       Dim myNatRec As String, myYear As Single, myName2 As String
       Dim myTeam2 As String, myHeat As Single, myStartTime As String
       Dim myType As String, myText As String, myText2 As String
       Dim myText3() As String, p1 As Integer
          
       Const adInteger = 3
       Const adSingle = 4
       Const adDate = 7
       Const adVarChar = 200
       Const adDouble = 5
       Const adBinary = 128
       
       On Error GoTo lbl_err
       
       Application.ScreenUpdating = False
       
       'create an ADODB.Recordset and call it rs
       Set rs = CreateObject("ADODB.Recordset")
          
       'create the various rows of the recordset
       With rs.Fields
          .Append "event", adVarChar, 12
          .Append "heat", adSingle
          .Append "lane", adSingle
          .Append "startTime", adVarChar, 12
          .Append "name", adVarChar, 35
          .Append "age", adSingle
          .Append "team", adVarChar, 8
          .Append "time2", adVarChar, 12
          .Append "gender", adVarChar, 7
          .Append "lcMeter", adSingle
          .Append "type", adVarChar, 24
          .Append "natRec", adVarChar, 10
          .Append "year", adSingle
          .Append "name2", adVarChar, 26
          .Append "Team2", adVarChar, 14
       End With
       rs.Open
       
       With ThisWorkbook
          Set sourceSh = .Sheets(1)
          Set finalSh = .Sheets(2)
       End With
       
       lastRow = sourceSh.Cells(Rows.Count, "a").End(xlUp).Row
       'carico dati
       For r = 1 To lastRow
          myText = sourceSh.Cells(r, "a")
          myText2 = Trim(myText)
          Do While InStr(myText2, "  ") > 0
             myText2 = Replace(myText2, "  ", " ")
          Loop
          myText3 = Split(myText2, " ")
          
          If myText Like "Event*" Then
             myEvent = Trim(Left(myText, 9))
             myGender = myText3(2)
             myLcMeter = CSng(myText3(6))
             myType = Trim(Mid(myText, 42, 20))
          ElseIf myText Like "*National:*" Then
             myNatRec = Trim(Mid(myText, 15, 8))
             myYear = CSng(Trim(Mid(myText, 25, 10)))
             p1 = InStrRev(myText, ",")
             myName2 = Trim(Mid(myText, 29, p1 - 29))
             myTeam2 = Trim(Mid(myText, p1 + 1))
          ElseIf myText Like "Heat*" Then
             myHeat = CSng(Trim(Mid(myText, 5, 3)))
             myStartTime = Trim(Mid(myText, 33, 10))
             
          ElseIf IsNumeric(Trim(Left(myText, 5))) Then
             rs.addnew
             rs("event") = myEvent
             rs("heat") = myHeat
             rs("lane") = CSng(Trim(Left(myText, 5)))
             rs("startTime") = myStartTime
             rs("name") = Trim(Mid(myText, 4, 32))
             rs("age") = CSng(Mid(myText, 36, 2))
             rs("team") = Trim(Mid(myText, 38, 6))
             rs("time2") = Trim(Mid(myText, 62, 10))
             rs("gender") = myGender
             rs("lcMeter") = myLcMeter
             rs("type") = myType
             rs("natRec") = myNatRec
             rs("year") = myYear
             rs("name2") = myName2
             rs("Team2") = myTeam2
             rs.Update
          End If
       Next r
      
       'if necessary to apply a filter
       'rs.Filter = "cognome like '*a*'"
       
       'sort [asc], desc
       rs.Sort = "event, heat,lane"
       
       'output data sorted and filtered
       With finalSh
          .Range("5:" & Rows.Count).Delete
          .Range("a5").CopyFromRecordset rs
          rs.Close
          Set rs = Nothing
       
          'formatting
          .Range("a:d").Font.Color = RGB(192, 0, 0)
          .Range("l:o").Font.Color = RGB(102, 112, 213)
          
          lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
          
          For r = lastRow To 7 Step -1
             If .Cells(r, 1) <> .Cells(r - 1, 1) Then
                .Rows(r & ":" & r + 6).Insert
                
                .Range("a3:o5").Copy .Cells(r + 5, 1)
             ElseIf .Cells(r, 2) <> .Cells(r - 1, 2) Then
                .Rows(r).Insert
             End If
          Next r
       End With
    
       Application.ScreenUpdating = True
    
    lbl_exit:
       Exit Sub
    
    lbl_err:
       'Stop
       'Resume Next
       
       MsgBox ("An error occurred in the code")
       Resume lbl_exit
    End Sub
    Regards,
    Antonio
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Analyze data in specific way

    Thanks a lot Mr. Antonio for this awesome and fascinating code
    Just a little remark ..
    There is Event 33 repeated at all events at the beginning of each group
    Take a look at the results

  4. #4
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628

    Re: Analyze data in specific way

    Excuse me, in my code there was one error:
       .Range("a3:o5").Copy .Cells(r + 5, 1)
    The right code is:
       .Range("a3:o3").Copy .Cells(r + 5, 1)
    Regards,
    Antonio
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Analyze data in specific way

    Thank you very much for this distinguished solution
    It's perfect
    Regards

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Analyze data in specific way

    Hi Yasser,

    Here's mine:

    Sub YasserSports(): Dim Hd As String, HL As Range, c As Long, r As Long, ws As Worksheet, wf As Worksheet
                        Dim E, N, H, Z, u As Long, v As Long, w As Long, x As Long, S As String: r = 3
    Set ws = Sheets("Source Sheet"): Set wf = Sheets("Final Sheet"): ws.UsedRange.Offset(2).Copy wf.Range("A1")
    Hd = "Event|Heat|Lane|Start Time|Name|Age|Team|Time|Gender|LC Meter|Type|National Record|Year|Name|Team"
            Columns("K:N").Font.ColorIndex = 3: Columns("V:Y").Font.ColorIndex = 31
    Set HL = Cells(8, "K").Resize(1, 15): HL.Interior.ColorIndex = 43: HL.Value = Split(Hd, "|")
        HL.Borders.Weight = xlThin: HL.BorderAround Weight:=xlMedium: HL.Font.Bold = True
    GetEv:
         Do: r = r + 1: Loop Until InStr(1, Range("A" & r), "Event")
         E = Split(WorksheetFunction.Trim(Range("A" & r))): v = UBound(E)
    GetNat:
        Do: r = r + 1: Loop Until InStr(1, Range("A" & r), "National")
        N = Split(WorksheetFunction.Trim(Range("A" & r))): x = UBound(N)
        HL.Copy Cells(r + 2, "K")
    GetHt:
            Do: r = r + 1: Loop Until InStr(1, Range("A" & r), "Heat")
            H = Split(WorksheetFunction.Trim(Range("A" & r))): w = UBound(H)
                Do: r = r + 1
                    If InStr(1, Range("A" & r + 1), "Event") Then GoTo GetEv
    
                    If r > Range("A" & Rows.count).End(xlUp).Row Then GoTo ExitSub
                    Z = Split(WorksheetFunction.Trim(Range("A" & r))): u = UBound(Z)
                            
    LoadLine: Range("K" & r) = E(0) & " " & E(1): Range("L" & r) = H(1): Range("M" & r) = Z(0)
              Range("N" & r) = H(w - 1) & " " & H(w):           Range("Y" & r) = N(x)
              For c = 1 To u - 3: S = S & " " & Z(c): Next c: Range("O" & r) = Trim(S): S = ""
              Range("P" & r) = E(3): Range("Q" & r) = Z(u - 1): Range("R" & r) = Z(u)
              S = E(2): Range("S" & r) = Left(S, Len(S) - 1): S = "": Range("T" & r) = E(v - 3)
              Range("U" & r) = E(v): Range("V" & r) = N(1): Range("W" & r) = N(2)
              For c = 3 To x - 1: S = S & " " & N(c): Next c: Range("X" & r) = Trim(S): S = ""
              If InStr(1, Range("A" & r + 1), "Heat") Then GoTo GetHt
              
                Loop Until r > Range("A" & Rows.count).End(xlUp).Row
    ExitSub:                wf.Columns("K:Y").AutoFit
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Analyze data in specific way

    Thank you very much Mr. aka Orrin (Is that your real name?)
    you provided me a great solution too ..
    Thanks a lot for this great and wonderful help
    Regards

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Analyze data in specific way

    Hi Yasser,

    You're welcome and thanks for the rep - here's a correction for the ", 6 Oct:

    Sub YasserSports(): Dim Hd As String, HL As Range, c As Long, r As Long, ws As Worksheet, wf As Worksheet
                        Dim E, N, H, Z, u As Long, v As Long, w As Long, x As Long, S As String: r = 3
    Set ws = Sheets("Source Sheet"): Set wf = Sheets("Final Sheet"): ws.UsedRange.Offset(2).Copy wf.Range("A1")
    Hd = "Event|Heat|Lane|Start Time|Name|Age|Team|Time|Gender|LC Meter|Type|National Record|Year|Name|Team"
            Columns("K:N").Font.ColorIndex = 3: Columns("V:Y").Font.ColorIndex = 31
    Set HL = Cells(8, "K").Resize(1, 15): HL.Interior.ColorIndex = 43: HL.Value = Split(Hd, "|")
        HL.Borders.Weight = xlThin: HL.BorderAround Weight:=xlMedium: HL.Font.Bold = True
    GetEv:
         Do: r = r + 1: Loop Until InStr(1, Range("A" & r), "Event")
         E = Split(WorksheetFunction.Trim(Range("A" & r))): v = UBound(E)
    GetNat:
        Do: r = r + 1: Loop Until InStr(1, Range("A" & r), "National")
        S = WorksheetFunction.Trim(Range("A" & r)): S = Replace(S, ",", "")
        N = Split(S): x = UBound(N): S = N(x - 1) & " " & N(x)
                        If IsDate(S) Then
        N(x) = S: N(x - 1) = " ": S = "": End If
                    HL.Copy Cells(r + 2, "K")
    GetHt:
            Do: r = r + 1: Loop Until InStr(1, Range("A" & r), "Heat")
            H = Split(WorksheetFunction.Trim(Range("A" & r))): w = UBound(H)
                Do: r = r + 1
                    If InStr(1, Range("A" & r + 1), "Event") Then GoTo GetEv
    
                    If r > Range("A" & Rows.count).End(xlUp).Row Then GoTo ExitSub
                    Z = Split(WorksheetFunction.Trim(Range("A" & r))): u = UBound(Z)
                            
    LoadLine: Range("K" & r) = E(0) & " " & E(1): Range("L" & r) = H(1): Range("M" & r) = Z(0)
              Range("N" & r) = H(w - 1) & " " & H(w):           Range("Y" & r) = N(x)
              For c = 1 To u - 3: S = S & " " & Z(c): Next c: Range("O" & r) = Trim(S): S = ""
              Range("P" & r) = E(3): Range("Q" & r) = Z(u - 1): Range("R" & r) = Z(u)
              S = E(2): Range("S" & r) = Left(S, Len(S) - 1): S = "": Range("T" & r) = E(v - 3)
              Range("U" & r) = E(v): Range("V" & r) = N(1): Range("W" & r) = N(2)
              For c = 3 To x - 1: S = S & " " & N(c): Next c: Range("X" & r) = Trim(S): S = ""
              If InStr(1, Range("A" & r + 1), "Heat") Then GoTo GetHt
              
                Loop Until r > Range("A" & Rows.count).End(xlUp).Row
    ExitSub:                wf.Columns("K:Y").AutoFit
    End Sub
    * Yes, Orrin is my real name (it means "Fair One" in the Welsh from which it was derived) - Does Yasser mean something??

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Analyze data in specific way

    Thanks a lot Mr. Orrin for correction
    As for my name it means in Arabic (my language) the easy man ... What about aka (is this a name? or what?)

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Analyze data in specific way

    Hi Yasser,

    AKA is a mnemonic for "Also Known As" it is used to describe an entities alias

    The easy man? - You're projects are normally fairly complex

+ 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] Analyze number to specific results
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-30-2015, 03:54 PM
  2. [SOLVED] Macro to analyze the data
    By dobracik in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-25-2012, 03:54 PM
  3. [SOLVED] Macro to analyze the Raw Data
    By dobracik in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-12-2012, 02:52 PM
  4. How to analyze comment data
    By liqdsunshyn in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-01-2012, 10:47 AM
  5. Formula to analyze data?
    By djw2000 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-04-2012, 06:14 AM
  6. Using LOOPS with IF to analyze specific cell ranges
    By seaottr in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-11-2010, 03:10 AM
  7. Analyze Data
    By tokentrinkit in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-18-2006, 03:05 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