简体   繁体   中英

Copy paste only part of rows, with one cell containing a picture, on another sheet when condition is met

I have a worksheet named Data where I listed products in rows. The information for each product are in columns A:W. The column A is used for companies ID. Depends on the ID selected in a dropdown list, I want the cells P:W to be pasted in a worksheet named Quote starting from row 11 in columns A:H. It must be noted the column W in the worksheet Data is used for pictures.

I found some codes over the Web and tried to adapt it to my case with no success. First, I cannot copy paste the pictures. I assume I should use the Range.Copy method instead of PasteSpecial method, but I cannot make it works in my code. Second, I could not find a proper way to copy and paste only the cells from columns P:W.

Sub Quote()

Dim Data As Worksheet
Dim Quote As Worksheet
Dim CompanyID As String
Dim Finalrow As Integer
Dim i As Integer

Set Data = Sheet3
Set Quote = Sheet2
CompanyID = Sheet6.Range("E5").Value

Data.Select
Finalrow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To Finalrow
    If Cells(i, 1) = CompanyID Then
    Range(Cells(i, 1), Cells(i, 23)).Copy
    Quote.Select
    Rows(11).Select
    Range("A200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
    Data.Select
    End If

Next i

Quote.Select
Range("A11:O200").Delete
Range("A1").Select

End Sub

Try

Data.Range(Cells(i, 16), Cells(i, 23)).Copy Quote.Range("A200").End(xlUp).Offset(1, 0).resize(1,8)

You can delete all the .Select rows in your code. In Excel automation .Select is almost never necessary. Also you have already referenced the worksheets. If you use those references in determining the range, you won't have to go to the sheets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM