Trying to paste to the next empty row

Needinghlp

New Member
Joined
Feb 27, 2013
Messages
25
Hi VBA experts,

I have an issue I just can't resolve. I have a worksheet (sheet2) which contains values of Yes, No or Unknown in column J. If there is a value of Yes or Unknown, I want to copy certain cells from that worksheet to certain cells in another worksheet (sheet1). On sheet1, column A contains static numbering 1 to 1000 but the rest of the columns will be blank.
Below is the code I'm using which for the most part is working - but... when information is copied into sheet 1, I end up with empty rows between rows of data. Does anyone know how I can fix this code so that I don't get the empty rows.

Private Sub CommandButton1_Click()
A = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

For i = 3 To A

If Worksheets("Sheet2").Cells(i, 10).Value = "Yes" Or Worksheets("Sheet2").Cells(i, 10).Value = "Unknown" Then

Worksheets("Sheet2").Cells(i, 1).Copy
Worksheets("Sheet1").Activate
b = Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
Worksheets("Sheet1").Cells(i, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues
Worksheets("Sheet2").Cells(i, 3).Copy
Worksheets("Sheet1").Activate
b = Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
Worksheets("Sheet1").Cells(i, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues
Worksheets("Sheet2").Cells(i, 4).Copy
Worksheets("Sheet1").Activate
b = Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
Worksheets("Sheet1").Cells(i, 4).Select
Selection.PasteSpecial Paste:=xlPasteValues
Worksheets("Sheet2").Cells(i, 5).Copy
Worksheets("Sheet1").Activate
b = Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
Worksheets("Sheet1").Cells(i, 5).Select
Selection.PasteSpecial Paste:=xlPasteValues
Worksheets("Sheet2").Cells(i, 22).Copy
Worksheets("Sheet1").Activate
b = Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
Worksheets("Sheet1").Cells(i, 14).Select
Selection.PasteSpecial Paste:=xlPasteValues

End If

Next


End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
you have not explained whether you want all the cells in the selected row in sheet2 should go to same row in sheet1 in different columns or not. If that is the case, once you calculated b, you should use the same b through out and should not recalculate until you get to the next iteration of the loop.
it would have helped if you have shown one row in sheet2 and the result you want to see in sheet1.
something similar to
VBA Code:
Sub test1()
    A = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    For i = 3 To A
    If Sheets("Sheet2").Cells(i, 10).Value = "Yes" Or Sheets("Sheet2").Cells(i, 10).Value = "Unknown" Then
        b = Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp).Row
        Sheets("Sheet1").Cells(b, "B").Value = Sheets("Sheet2").Cells(i, "A").Value
        Sheets("Sheet1").Cells(b, "c").Value = Sheets("Sheet2").Cells(i, "c").Value
        Sheets("Sheet1").Cells(b, "d").Value = Sheets("Sheet2").Cells(i, "d").Value
        Sheets("Sheet1").Cells(b, "e").Value = Sheets("Sheet2").Cells(i, "e").Value
        Sheets("Sheet1").Cells(b, "n").Value = Sheets("Sheet2").Cells(i, "v").Value
    End If
    Next i
End Sub
If this helps your qn, pl mark it as solution for the benefit of future readers of this thread
 
Upvote 0

Forum statistics

Threads
1,213,514
Messages
6,114,078
Members
448,547
Latest member
arndtea

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