VBA code to copy data from sheet to another

mazher

Active Member
Joined
Nov 26, 2003
Messages
359
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Dear Forum Members,
Please can someone help me with the following;

I Sheet2 I have invoices data (containing formulas) starting from
B2:K7000

Every month it keeps on increasing downward.


B1:K1 are column headings

Column K is the payment date

Currently, I copy the data of Sheet2 manually in Sheet7 and then delete the rows that have been paid in column K, to get an upto date list of the pending invoices.

I am pasting the data using paste special to remove the formulas.

Please can someone help me to automate this task?

Thanks in advance.
 
Thank you very much @Alex Blakenburg

I started working, :)

I didn't realise that missing column heading can be a problem on my side.


I will be extremely thankful to you if you can please add these two conditions to the code for filtering the data so that the code does not copy the data from the "Data Inv" Sheet where the
Values in Column I (N Total are zero or empty)
Cells in Column B (Invoice No) are empty

Thanks in advance.
 
Last edited:
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Give this a try:

VBA Code:
Sub CopyPasteUnpaid()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim rngSrc As Range, rngDest As Range, rngCrit As Range
    Dim srcRowFirst As Long, srcRowLast As Long, srcColLast As Long
       
    Set srcSht = Worksheets("Data Inv")
    Set destSht = Worksheets("Pending")
    srcRowFirst = 1
        
    With srcSht
        srcRowLast = srcSht.Cells(Rows.Count, "A").End(xlUp).Row
        srcColLast = srcSht.Cells(srcRowFirst, Columns.Count).End(xlToLeft).Column
        Set rngSrc = .Range(.Cells(srcRowFirst, "A"), .Cells(srcRowLast, srcColLast))
        ' Temporary range for criteria
        .Columns(srcColLast + 2).Resize(, 4).EntireColumn.Insert
        Set rngCrit = .Cells(srcRowFirst, srcColLast + 2).Resize(2, 4)
    End With
    
    With destSht
        .Cells.Clear
        Set rngDest = .Range("A1")
    End With
    
    ' Set Criteria
    rngCrit.Cells(1, 1).Value = srcSht.Range("K" & srcRowFirst).Value
    rngCrit.Cells(2, 1).Value = "="
    rngCrit.Cells(1, 2).Value = srcSht.Range("B" & srcRowFirst).Value
    rngCrit.Cells(2, 2).Value = "<>"
    rngCrit.Cells(1, 3).Value = srcSht.Range("I" & srcRowFirst).Value
    rngCrit.Cells(2, 3).Value = "<>"
    rngCrit.Cells(1, 4).Value = srcSht.Range("I" & srcRowFirst).Value
    rngCrit.Cells(2, 4).Value = "<>0"
    
    ' Apply Filter and Copy to Destination
    rngSrc.AdvancedFilter xlFilterCopy, rngCrit, rngDest
    rngCrit.EntireColumn.Delete
    
    ' Set rngDest = rngDest.CurrentRegion
    ' rngDest.Columns.AutoFit               ' Use OPs setting instead
    
    With destSht
        .Columns("B:C").EntireColumn.AutoFit
        .Columns("E:K").EntireColumn.AutoFit
        .Columns("D:D").ColumnWidth = 49.57
        .Range("F:F,G:G,H:H,I:I").Style = "Comma"   ' If omitted will default to format on source sheet
        .Columns("K:K").NumberFormat = "m/d/yyyy"   ' If omitted will default to format on source sheet
    End With
    
End Sub
 
Upvote 0
Extremely thankful to you @Alex Blankenburg for all your time, and help and for providing me with the solution to my problem.
 
Upvote 0
Sorry for bothering you again @Alex Blakenburg

I just realized that in the Pending sheet the code is still copying the rows where the cell value in Column I are text.
 
Upvote 0
I have logged off for the night can you send an XL2BB with examples and I will have a look tomorrow.
 
Upvote 0
Hi @Alex Blakenburg ,

Thanks for bothering you again and again.

I have pasted the code

