Copy Paste a range instead of row

st1

New Member
Joined
Jul 29, 2009
Messages
25
Hi Everyone
I have a macro i have been using for years no problem.
But i would like change it to copy a range of cells instead of whole row.
I have been tying to change it to no success
Please help!

Code:
Dim LSearchRow As Integer
    Dim LCopyToRow As Integer


    On Error GoTo Err_Execute


    Sheets("SHOP LOAD").Select


    'Start search in row 7
    LSearchRow = 7
    
    Sheets("SHOP LOAD").Select
    'Start copying data to row 2 in Sheet2 (row counter variable)
    LCopyToRow = 4


    While Len(Range("A" & CStr(LSearchRow)).Value) > 0


        'If value in column H = "COMPLETE", copy entire row to April18
        If Range("I" & CStr(LSearchRow)).Value = "COMPLETE" Then


            'Select row in Sheet to copy
            Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
            Selection.Copy


            'Paste row into April18 in next row
            Sheets("April18").Select
            LCopyToRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).PasteSpecial xlPasteValues


            'Move counter to next row
            LCopyToRow = LCopyToRow + 1


            'Go back to Sheet2 to continue searching
            Sheets("SHOP LOAD").Select


        End If

           LSearchRow = LSearchRow + 1
 

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.
Hope this helps.

Code:
Dim LSearchRow As Integer, LC As Long
Dim LCopyToRow As Integer
Dim wsS As Worksheet, wsA As Worksheet
Set wsS = Sheets("SHOP LOAD")
Set wsA = Sheets("April18")
With wsS
    On Error GoTo Err_Execute
    'Start search in row 7
    LSearchRow = 7
    LCopyToRow = 4
    While Len(.cells(LSearchRow, 1).Value) > 0
        'If value in column H = "COMPLETE", copy entire row to April18
        If .cells(LSearchRow, 9).Value = "COMPLETE" Then
            LC = .cells(LSearchRow, Columns.count).End(xlToLeft).column
            LCopyToRow = wsA.cells(Rows.count, 1).End(xlUp).Row + 1
            'Select row in Sheet to copy
            .Rows(LSearchRow).copy
            wsA.Range(.cells(LCopyToRow, 1), .cells(LCopyToRow, LC)).Value = .Range(.cells(LSearchRow, 1), .cells(LSearchRow, LC)).Value
            'Move counter to next row
            LSearchRow = LSearchRow + 1
        End If
End With
 
Last edited:
Upvote 0
Hi
I tried the suggested but got some errors i tried to modify but still get error.
Please see my modified code.
Code:
' onemacro4 Macro
'
Dim LSearchRow As Integer, LC As Long
Dim LCopyToRow As Integer
Dim wsS As Worksheet, wsA As Worksheet
Set wsS = Sheets("SHOP LOAD")
Set wsA = Sheets("April18")


With wsS
    On Error GoTo Err_Execute
    'Start search in row 7
    LSearchRow = 7
    LCopyToRow = 4
    
    While Len(.Cells(LSearchRow, 1).Value) > 0
        'If value in column H = "COMPLETE", copy entire row to April18
        If .Cells(LSearchRow, 9).Value = "COMPLETE" Then
            LC = .Cells(LSearchRow, Columns.Count).End(xlToLeft).Column
            LCopyToRow = wsA.Cells(Rows.Count, 1).End(xlUp).Row + 1
            'Select row in Sheet to copy
            .Rows(LSearchRow).Copy
            wsA.Range(.Cells(LCopyToRow, 1), .Cells(LCopyToRow, LC)).Value = .Range(.Cells(LSearchRow, 1), .Cells(LSearchRow, LC)).Value
           LSearchRow = LSearchRow + 1
        End If
 
            
Wend
End With


Exit Sub


Err_Execute:
    MsgBox "An error occurred."




End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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