Insert rows and values

powder

New Member
Joined
Aug 4, 2005
Messages
10
Hi Forum -- I'm looking for advise to assign values in column B to the unique values in column A. The results will look like column C. Thanks!

------
A | B
------
A |
B |
C | 1
C | 2
C | 3

---
C
---
A1
A2
A3
B1
B2
B3
C1
C2
C3
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
you can try this,

Code:
Sub GetUniqueCouples()
    Dim i, j, k, lastA, lastB As Long
    Dim MyArray() As String
    
   lastA = Cells(Rows.Count, 1).End(xlUp).Row
   lastB = Cells(Rows.Count, 2).End(xlUp).Row
    
    ReDim MyArray(lastA * lastB)
    
    MyArray(0) = "Result"
    
    k = 1
    For i = 2 To lastA
        For j = 2 To lastB
            MyArray(k) = Cells(i, "A").Value & Cells(j, "B").Value
            k = k + 1
        Next j
    Next i
    
    Range("D1:D" & UBound(MyArray) + 1) = WorksheetFunction.Transpose(MyArray)
  
    Cells(1, "E").Value = Cells(1, "D").Value
    Range("D:D").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("E1"), Unique:=True

   'Range("D:D").ClearContents
    
End Sub
 
Upvote 0
This is awesome! Thank you very much.

What if I have another column of values like so:

-----------
A | B | C
-----------
A | |
B | |
C | 1 | Cat
C | 2 | Dog
C | 3 | Rat

---------
Results
---------
A1Cat
A2Dog
A3Rat
B1Cat
B2Dog
B3Rat
C1Cat
C2Dog
C3Rat

I'm trying to learn VBA and appreciate your advise.
 
Upvote 0
things get a bit tricky, i am learning too, that is why I am here :)

PS try not to have empty fields on top. this may cause issues.
A 1 dog
A 1 cat
A 1
B
B

Code:
Sub GetUniqueCouples()
    Dim i, j, k,[COLOR=#ff0000] l,[/COLOR] lastA, lastB[COLOR=#ff0000], lastC[/COLOR] As Long
    Dim MyArray() As String
    
   lastA = Cells(Rows.Count, 1).End(xlUp).Row
   lastB = Cells(Rows.Count, 2).End(xlUp).Row
   [COLOR=#ff0000]lastC = Cells(Rows.Count, 3).End(xlUp).Row[/COLOR]
    
    ReDim MyArray(lastA * lastB [COLOR=#ff0000]* lastC)[/COLOR]
    
    MyArray(0) = "Result"
    
    k = 1
    For i = 2 To lastA
        For j = 2 To lastB
           [COLOR=#ff0000]For l = 2 to lastC[/COLOR]
            MyArray(k) = Cells(i, "A").Value & Cells(j, "B").Value[COLOR=#ff0000] & Cells(l, "C").Value[/COLOR]
            k = k + 1
          [COLOR=#ff0000]Next l[/COLOR]
        Next j
    Next i
    
    Range("D1:D" & UBound(MyArray) + 1) = WorksheetFunction.Transpose(MyArray)
  
    Cells(1, "E").Value = Cells(1, "D").Value
    Range("D:D").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("E1"), Unique:=True

   'Range("D:D").ClearContents
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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