Select data and paste macro problems

Ed Harris

New Member
Joined
Dec 9, 2017
Messages
49
Office Version
  1. 2010
Platform
  1. Windows
I have tried to adapt this macro from working on an adjacent array to working on a single column. The original macro put the data in a new sheet but I want it in column j
There are no bugs but it is not working even to paste in another sheet. Can someone help please.

VBA Code:
Public Sub Getcolourcont() 'searches for coloured cells and collects the cell address,
    ' Find the last row of data
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    ' Loop through each row
    For r = 2 To FinalRow
        For C = 1 To 1
            ThisValue = Cells(r, C).Interior.Color
            FirstCell = Cells(r, C).Address
            If ThisValue = RGB(0, 255, 255) Then  ' Decide if to copy based on contents of the row
                Cells(r, 1).Resize(1, 6).Copy       ' Copy the cells 1 to 6 to the clipboard
                Sheets("Sheet2").Select
                NextRow = Cells(Rows.Count, 2).End(xlUp).Row + 1 'specify cell to paste at destination
                Cells(NextRow, 1).Value = FirstCell 'paste the cell address
                Cells(NextRow, 2).Select
                ActiveSheet.Paste
                Sheets("Sheet5").Select
            End If
        Next C
    Next r
End Sub

Beam2 2022.xlsm
ABCDEFG
2773.4370544853.511.41000
2873.4376244853.511.41000
2973.4365344853.511.41000
3073.5363344853.511.41000
3173.4359644853.511.41000
3273.4361244853.511.41000
3373.4368144853.511.61000
3473.4378744853.511.51000
Sheet5
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
It is not really clear what you were trying to do, so I just guess.

Here is what my code does:
Loop through column A of Sheet1
If cell color is Cyan (RGB(0, 255,255) then write the cell address in column A of Sheet2 and also copy column A to F to Sheet2 starting from row 2 and so on.

VBA Code:
Public Sub Getcolourcont() 'searches for coloured cells and collects the cell address,

Dim cell As Range, rngColA As Range
Dim r As Long, C As Long, ThisValue As Long, NextRow As Long
Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")

' Define column A range
Set rngColA = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp))

' Loop through each row in column A
For Each cell In rngColA
    ThisValue = cell.Interior.Color
    If ThisValue = RGB(0, 255, 255) Then
        NextRow = ws2.Cells(Rows.Count, "A").End(xlUp).Offset(1).Row
        ws2.Range("A" & NextRow) = cell.Address(0, 0)
        cell.Resize(1, 6).Copy ws2.Range("B" & NextRow)
    End If
Next

End Sub
 
Upvote 0
Yess that works - also I found my error the RGB designation should have been 255,255,0. Many thanks, do you know how to write the data to column J in the source sheet instead of a new sheet?
 
Upvote 0
Yess that works - also I found my error the RGB designation should have been 255,255,0. Many thanks, do you know how to write the data to column J in the source sheet instead of a new sheet?
Try this. Hope this work. About to go to sleep now in my time zone. Got to wake up early to see doctor in morning :)
VBA Code:
Public Sub Getcolourcont() 'searches for coloured cells and collects the cell address,

Dim cell As Range, rngColA As Range
Dim r As Long, C As Long, ThisValue As Long, NextRow As Long
Dim ws1 As Worksheet

Set ws1 = ActiveWorkbook.Sheets("Sheet1")

' Define column A range
Set rngColA = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp))

' Loop through each row in column A
For Each cell In rngColA
    ThisValue = cell.Interior.Color
    If ThisValue = RGB(255, 255, 0) Then
        NextRow = ws1.Cells(Rows.Count, "J").End(xlUp).Offset(1).Row          ' Find next row in column J
        ws1.Range("J" & NextRow) = cell.Address(0, 0)
        cell.Resize(1, 6).Copy ws1.Range("J" & NextRow)
    End If
Next

End Sub
 
Upvote 0
Oopss mistake. I paste copied data on address. Should be
cell.Resize(1, 6).Copy ws1.Range("K" & NextRow)
Here's correction
VBA Code:
Public Sub Getcolourcont() 'searches for coloured cells and collects the cell address,

Dim cell As Range, rngColA As Range
Dim r As Long, C As Long, ThisValue As Long, NextRow As Long
Dim ws1 As Worksheet

Set ws1 = ActiveWorkbook.Sheets("Sheet1")

' Define column A range
Set rngColA = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp))

' Loop through each row in column A
For Each cell In rngColA
    ThisValue = cell.Interior.Color
    If ThisValue = RGB(255, 255, 0) Then
        NextRow = ws1.Cells(Rows.Count, "J").End(xlUp).Offset(1).Row          ' Find next row in column J
        ws1.Range("J" & NextRow) = cell.Address(0, 0)
        cell.Resize(1, 6).Copy ws1.Range("K" & NextRow)
    End If
Next

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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