VBA compact attributes in less columns

bogdant

New Member
Joined
Jun 14, 2018
Messages
4
Hi everyone. My first post here but used as inspiration many times.

The original data is on A1:F14 and the desired outcome in K1:M14. I`m looking to use less columns to show attributes for each ID on column A. The single restriction is that the unique strings must be placed on the same column, whatever that is.
Does not matter if the outcome is in the same sheet or a new one.

Any idea would be appreciated.
​​​​​​​Thank you!

idv1v2v3v4v5idv1v2
1ae1ae
2ae2ae
3ae3ae
4ab4ab
5b5b
6bd6db
7bd7db
8bd8db
9cd9dc
10c10c
11c11c
12c12c
13a13a

<tbody>
</tbody>
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this:-
You may need to alter the "Rng" address to suit other data !!!
NB:- The position of the original data in "A2:F14") will be altered by the code.
Code:
[COLOR="Navy"]Sub[/COLOR] MG14Jun43
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] col [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("B2:F14").SpecialCells(xlCellTypeConstants)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Areas
    [COLOR="Navy"]If[/COLOR] Intersect(Dn, Range("B:C")) [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]For[/COLOR] Ac = 2 To 5
            [COLOR="Navy"]If[/COLOR] Application.CountA(Cells(Dn(1).Row, Ac).Resize(Dn.Count)) = 0 [COLOR="Navy"]Then[/COLOR]
                Cells(Dn(1).Row, Ac).Resize(Dn.Count) = Dn.Value
                 Dn.Value = ""
                [COLOR="Navy"]Exit[/COLOR] For
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] Ac
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Mi MickG. It works great!

So I assume will need to change Ac range to reflect the number of columns as well (instead of 5 in this example)?

Thank you!
Bogdan T
 
Upvote 0
You're welcome
NB:- When you alter the number of columns in the range "Rng" you will need to reflect the number of columns in the "Ac" loop.
i.e. "B2:F14" = 5 columns
 
Upvote 0
Just applied to some more examples and something goes wrong. I get R5 values both in V1 and V2 and one value of SR3 is copied below the range.
Am I doing something wrong?

Input:
EKVV1V2V3V4V5V6
2SR3SR4SR6
9SR3SR5SR6
21SR2SR3SR4SR6
112SR1SR3SR5SR6
115SR3SR4SR6
117SR3SR5SR6
349SR3SR5SR6
352SR2SR3SR4SR6
1417SR3SR5SR6
1980SR3SR5SR6
2458SR3SR6
3558SR3SR6
3575SR3SR5SR6
3592SR3SR4SR6

<colgroup><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>


Output:
EKVV1V2V3V4
2SR4SR6SR3
9SR5SR6SR3
21SR4SR2SR6SR3
112SR1SR5SR6SR3
115SR4SR6SR3
117SR5SR6SR3
349SR5SR6SR3
352SR4SR2SR6SR3
1417SR5SR6SR3
1980SR5SR6SR3
2458SR6SR3
3558SR6SR3
3575SR5SR6SR3
3592SR3SR6
SR3



<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
and the code was

Sub MG14Jun43()
Dim Rng As Range, Dn As Range, Ac As Long, col As Range
Set Rng = Range("B2:G15").SpecialCells(xlCellTypeConstants)
For Each Dn In Rng.Areas
If Intersect(Dn, Range("B:C")) Is Nothing Then
For Ac = 2 To 6
If Application.CountA(Cells(Dn(1).Row, Ac).Resize(Dn.Count)) = 0 Then
Cells(Dn(1).Row, Ac).Resize(Dn.Count) = Dn.Value
Dn.Value = ""
Exit For
End If
Next Ac
End If
Next Dn
End Sub
 
Upvote 0
Try this:-
If not what you want please show example of expected results.
NB:- This code takes a few seconds to Run.
Code:
[COLOR="Navy"]Sub[/COLOR] MG15Jun19
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("B2:G15")
Application.ScreenUpdating = False
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng.Columns
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Rw [COLOR="Navy"]In[/COLOR] Dn.Cells
        [COLOR="Navy"]If[/COLOR] Not IsEmpty(Rw.Value) [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] Not .Exists(Rw.Value) [COLOR="Navy"]Then[/COLOR]
                 .Add Rw.Value, Rw
            [COLOR="Navy"]Else[/COLOR]
                [COLOR="Navy"]Set[/COLOR] .Item(Rw.Value) = Union(.Item(Rw.Value), Rw)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Rw
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, P [COLOR="Navy"]As[/COLOR] Range, Fd [COLOR="Navy"]As[/COLOR] Boolean
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]If[/COLOR] Not .Item(K).Column = 2 [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]For[/COLOR] ac = 2 To Rng.Columns.Count
            Fd = False
            [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] P [COLOR="Navy"]In[/COLOR] .Item(K)
                [COLOR="Navy"]If[/COLOR] Not IsEmpty(Cells(P.Row, ac)) [COLOR="Navy"]Then[/COLOR]
                    Fd = True: [COLOR="Navy"]Exit[/COLOR] For
                [COLOR="Navy"]End[/COLOR] If
            [COLOR="Navy"]Next[/COLOR] P
            [COLOR="Navy"]If[/COLOR] Fd = False [COLOR="Navy"]Then[/COLOR]
                [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] P [COLOR="Navy"]In[/COLOR] .Item(K)
                    Cells(P.Row, ac) = P
                    [COLOR="Navy"]If[/COLOR] Not Cells(P.Row, ac).Address = P.Address [COLOR="Navy"]Then[/COLOR] P.Value = ""
                [COLOR="Navy"]Next[/COLOR] P
                [COLOR="Navy"]Exit[/COLOR] For:
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Next[/COLOR] ac
   [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,
this code looks like it takes care of not spreading the same string on multiple columns. The time is ok, my ranges are not that great. I`ll test it more in the following days.

Thank you again for helping with this.

Regards Bogdan T
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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