Consulting

Results 1 to 8 of 8

Thread: Macro to combine data under 3rd and 4th Column if condn satisfy for 1st , 2nd column

  1. #1
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location

    Macro to combine data under 3rd and 4th Column if condn satisfy for 1st , 2nd column

    Hello Everybody,
    Good Morning!!
    I am looking for a macro which will combine the data available in 3rd and 4th Column if conditions satisfied for 1st and 2nd Column.

    Below Input and Output data for your reference..

    INPUT
    KM Leg Org Dest
    2,019,769,184 1 BOM DEL
    2,019,769,184 2 DEL BLR
    2,019,770,095 1 BOM MAA
    2,019,770,675 2 MAA DEL
    2,019,770,213 1 SXR IXJ
    2,019,770,213 2 IXJ DEL
    2,019,770,213 3 DEL BOM
    2,019,770,213 4 BOM SXR
    2,019,770,816 2 DEL MAA
    2,019,770,816 3 MAA IXC
    OUTPUT Rule
    KM Leg Org Dest OUTPUT
    2,019,769,184 1 BOM DEL BOM - DEL - BLR KM are same for Leg 1 & 2 hence Org and Dest combined in column E
    2,019,769,184 2 DEL BLR
    2,019,770,095 1 BOM MAA BOM - MAA KM is only one and not matching with any one hence Org and Dest taken as it is in column E
    2,019,770,675 2 MAA DEL MAA - DEL KM is only one and not matching with any one hence Org and Dest taken as it is in column E
    2,019,770,213 1 SXR IXJ SXR - IXJ - DEL - BOM - SXR KM are same for Leg 1, 2, 3 & 4 hence Org and Dest combined in column E
    2,019,770,213 2 IXJ DEL
    2,019,770,213 3 DEL BOM
    2,019,770,213 4 BOM SXR
    2,019,770,816 2 DEL MAA DEL - MAA - IXC KM are same for Leg 2 & 3 hence Org and Dest combined in column E
    2,019,770,816 3 MAA IXC
    2,019,770,800 6 KLH BOM KLH - BOM KM is only one and not matching with any one hence Org and Dest taken as it is in column E


    Thank You,

    Shan

  2. #2
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location

    Macro to combine data under 3rd and 4th Column if condn satisfy for 1st , 2nd column

    Please find attached excel for more understanding...
    Attached Files Attached Files

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try this in your sample file:
    Sub blah()
    Sheets("Sheet1").Copy After:=Sheets(Sheets.Count)  'delete this line later.
    Dim SelRng As Range
    Range("A1").CurrentRegion.Select  'delete this line if it's a nuisance.
    On Error Resume Next
    Set SelRng = Application.InputBox("Select the area to process (4 columns)" & vbLf & "Include column headers (KM, Leg, Org, Dest)" & vbLf & vbLf & "Make sure there's nothing you want to keep in the single column directly to the right of your selection.", "Area to process", Selection.Address, Type:=8)
    On Error GoTo 0
    If Not SelRng Is Nothing Then
      Set myrng = SelRng.Resize(, 5)
      myrng.Subtotal GroupBy:=1, Function:=xlCount, TotalList:=Array(5), SummaryBelowData:=0
      Set ResultsRng = myrng.Resize(myrng.Rows.Count - 2, 1).Offset(2, 4).SpecialCells(xlCellTypeFormulas, 1).Offset(1)
    
      With ResultsRng
        For i = 1 To .Areas.Count
          mystr = .Areas(i).Offset(, -2).Value
          If i = .Areas.Count Then
            Set rngmyStr2 = .Areas(i).Offset(, -1).Resize(myrng.Cells(myrng.Cells.Count).Row - .Areas(i).Row + 1)  'range the 2nd bit of the result
          Else
            Set rngmyStr2 = Range(.Areas(i).Offset(, -1), .Areas(i + 1).Offset(-2, -1))  'range of the 2nd bit of the result
          End If
          If rngmyStr2.Rows.Count > 1 Then
            myStr2 = Application.Transpose(rngmyStr2.Value)
            zzz = Join(Array(mystr, Join(Application.Transpose(rngmyStr2.Value), " - ")), " - ")
          Else
            zzz = .Areas(i).Offset(, -2).Value & " - " & .Areas(i).Offset(, -1).Value
          End If
          .Areas(i).Value = zzz
        Next i
      End With
      myrng.RemoveSubtotal
      myrng.Cells(1).Offset(, 4).Select
    Else
      MsgBox "Process aborted"
    End If
    End Sub
    Note that it relies entirely on changes in the values in column A, so if there happen to be 2 trips with the same values next to each other it will count them as one.
    See comments in the code about the first line copying sheet1. Also attachment.
    Attached Files Attached Files
    Last edited by p45cal; 12-18-2015 at 09:29 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    oh groan, you've cross posted this without providing links (and you seem to have done this routinely in the past).
    http://www.mrexcel.com/forum/excel-q...nd-column.html
    Please please supply links to your other cross posts when you do this; read this to see why: http://www.excelguru.ca/content.php?184
    All forums have the same rules on this.

  5. #5
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location
    Thank you Sir..

    I am sorry for the cross posted... I thought its a two different websites will help me to create the macro. I am really sorry. Hence onwards the same will not be repeated.

    Thank you so much for making me understanding the concept of Cross posting.!!

  6. #6
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location
    Thank you so much for your resolution..... it is working as desired...!!!

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    G'day Shan, For your benefit, please be aware the the VBA community consists of members who are multi forum members or contributors. Hence its all to easy for cross posted issues to be located. I would have it as a guess that almost all sites have as part of their "rules", one which deals with the issue of cross posting. Please take the opportunity to read the rules and then you'll understand why cross posting without the links, irritates a significant majority of the VBA community.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    VBAX Regular
    Joined
    Jun 2015
    Posts
    88
    Location
    Yes Sir.. I agree with you.. the same will not be repeated from me again.. i appolized for the same.

Posting Permissions

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