combine same values three columns

cnu_753

New Member
Joined
Apr 30, 2017
Messages
10
Hi,
Actually I am working with excel since 5yrs but very new to <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">vba</acronym> and very interested to learn also. Please find the following post

DATA
12LHS
23LHS
34RHS
45RHS
56LHS

<tbody>
</tbody>


<tbody>
</tbody>

and my result will be like this

RESULT
13LHS
35RHS
56LHS

<tbody>
</tbody>


Kindly give <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-top-style: initial; border-right-style: initial; border-bottom-style: dotted; border-left-style: initial; border-top-color: initial; border-right-color: initial; border-bottom-color: rgb(0, 0, 0); border-left-color: initial; border-image: initial; cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> for this.
Thanking you
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Assuming the sample data you provided starts in A1 and ends in C5...

I'm assuming what you want is...for each group of Column C values, you want the end result to be
1. the minimum value from Column A & Column B in the same group
2. the maximum value from Column A & Column B in the same group
3. the corresponding value from Column C

is that correct?

Where do you want the result to be? Here's a macro I created that accomplishes this:

Code:
Sub test()
Dim i As Integer, x As Integer, y As Integer

For y = 1 To 5
    x = x + 1
    
    Do
        i = i + 1
    Loop Until Worksheets("Sheet1").Range("C" & i).Value <> Worksheets("Sheet1").Range("C" & i + 1).Value
    
    Worksheets("Sheet1").Range("E" & x).Value = Application.WorksheetFunction.Min(Worksheets("Sheet1").Range("A" & y & ":B" & i))
    Worksheets("Sheet1").Range("F" & x).Value = Application.WorksheetFunction.Max(Worksheets("Sheet1").Range("A" & y & ":B" & i))
    Worksheets("Sheet1").Range("G" & x).Value = Worksheets("Sheet1").Range("C" & i).Value
    
    y = i
Next y
End Sub

There is likely a better (easier, prettier) way to do this, but with your sample data, the results I received were the results you provided.

Currently, the macro is putting the results in Columns E-G, but that can be adjusted according to your needs. Also note that it is currently only looping through rows 1-5 (y = 1 To 5)...so if you'll need to adjust that to suit your needs as well. If your range of data is dynamic, you'll need to include some lines to find the last row that has data; I can help you with that if needed.
 
