Copying data to another sheet

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I have 4 sheets that I would like to copy some data from and put it in a sheet called Data, each sheet has approx 1800 rows.

I would like the macro to look at each row and if the value of cells CQ and CU are greater than 0 copy the data from cells CP:DA of that row and paste them onto sheet Data in the next empty row starting from row 2
It would then do this for the next 3 sheets, the sheets are called "22", "25", "26", "Names"

Regards
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi, Damo10
I haven't tested this code but I think it will error at copying in copyFunc function but please let me know

Code:
Sub CopyAll()
    Dim LR1 As Long, LR2 As Long, LR3 As Long, LR4 As Long, count As Long, i As Long
    LR1 = Worksheets("22").Range("A" & Rows.count).End(xlUp).Row
    LR2 = Worksheets("25").Range("A" & Rows.count).End(xlUp).Row
    LR3 = Worksheets("26").Range("A" & Rows.count).End(xlUp).Row
    LR4 = Worksheets("Names").Range("A" & Rows.count).End(xlUp).Row
    
    count = 2
    count = copyFunc("22", LR1, count)
    count = copyFunc("25", LR2, count)
    count = copyFunc("26", LR3, count)
    count = copyFunc("Names", LR4, count)
    
End Sub

Function copyFunc(strWS As String, LR As Long, count As Long) As Long
    For i = 2 To LR
        If Worksheets(strWS).Range("CQ" & i).Value > 0 And Worksheets(strWS).Range("CU" & i).Value > 0 Then
            Worksheets(strWS).Range("CP" & i & ":" & "DA" & i).Copy Worksheets("Data").Range("A" & count)
            count = count + 1
        End If
    Next i
    copyFunc = count
End Function
 
Upvote 0
Hi,

Thanks for reply

I have tested the code and it is filling all the cells on the Data sheet that it copys with =#REF!

Regards
 
Upvote 0
Try
Rich (BB code):
Sub CopyAll()
    Dim LR1 As Long, LR2 As Long, LR3 As Long, LR4 As Long, count As Long, i As Long
    LR1 = Worksheets("22").Range("A" & Rows.count).End(xlUp).Row
    LR2 = Worksheets("25").Range("A" & Rows.count).End(xlUp).Row
    LR3 = Worksheets("26").Range("A" & Rows.count).End(xlUp).Row
    LR4 = Worksheets("Names").Range("A" & Rows.count).End(xlUp).Row
    
    count = 2
    count = copyFunc("22", LR1, count)
    count = copyFunc("25", LR2, count)
    count = copyFunc("26", LR3, count)
    count = copyFunc("Names", LR4, count)
    
End Sub

Function copyFunc(strWS As String, LR As Long, count As Long) As Long
    For i = 2 To LR
        If Worksheets(strWS).Range("CQ" & i).Value > 0 And Worksheets(strWS).Range("CU" & i).Value > 0 Then
            Worksheets(strWS).Range("CP" & i & ":" & "DA" & i).Copy
            Worksheets("Data").Range("A" & count).PasteSpecial xlPasteValues
            count = count + 1
        End If
    Next i
    copyFunc = count
End Function

Just as a verification, did you have formulas in the cells you wanted to copy?
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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