Copy Data on sheet6 based on yellow colour

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have data that is in yellow in Col D on sheet6

Where this is the then the item in Col A:D where there are yellow items are to be copied into A2 onwards

I have written code to do this, except that it is copying this after the last row containing data


I need this amended as I need this copied in row 2 onwards.


Kindly amend my code so it copies the data into row2 onwards and not after the last row containing data

Your assistance is most appreciated

Code:
 Sub CopyCOSYellowItems()

Application.ScreenUpdating = False
  

  Sheets(7).Select
  Range("A2").Select
  
  
  Dim wks As Worksheet
  Dim wNew As Worksheet
  Dim lRow As Long
  Dim x As Long
  Dim j As Long
j = 1
Set wks = Sheets(6)
  lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
  Set wNew = Sheets(7)
For x = 2 To lRow
  If wks.Cells(x, 4).Interior.Color = vbYellow Then
      wks.Range("A" & x & ":D" & x).Copy
      wNew.Range("D" & Rows.Count).End(xlUp).Offset(j, -3).PasteSpecial Paste:=xlPasteValues
     ' j = 1
  End If
Next
  
  
  
  Application.CutCopyMode = False
 ' Sheets(7).Select
  'If Range("A1") = "Branch" Then
  'End If
  'Exit Sub
  
  
  
    wks.Range("a1:D1").Copy
     wNew.Range("A1").PasteSpecial Paste:=xlPasteValues
      
  Application.CutCopyMode = False

  Application.ScreenUpdating = True

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hello,

does this work as expected?

Code:
Sub CopyCOSYellowItems()
    Application.ScreenUpdating = False
    Sheets(7).Select
    Range("A2").Select
    Dim wks As Worksheet
    Dim wNew As Worksheet
    Dim lRow As Long
    Dim x As Long
    Dim j As Long
    j = 1
    Set wks = Sheets(6)
    lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
    Set wNew = Sheets(7)
    MY_COUNT = 2
    For x = 2 To lRow
        If wks.Cells(x, 4).Interior.Color = vbYellow Then
            wks.Range("A" & x & ":D" & x).Copy
            wNew.Range("D" & MY_COUNT).PasteSpecial Paste:=xlPasteValues
            MY_COUNT = MY_COUNT + 1
         ' j = 1
        End If
    Next
    Application.CutCopyMode = False
    ' Sheets(7).Select
    'If Range("A1") = "Branch" Then
    'End If
    'Exit Sub
    wks.Range("a1:D1").Copy
    wNew.Range("A1").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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