Last edited:
Upvote 0
Hi
what ever u said is correct, my data is dynamic and it wont be as continues numbers as i sent to you
66+60067+300LHS
67+30067+600LHS
67+60067+980LHS
67+98068+230LHS
68+23068+760LHS
68+76068+900LHS
68+90069+240LHS
69+24069+500LHS
69+50070+000LHS
70+00070+500LHS
70+75071+400LHS
71+40071+800LHS
71+80072+000LHS
72+00072+280LHS
72+28072+580LHS
72+58072+780LHS
72+78072+980LHS
72+98073+080LHS
73+08073+170LHS
73+17073+260LHS
73+26073+400LHS
73+40073+600LHS
73+60074+000LHS
74+00074+220LHS
74+22074+280LHS
76+51076+620LHS
76+62076+920LHS
76+92077+190LHS
77+19077+400LHS
77+40077+590LHS
77+59078+080LHS
78+08078+400LHS
78+40078+730LHS
78+73078+850LHS
78+85079+250LHS
79+25079+460LHS
79+46079+780LHS
79+78080+100LHS
80+10080+460LHS
80+46080+760LHS
80+76080+960LHS
80+96081+210LHS
81+21081+460LHS
81+46081+920LHS
81+92082+260LHS
82+26082+750LHS
85+26086+060LHS
86+06086+820LHS
86+98088+000LHS
66+60067+450LHS
67+45067+680LHS
67+68068+030LHS
68+03068+450LHS
68+45068+900LHS
68+90069+000LHS
69+00069+500LHS
69+50069+820LHS
69+82070+500LHS
70+75071+400LHS
71+40072+000LHS
72+00072+500LHS
72+50072+800LHS
72+80073+290LHS
73+29073+500LHS
73+50073+900LHS
73+90074+220LHS
74+22074+280LHS
76+51076+620LHS
76+62076+950LHS
76+95077+400LHS
77+40077+590LHS
77+59078+080LHS
78+08078+400LHS
78+40078+720LHS
78+72078+870LHS
78+87079+130LHS
79+13079+420LHS
79+42079+790LHS
79+79080+120LHS
80+12080+490LHS
80+49080+820LHS
80+82081+070LHS
81+07081+500LHS
81+50081+940LHS
81+94082+260LHS
82+26082+750LHS
85+26086+060LHS
86+06086+820LHS
86+98088+000LHS
66+60067+550RHS
67+55068+500RHS
68+50069+400RHS
69+40070+500RHS
70+75071+400RHS
71+40072+000RHS
72+00072+500RHS
72+50073+000RHS
73+00073+500RHS
73+50074+000RHS
74+00074+250RHS
74+25074+280RHS
76+51076+620RHS
76+62076+890RHS
76+89077+110RHS
77+11077+400RHS
77+40077+580RHS
77+58078+000RHS
78+00078+210RHS
78+21078+400RHS
78+40078+720RHS
78+72078+870RHS
78+87079+400RHS
79+40079+600RHS
79+60079+940RHS
79+94080+000RHS
80+00080+340RHS
80+34080+470RHS
80+47080+760RHS
80+76080+880RHS
80+88080+940RHS
80+94081+080RHS
81+08081+200RHS
81+20081+400RHS
81+40081+850RHS
81+85081+950RHS
81+95082+650RHS
82+65082+750RHS
85+26086+060RHS
86+06086+820RHS
86+98088+000RHS
66+60067+550RHS
67+55068+500RHS
68+50069+400RHS
69+40070+500RHS
70+75071+400RHS
71+40072+000RHS
72+00072+500RHS
72+50073+000RHS
73+00073+500RHS
73+50074+000RHS
74+00074+250RHS
74+25074+280RHS
76+51076+620RHS
76+62076+800RHS
76+80076+920RHS
76+92077+400RHS
77+40077+600RHS
77+60077+900RHS
77+90078+100RHS
78+10078+400RHS
78+40078+720RHS
78+72078+870RHS
78+87079+370RHS
79+37079+600RHS
79+60079+700RHS
79+70080+270RHS
80+27080+380RHS
80+38080+780RHS
80+78081+180RHS
81+18081+480RHS
81+48081+720RHS
81+72082+200RHS
82+20082+750RHS
85+26086+060RHS
86+06086+820RHS
86+98088+000RHS

<colgroup><col><col><col></colgroup><tbody>
</tbody>
This is my data sir,

In some lines from value and to value wont be equal.
and my result for the above will be as follows

6660070500LHS
7075074280LHS
7651082750LHS
8526085820LHS
8598088000LHS
6660070500RHS
7140074280RHS
7651082750RHS
8526085820RHS
8598088000RHS

<colgroup><col span="3"></colgroup><tbody>
</tbody>
 
Upvote 0
What are the + signs?

The code I provided won't work at all for your actual data...

The first line of your result is 66600/70500 LHS...why is 70500 the result?

If I'm looking at this correctly, the first group isn't actually defined by the value in the third column, but it's actually defined by the second column not being the same as the first column in the next row? Like so:


70+000
70+500
LHS
70+750
71+400LHS

<tbody>
</tbody>

Is that right?
 
Upvote 0
1. You didn't have the + signs in your results...is that intentional?
2. Looking at your data, it looks like you pasted all of the RHS values twice...is that intentional?

66+600
70+500LHS
70+75074+280LHS
76+51082+750LHS
85+26086+820LHS
86+98088+000LHS
66+600
70+500RHS
70+75074+280RHS
76+51082+750RHS
85+26086+820
RHS
86+98088+000RHS
66+600
70+500RHS
70+75074+280RHS
76+51082+750RHS
85+26086+820RHS
86+98088+000RHS

<tbody>
</tbody>

I have a new macro and this was the result of it...As you can see, the blue and orange is a repeat...did you accidentally paste that section twice or is that actually how your data looks?

Here's the macro I have:

Code:
Sub test()
Dim lRow As Integer
Dim i As Integer, x As Integer, y As Integer

lRow = Worksheets("Sheet1").Range("A1").End(xlDown).Row

For y = 1 To lRow
    x = x + 1
    
    Do
        i = i + 1
    Loop Until Worksheets("Sheet1").Range("C" & i).Value <> Worksheets("Sheet1").Range("C" & i + 1).Value Or Worksheets("Sheet1").Range("B" & i).Value <> Worksheets("Sheet1").Range("A" & i + 1).Value
    
    Worksheets("Sheet1").Range("E" & x).Value = Worksheets("Sheet1").Range("A" & y).Value
    Worksheets("Sheet1").Range("F" & x).Value = Worksheets("Sheet1").Range("B" & i).Value
    Worksheets("Sheet1").Range("G" & x).Value = Worksheets("Sheet1").Range("C" & i).Value
    
    y = i
Next y
End Sub

If you want to remove the + signs from your results, you can use this modified code:

Code:
Sub test()
Dim lRow As Integer
Dim i As Integer, x As Integer, y As Integer

lRow = Worksheets("Sheet1").Range("A1").End(xlDown).Row

For y = 1 To lRow
    x = x + 1
    
    Do
        i = i + 1
    Loop Until Worksheets("Sheet1").Range("C" & i).Value <> Worksheets("Sheet1").Range("C" & i + 1).Value Or Worksheets("Sheet1").Range("B" & i).Value <> Worksheets("Sheet1").Range("A" & i + 1).Value
    
    Worksheets("Sheet1").Range("E" & x).Value = Replace(Worksheets("Sheet1").Range("A" & y).Value, "+", "")
    Worksheets("Sheet1").Range("F" & x).Value = Replace(Worksheets("Sheet1").Range("B" & i).Value, "+", "")
    Worksheets("Sheet1").Range("G" & x).Value = Replace(Worksheets("Sheet1").Range("C" & i).Value, "+", "")
    
    y = i
Next y
End Sub

If the data was not accidentally pasted twice and that is what it actually looks like, then this will need to be modified again to account for that. Let me know.
 
Last edited:
Upvote 0
Try this:-
Results start "F1"
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Jul57
[COLOR="Navy"]Dim[/COLOR] Ac      [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ray     [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] n       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Dic     [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Dim[/COLOR] K       [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] p       [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] c       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
 
Ray = Range("C1").CurrentRegion.Resize(, 3)
 [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
       [COLOR="Navy"]For[/COLOR] n = 1 To UBound(Ray, 1)
            [COLOR="Navy"]If[/COLOR] Not Dic.Exists(Ray(n, 3)) [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]Set[/COLOR] Dic(Ray(n, 3)) = CreateObject("Scripting.Dictionary")
            [COLOR="Navy"]End[/COLOR] If
              [COLOR="Navy"]For[/COLOR] Ac = 1 To 2
                [COLOR="Navy"]If[/COLOR] Not Dic(Ray(n, 3)).Exists(Ray(n, Ac)) [COLOR="Navy"]Then[/COLOR]
                    Dic(Ray(n, 3)).Add (Ray(n, Ac)), Ac
                [COLOR="Navy"]Else[/COLOR]
                    [COLOR="Navy"]If[/COLOR] Not Dic(Ray(n, 3)).Item(Ray(n, Ac)) = Ac [COLOR="Navy"]Then[/COLOR]
                        Dic(Ray(n, 3)).Remove (Ray(n, Ac))
                    [COLOR="Navy"]End[/COLOR] If
                [COLOR="Navy"]End[/COLOR] If
             [COLOR="Navy"]Next[/COLOR] Ac
      [COLOR="Navy"]Next[/COLOR] n
   
    
c = 1
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] Dic.keys
       Ac = 0
       [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] p [COLOR="Navy"]In[/COLOR] Dic(K)
            Ac = Ac + 1
            Cells(c, "H") = K
            Cells(c, "E").Offset(, Ac) = Replace(p, "+", "")
                [COLOR="Navy"]If[/COLOR] Ac Mod 2 = 0 [COLOR="Navy"]Then[/COLOR]
                    c = c + 1: Ac = 0
                [COLOR="Navy"]End[/COLOR] If
       [COLOR="Navy"]Next[/COLOR] p
    [COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,215,268
Messages
6,123,969
Members
449,137
Latest member
yeti1016

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