Solved

Excel Copy Macro

Posted on 2011-09-07
6
183 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Hamilj03
6 Comments
 
LVL 5

Expert Comment

by:slycoder
Comment Utility
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


0
 
LVL 4

Expert Comment

by:SafetyFish
Comment Utility
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
0
 

Author Comment

by:Hamilj03
Comment Utility
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.
0
 

Accepted Solution

by:
Hamilj03 earned 0 total points
Comment Utility
Got it.
Sub copycell()
Dim numerial1 As Integer
Dim numerial2 As Integer
Dim description1 As String

    Range("D2").Select
    numerial1 = ActiveCell.Value
    ActiveCell.Offset(0, 1).Select
    description1 = ActiveCell.Value
    ActiveCell.Offset(1, -1).Select
    numerial2 = ActiveCell.Value
Do
    
    If numerial1 = numerial2 Then
        ActiveCell.Offset(0, 1).Select
        ActiveCell.Value = description1
        
    Else
        numerial1 = ActiveCell.Value
        ActiveCell.Offset(0, 1).Select
        description1 = ActiveCell.Value
    End If
    ActiveCell.Offset(1, -1).Select
    numerial2 = ActiveCell.Value
Loop Until IsEmpty(ActiveCell)

End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:teylyn
Comment Utility
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now