Cut and Paste Macro

jbvinny

Active Member
Joined
Nov 21, 2007
Messages
274
I need a piece of code that will scan column B. If the value in column B differs from the row above it, I need to paste the value from Column A into Column C in the row that the first listed value in Column B appeared.

I have a feeling I need to Dim variables but I am still learning VBA and no profficient enough just yet.

I know this is hard to explain. I would post a screen shot by my company doesnt allow me to download the html maker.

I appreciate any help!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
This works on my test workbook:
Code:
Sub TestABC()
    Dim Rng As Range
    Dim c As Range
    Set Rng = Range("B2:B" & Range("B65536").End(xlUp).Row)
    For Each c In Rng
        If c.Value <> c.Offset(-1, 0).Value Then
            c.Offset(, 1).Value = c.Offset(, -1).Value
        End If
    Next c
End Sub
 
Upvote 0

jbvinny

Active Member
Joined
Nov 21, 2007
Messages
274
Thanks! I was able to modify the code to work as needed. However, I need to delete the row in which the data was copied from after.....any advice?
 
Upvote 0

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
It doesn't make any sense to Delete a ROW that you just copied data to.

Do you mean to just delete the contents of the CELL that was copied?

You can do that by adding a line to the code after the copy line.
Code:
Sub TestABC()
    Dim Rng As Range
    Dim c As Range
    Set Rng = Range("B2:B" & Range("B65536").End(xlUp).Row)
    For Each c In Rng
        If c.Value <> c.Offset(-1, 0).Value Then
            c.Offset(, 1).Value = c.Offset(, -1).Value
            c.Offset(, -1).ClearContents
        End If
    Next c
    Application.Run ("'" & Wkb.Name & "'!TrendMacro")
End Sub
 
Upvote 0

jbvinny

Active Member
Joined
Nov 21, 2007
Messages
274
You were correct on the deleting of the rows.

I modified your code to:
___________________________________________________

Sub TestABC()
Dim Rng As Range
Dim c As Range
Set Rng = Range("A2:A" & Range("A65536").End(xlUp).Row)
For Each c In Rng
If c.Value = c.Offset(-1, 0).Value Then
c.Offset(-1, 2).Value = c.Offset(0, 1).Value
End If
Next c
End Sub
________________________________________________

After testing it works for the most part. Except when there are more than two occurances in "A". Currently the code scans Column "A" for duplicates. Then copies the data from Column "B" into "C" of the first Row of that occurence. However, if there are three records with the same data in Column "A" I would want to copy the third row of data from "B" and place it in "D". The forth would be copied from "B" and pasted into "E" and so on.....

I realize this is asking a lot but if you can get me started I could do the leg work.

Thanks for all the help!
 
Upvote 0

Datsmart

Well-known Member
Joined
Jun 19, 2003
Messages
7,985
Sorry for the delay, other obligations....

Give this a try:
Code:
Sub TestABCD()
Dim i As Integer
lr = Range("A65536").End(xlUp).Row
For i = 2 To lr
    cnt = 0
    If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
        Cells(i, "C").Value = Cells(i, "B").Value
        Cells(i, "B").Value = ""
        cnt = cnt + 1
    If Cells(i, 1).Value = Cells(i + cnt, 1).Value Then
        Cells(i + 1, "D").Value = Cells(i + 1, "B").Value
        Cells(i + 1, "B").Value = ""
        cnt = cnt + 1
    If Cells(i, 1).Value = Cells(i + cnt, 1).Value Then
        Cells(i + cnt, "E").Value = Cells(i + cnt, "B").Value
        Cells(i + cnt, "B").Value = ""
        cnt = cnt + 1
    If Cells(i, 1).Value = Cells(i + cnt, 1).Value Then
        Cells(i + cnt, "F").Value = Cells(i + cnt, "B").Value
        Cells(i + cnt, "B").Value = ""
        cnt = cnt + 1
    If Cells(i, 1).Value = Cells(i + cnt, 1).Value Then
        Cells(i + cnt, "G").Value = Cells(i + cnt, "B").Value
        Cells(i + cnt, "B").Value = ""
        cnt = cnt + 1
    End If
    End If
    End If
    End If
    i = i + cnt
    End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,924
Messages
6,012,339
Members
441,691
Latest member
starlightmuse

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
Top