Pending Summary Testing1.xlsm
ABCDEFGHIJK
1POINVOICE NOCONTRACTSCHOOLDAYSRATEG TotalVATN TotalPERIODDate
21001ABC-FEB15-001AA212WWNFKP1£60.00£60.00£12.00£72.0001st February 2015 - 28th February 201511/12/2015
31001ABC-FEB15-002BB212VRSILB1£98.00£98.00£19.60£117.6001st February 2015 - 28th February 201511/12/2015
41001ABC-FEB15-003CC212GHHQDH1£85.55£85.55£17.11£102.6601st February 2015 - 28th February 201511/12/2015
51001ABC-FEB15-003CC212KCKCSS1£65.95£65.95£13.19£79.1401st February 2015 - 28th Febraury 201511/12/2015
61001ABC-FEB15-004DD212AGEJFR1£82.00£82.00£16.40£98.4001st February 2015 - 28th February 2015
71001ABC-FEB15-005EE212AGNMLJ1£39.75£39.75£7.95£47.7001st February 2015 - 28th February 2015
81001ABC-FEB15-006FF212MYQGPU1£69.75£69.75£13.95£83.7001st February 2015 - 28th February 2015
91001ABC-FEB15-007GG212RRMCJL1£64.30£64.30£12.86£77.1601st February 2015 - 28th February 2015
101001ABC-FEB15-008HH212VFIOWT1£49.90£49.90£9.98£59.8801st February 2015 - 28th February 201511/12/2015
111001ABC-FEB15-009II212RQADLX1£114.00£114.00£0.00£114.0001st February 2015 - 28th February 2015
12POINVOICE NOCONTRACTSCHOOLDAYSRATEG TotalVATN TotalPERIOD
131001ABC-FEB15-010JJ212OCQPBP1£78.49£78.49£0.00£78.4901st February 2015 - 28th February 2015
141001ABC-FEB15-011KK212YDBZOG1£108.00£108.00£21.60£129.6001st February 2015 - 28th February 2015
151001ABC-FEB15-012LL212EVSHVD1£89.00£89.00£0.00£89.0001st February 2015 - 28th February 2015
161001ABC-FEB15-012LL212HMFAEK1£89.00£89.00£0.00£89.0001st February 2015 - 28th Febraury 201511/12/2015
171001ABC-FEB15-013MM212RDVBQO1£120.00£120.00£0.00£120.0001st February 2015 - 28th February 201511/12/2015
181001ABC-FEB15-013MM212FYCZPP1£120.00£120.00£0.00£120.0001st February 2015 - 28th Febraury 201511/12/2015
191001ABC-FEB15-014NN212BWJFLR1£58.00£58.00£0.00£58.0001st February 2015 - 28th February 201511/12/2015
201001ABC-FEB15-015OO212HJYCAR1£85.00£85.00£0.00£85.0001st February 2015 - 28th February 201511/12/2015
211001ABC-FEB15-016PP212FJYEKV1£78.00£78.00£0.00£78.0001st February 2015 - 28th February 201511/12/2015
221001ABC-FEB15-016PP213YWUBKL1£160.00£160.00£0.00£160.0001st February 2015 - 28th February 201511/12/2015
231001ABC-FEB15-016PP212ZJWDTG1£145.00£145.00£0.00£145.0001st February 2015 - 28th February 201511/12/2015
241001ABC-FEB15-017QQ212CAGRZP1£79.00£79.00£0.00£79.0001st February 2015 - 28th February 201511/12/2015
251001ABC-FEB15-018RR212DIAQJA1£160.00£160.00£0.00£160.0001st February 2015 - 28th February 2015
26POINVOICE NOCONTRACTSCHOOLDAYSRATEG TotalVATN TotalPERIOD
271001ABC-FEB15-018RR212SACTYF1£160.00£160.00£0.00£160.0001st February 2015 - 28th February 2015
281001ABC-FEB15-019SS212IVKFEB1£115.00£115.00£0.00£115.0001st February 2015 - 28th February 201511/12/2015
291001ABC-FEB15-020TT212FKTQTD1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 201511/12/2015
301001ABC-FEB15-020TT212HJRXYN1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
311001ABC-FEB15-020TT212MSFXJS1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
321001ABC-FEB15-020TT212CJQNNJ1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
331001ABC-FEB15-020TT212UTPZNX1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
341001ABC-FEB15-021UU212HQYYPP1£100.00£100.00£0.00£100.0001st February 2015 - 28th February 2015
35POINVOICE NOCONTRACTSCHOOLDAYSRATEG TotalVATN TotalPERIOD
361001ABC-FEB15-022TT212MSFXJS1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
371001ABC-FEB15-023TT212CJQNNJ1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
381001ABC-FEB15-024TT212CJQNNJ1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
391001ABC-FEB15-025TT212UTPZNX1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 201511/12/2015
401001ABC-FEB15-026UU212HQYYPP1£100.00£100.00£0.00£100.0001st February 2015 - 28th February 201511/12/2015
Data Inv
Cell Formulas
RangeFormula
G2:G11,G36:G40,G27:G34,G13:G25G2=F2*E2
H2:H10,H14H2=G2*20%
I2:I11,I36:I40,I27:I34,I13:I25I2=G2+H2
H11,H36:H40,H27:H34,H13,H15:H25H11=G11*0%
F10F10=24.95*2



I dont need these rows marked in yellow in the "Pending" Sheet

Pending Summary Testing1.xlsm
ABCDEFGHIJK
1POINVOICE NOCONTRACTSCHOOLDAYS RATE G Total VAT N Total PERIODDate
21001ABC-FEB15-004DD212AGEJFR182.0082.0016.4098.4001st February 2015 - 28th February 2015
31001ABC-FEB15-005EE212AGNMLJ139.7539.757.9547.7001st February 2015 - 28th February 2015
41001ABC-FEB15-006FF212MYQGPU169.7569.7513.9583.7001st February 2015 - 28th February 2015
51001ABC-FEB15-007GG212RRMCJL164.3064.3012.8677.1601st February 2015 - 28th February 2015
61001ABC-FEB15-009II212RQADLX1114.00114.00-114.0001st February 2015 - 28th February 2015
7POINVOICE NOCONTRACTSCHOOLDAYS RATE G Total VAT N Total PERIOD
81001ABC-FEB15-010JJ212OCQPBP178.4978.49-78.4901st February 2015 - 28th February 2015
91001ABC-FEB15-011KK212YDBZOG1108.00108.0021.60129.6001st February 2015 - 28th February 2015
101001ABC-FEB15-012LL212EVSHVD189.0089.00-89.0001st February 2015 - 28th February 2015
111001ABC-FEB15-018RR212DIAQJA1160.00160.00-160.0001st February 2015 - 28th February 2015
12POINVOICE NOCONTRACTSCHOOLDAYS RATE G Total VAT N Total PERIOD
131001ABC-FEB15-018RR212SACTYF1160.00160.00-160.0001st February 2015 - 28th February 2015
141001ABC-FEB15-020TT212HJRXYN1186.00186.00-186.0001st February 2015 - 28th February 2015
151001ABC-FEB15-020TT212MSFXJS1186.00186.00-186.0001st February 2015 - 28th February 2015
161001ABC-FEB15-020TT212CJQNNJ1186.00186.00-186.0001st February 2015 - 28th February 2015
171001ABC-FEB15-020TT212UTPZNX1186.00186.00-186.0001st February 2015 - 28th February 2015
181001ABC-FEB15-021UU212HQYYPP1100.00100.00-100.0001st February 2015 - 28th February 2015
19POINVOICE NOCONTRACTSCHOOLDAYS RATE G Total VAT N Total PERIOD
201001ABC-FEB15-022TT212MSFXJS1186.00186.00-186.0001st February 2015 - 28th February 2015
211001ABC-FEB15-023TT212CJQNNJ1186.00186.00-186.0001st February 2015 - 28th February 2015
221001ABC-FEB15-024TT212CJQNNJ1186.00186.00-186.0001st February 2015 - 28th February 2015
Pending


I don't want to copy Values in Column I (N Total are zero or empty or Text)

Thanks again for all your help.
 
Upvote 0
I can do a generic text exclusion for Column N but it will be more complicated than just excluding the additional heading eg Invoice No column <>"INVOICE NO".
Is it likely that the only text values in Column N are going to be headings repeating ?
 
Upvote 0
OK Additional criteria added.

VBA Code:
Sub CopyPasteUnpaid()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim rngSrc As Range, rngDest As Range, rngCrit As Range
    Dim srcRowFirst As Long, srcRowLast As Long, srcColLast As Long
       
    Set srcSht = Worksheets("Data Inv")
    Set destSht = Worksheets("Pending")
    srcRowFirst = 1
        
    With srcSht
        srcRowLast = srcSht.Cells(Rows.Count, "A").End(xlUp).Row
        srcColLast = srcSht.Cells(srcRowFirst, Columns.Count).End(xlToLeft).Column
        Set rngSrc = .Range(.Cells(srcRowFirst, "A"), .Cells(srcRowLast, srcColLast))
        ' Temporary range for criteria
        .Columns(srcColLast + 2).Resize(, 5).EntireColumn.Insert
        Set rngCrit = .Cells(srcRowFirst, srcColLast + 2).Resize(2, 5)
    End With
    
    With destSht
        .Cells.Clear
        Set rngDest = .Range("A1")
    End With
    
    ' Set Criteria
    rngCrit.Cells(1, 1).Value = srcSht.Range("K" & srcRowFirst).Value
    rngCrit.Cells(2, 1).Value = "="
    rngCrit.Cells(1, 2).Value = srcSht.Range("B" & srcRowFirst).Value
    rngCrit.Cells(2, 2).Value = "<>"
    rngCrit.Cells(1, 3).Value = srcSht.Range("B" & srcRowFirst).Value
    rngCrit.Cells(2, 3).Value = "<>INVOICE NO"
    rngCrit.Cells(1, 4).Value = srcSht.Range("I" & srcRowFirst).Value
    rngCrit.Cells(2, 4).Value = "<>"
    rngCrit.Cells(1, 5).Value = srcSht.Range("I" & srcRowFirst).Value
    rngCrit.Cells(2, 5).Value = "<>0"
    
    ' Apply Filter and Copy to Destination
    rngSrc.AdvancedFilter xlFilterCopy, rngCrit, rngDest
    rngCrit.EntireColumn.Delete
    
    ' Set rngDest = rngDest.CurrentRegion
    ' rngDest.Columns.AutoFit               ' Use OPs setting instead
    
    With destSht
        .Columns("B:C").EntireColumn.AutoFit
        .Columns("E:K").EntireColumn.AutoFit
        .Columns("D:D").ColumnWidth = 49.57
        .Range("F:F,G:G,H:H,I:I").Style = "Comma"   ' If omitted will default to format on source sheet
        .Columns("K:K").NumberFormat = "m/d/yyyy"   ' If omitted will default to format on source sheet
    End With
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

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