Copy Records

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
780
Office Version
  1. 365
Hi,

can i please get help modifying the below code to:

1. copy only whole rows but just upto certain column (for example my spreadsheet is from A:J, but only want to copy upto column F)
2. Once records are copy delete records from source sheet
3. copy values and no formulas

code:

VBA Code:
Sub COPY_PAIDINVOICES()
On Error Resume Next


    Dim srchtrm As String
    Dim rng As Range, destRow As Long
    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim c As Range
    Dim i As Integer
    Dim Today As Date
    
    With Application
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    
    Set shtSrc = Sheets("CDA_INVOICES")    'source sheet
    Set shtDest = Sheets("PAID_INVOICES")    'destination sheet
    destRow = 2 'start copying to this row


    'don't scan the entire column...
    Set rng = Application.Intersect(shtSrc.Range("H:H"), shtSrc.UsedRange)


    For Each c In rng.Cells
        If c.Value = "PAID IN FULL" Then
            
            c.EntireRow.Copy shtDest.Cells(destRow, 1)
          
            destRow = destRow + 1


        End If
    Next
    
    With Application
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
    End With
    
    Application.CutCopyMode = False
    
    Sheets("CDA_INVOICES").Range("H14").Select
End Sub

thanks.
tableinv.jpg
tableinv.jpg
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
To copy just columns A:F, try changing this row:
VBA Code:
c.EntireRow.Copy shtDest.Cells(destRow, 1)
to this:
VBA Code:
Range(Cells(c.Row, "A"), Cells(c.Row, "F")).Copy shtDest.Cells(destRow, 1)
and then to delete the source row afterwards, you can use a line like this.
VBA Code:
c.EntireRow.Delete

However, you may have problem if you have consecutive rows that need to be copied/moved, as deleting a row moves all the rows below that up.
This might cause the 2nd in consecutive rows to be missed.

If that is the case, then instead of deleting the row, just clear the contents, i.e.
VBA Code:
c.EntireRow.ClearContents
You would then have blank rows that you can delete or sort out after the end of the loop.
 
Upvote 0
thank so much worked.

only problem having is for due date column and paid amounts is coying formulas instead of values (so showing #VALUE! error) any help on that.
 
Upvote 0
Try this:
VBA Code:
Range(Cells(c.Row, "A"), Cells(c.Row, "F")).Copy 
shtDest.Cells(destRow, 1).PasteSpecial xlPasteValues
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

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