NEWBIE!! HELP w/ pasteing data

BIGTONE559

Active Member
Joined
Apr 20, 2011
Messages
336
Lost as to the proper syntax when trying to paste rows of cells to "J5" and keep a running list

Code:
Sub Help_ME()



Dim cell As Range
Dim dataRange As Range

Set dataRange = Range("G5:G30")


For Each cell In dataRange

If cell.Value = True Then

Range(Cells(cell.Row, 1), Cells(cell.Row, 5)).Copy


msgBox("Blah blah blah")
   
    
Else


End If


Next cell

End Sub
The message box would show me that it was selecting the proper values (and will be removed) however i don't know how to copy those values into Cells Beginning with "J5" i originally used range("J5") but it would overwrite the existing values as it looped.

please help!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Maybe like this

Code:
If cell.Value = True Then
    LR = WorksheetFunction.Max(5, Range("J" & Rows.Count).End(xlUp).Row + 1)
    Range(Cells(cell.Row, 1), Cells(cell.Row, 5)).Copy Destination:=Range("J" & LR)
 
Upvote 0
You were almost there you just need a variable to track the output row and a way to define the output range. This incorporates both.

Code:
Sub Help_ME()

    Dim cell As Range
    Dim dataRange As Range
    Dim DestRow As Long
    
    Set dataRange = Range("G5:G30")
    
    '// Set the first row for the output
    DestRow = 5
    
    For Each cell In dataRange
        If cell.Value = True Then
            Range(Cells(cell.Row, 1), Cells(cell.Row, 5)).Copy _
                Destination:=Cells(DestRow, 10)
            '// Increment the Destination Row
            DestRow = DestRow + 1
        End If
    Next cell
End Sub

This is another method of doing the same thing I think it might be a little faster as the copy and paste operation is only done once, after it has identified the ranges to copy.

Code:
Sub Help_ME2()

    Dim cell As Range
    Dim dataRange As Range
    Dim pasteRng As Range
    Dim cpyRng As Range
    
    Set dataRange = Range("G5:G30")
    Set pasteRng = Range("J5")
    
    For Each cell In dataRange
        If cell.Value = True Then
            '// For first range to be copied
            If cpyRng Is Nothing Then
                '// Set the start of the copy range to first match
                Set cpyRng = Range(Cells(cell.Row, 1), Cells(cell.Row, 5))
            Else
                '// Join additional match ranges to the existing range
                Set cpyRng = Union(cpyRng, _
                                Range(Cells(cell.Row, 1), Cells(cell.Row, 5)))
            End If
        End If
    Next cell
    '// Copy and paste ranges that match criteria
    cpyRng.Copy Destination:=pasteRng
End Sub
 
Upvote 0
I'd like to thank both of you for helping me out. I had problems understanding the syntax after using the .Copy

Appreciate all the help
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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