Link to home
Start Free TrialLog in
Avatar of Hamilj03
Hamilj03

asked on

Excel Copy Macro

I have data in 2 columns A and B.  I want to start in row 1 and copy the value in column B.  Then I need to go down column A and paste the value of B for each cell that it finds a match in column A.  When it finds a new number in B, it will not appear again.  Example:

A                                             B
10                                            Masonry
10                                            Masonry
10                                            Masonry
10                                            Masonry
10                                            Masonry
20                                            Drywall
20                                            Drywall
20                                            Drywall
30                                            Mosaic
30                                            Mosaic
Avatar of Carlos Ramirez
Carlos Ramirez
Flag of United States of America image

Try Copying Unique items using "Sort and Filter":

1) Add an unique title to each column
2) select both columns
3) Click on - Data - Sort & Filter Section - Advanced
4) click on copy to another location
5) in the Copy to box - click on an empty area of your spreadsheet to copy the new list to
6) check the Unique records only box and click on OK

Hope this helps


Just copy this into a module and link it to a button (or simply run it using F5 or F8).

The code assumes the format give above, but does ask the user to give the cell address of column "A" in the above example.

Sub Copy()
Dim rngNumbers As Range, strNumRangeAdd As String
Dim cell As Range, arrNumsUsed() As Long
Dim intArrUB As Integer, i As Integer
Dim bUsed As Boolean, strCopyValue As String

strNumRangeAdd = InputBox("Please give column range with guide numbers, e.g. A2:A365")
Set rngNumbers = Range(strNumRangeAdd)
intArrUB = 0
bUsed = False

For Each cell In rngNumbers
    bUsed = False
    If intArrUB = 0 Then
        ReDim Preserve arrNumsUsed(1)
        arrNumsUsed(1) = cell.Value
        intArrUB = UBound(arrNumsUsed)
        bUsed = True
        strCopyValue = cell.Offset(0, 1).Value
    End If
    i = 1
    If intArrUB > 0 Then
        For i = 1 To intArrUB
            If arrNumsUsed(i) = cell.Value Then
                bUsed = True
            End If
        Next i
    End If
    If bUsed = False Then
        ReDim Preserve arrNumsUsed(intArrUB + 1)
        intArrUB = UBound(arrNumsUsed)
        arrNumsUsed(intArrUB) = cell.Value
        strCopyValue = cell.Offset(0, 1).Value
    End If
    cell.Offset(0, 1).Value = strCopyValue
Next cell
End Sub
Avatar of Hamilj03
Hamilj03

ASKER

Safety,

It seems to work some times but not others.  I may have misled you when I said that they will not be used again.  What I meant was that they are in sequential order.  you will go down the list copying values.  Once you find a new value, then the value being copied will be changed.
ASKER CERTIFIED SOLUTION
Avatar of Hamilj03
Hamilj03

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.