Macro to copy I need a macro that will look on sheet1 and copy the values to the next blank row in sheet2 starting at a specific cell.

onlibo

New Member
Joined
Jan 1, 2014
Messages
8
Hi,

This will be an easy one for you folks, but I am a novice in VBA.

Starting in row 9 of sheet1, if there is a value in b9, copy the values in cells b9, c9, d9, g1 and g2 and paste special the values in the same order into the next blank row in sheet2 starting at cell a14. Continue this copy/pasting of data for all rows in sheet1 if there is data in column b for the range b9 through b15. The data copied from cells g1 and g2 will need the same for each record like $g$1 and $g$2$ in a standard formula.

Thanks in Advance for any help you folks can offer,

Scott
 

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
So for example row 10 of sheet 1 meets the criteria, then B10,C10,D10,G1,G2 are copied to A14,B14,C14,D14,E14 and the next row that meets the criteria is copied same way to row 15 of sheet2, is that right?
 
Upvote 0
Please create a copy of the workbook and try this macro on a copy

Code:
Sub copymacro()    Dim wks As Worksheet
    Dim wks2 As Worksheet
    
    Set wks = Worksheets("sheet1")
    Set wks2 = Worksheets("sheet2")
    
    lastrow = wks.Range("A9").End(xlDown).Row
    lastrow2 = 14
    
    For i = 9 To lastrow
        
        If wks.Range("B" & i).Value <> "" Then
            wks.Range("B" & i & ":" & "D" & i).Copy
            wks2.Activate
            wks2.Range("A" & lastrow2).Select
            ActiveSheet.Paste
            
            wks.Activate
            wks.Range("G1:G2").Select
            Selection.Copy
            wks2.Activate
            wks2.Range("D" & lastrow2).Select
            Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
            
            lastrow2 = lastrow2 + 1
        End If
        
        
    Next i
        
    
End Sub
 
Upvote 0
Hi,

That works great! Thanks very much. The only problem is if I run the macro a second time, the data pastes over the existing data in sheet2. Could you please offer a modification to ensure that if there is data in the target cell in sheet2, it will then move down 1 row until the next empty row is available and then paste special?

Best,

Scott
 
Upvote 0
I initially made it dynamic but because you wanted it to start from A14, I had to keep the way I did, lets see how we can it to work. Does this meet your request?
Code:
Sub copymacro()    Dim wks As Worksheet
    Dim wks2 As Worksheet
    
    Set wks = Worksheets("sheet1")
    Set wks2 = Worksheets("sheet2")
    
    lastrow = wks.Range("A9").End(xlDown).Row
    
    lastrow2a = 14
    lastrow2b = wks2.Range("A65536").End(xlUp).Row + 1
    
    If lastrow2b < lastrow2a Then
        lastrow2 = lastrow2a
    ElseIf lastrow2b > lastrow2a Then
        lastrow2 = lastrow2b
    End If
    
    For i = 9 To lastrow
        
        If wks.Range("B" & i).Value <> "" Then
            wks.Range("B" & i & ":" & "D" & i).Copy
            wks2.Activate
            wks2.Range("A" & lastrow2).Select
            ActiveSheet.Paste
            
            wks.Activate
            wks.Range("G1:G2").Select
            Selection.Copy
            wks2.Activate
            wks2.Range("D" & lastrow2).Select
            Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
            
            lastrow2 = lastrow2 + 1
        End If
        
        
    Next i
        
    
End Sub
 
Upvote 0
Change this line of code:
Code:
lastrow2 = 14
To this;
Code:
If wks2.Range("A14") = "" Then
    lastrow2 = 14
Else
    lastrow2 = wks2.Cells(Rows.Count, 1).End(xlUp)(2)
End If
 
Upvote 0
I tried it, but it errored out. Here is the code as it exists in my workbook:

Sub copymacro()
Dim wks As Worksheet
Dim wks2 As Worksheet

Set wks = Worksheets("sheet1")
Set wks2 = Worksheets("sheet2")

lastrow = wks.Range("A9").End(xlDown).Row
If wks2.Range("A14") = "" Then
lastrow2 = 14
Else
lastrow2 = wks2.Cells(Rows.Count, 1).End(xlUp)(2)
End If

For i = 9 To lastrow

If wks.Range("B" & i).Value <> "" Then
wks.Range("B" & i & ":" & "D" & i).Copy
wks2.Activate
wks2.Range("A" & lastrow2).Select
ActiveSheet.Paste

wks.Activate
wks.Range("G1:G2").Select
Selection.Copy
wks2.Activate
wks2.Range("D" & lastrow2).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True

lastrow2 = lastrow2 + 1
End If


Next i
 
Upvote 0
have you tried the modified one I sent?
 
Upvote 0
I apologize. I missed that middle post with the modified code. This one works as expected now. Excellent! I appreciate your help very much.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,784
Members
449,049
Latest member
greyangel23

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