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.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I have used macro recorder and got this following code
VBA Code:
Sub CopyPasteData()
    
    Range("A1048575").Select
    Selection.End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Select
    Range("A7190:K7335").Select
    Range("A7335").Activate
    Range(Selection, Cells(1)).Select
    Range("A1:K7335").Select
    Range("A7335").Activate
    Selection.Copy
    Sheets("Pending").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    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").Select
    Selection.Style = "Comma"
    Columns("K:K").Select
    Selection.NumberFormat = "m/d/yyyy"
    
    Range("A1").Select
        
    Application.CutCopyMode = False
End Sub


I am unable to record a macro for deleting the rows in Sheet Pending where there is a date value in column K.

Please can someone help me clean this code and kindly add the above task.

Thanks in advance
 
Upvote 0
When you describe the requirement you need to give the real sheet names. Your text says
Sheet2 manually in Sheet7
Your code says Sheet7 is called Pending. If that is the case what is Sheet2 called.

Do you we clear the Pending Sheet each time the code runs and then from Sheet2 copy all the rows with column K = blank ?
And can we ignore the reference to Sheet2 being appended to and that you always want all unpaid rows from that sheet ?
 
Upvote 0
Thanks for your time and reply @Alex Blakenburg

Sorry for the confusion

My main data sheet is "Data Inv"
I am want to copy data in the sheet named "Pending"

I want to clear all the data in the "Pending" sheet when the macro runs.
I want to copy all the rows from the "Data Inv" sheet where column K is blank ( is not paid)

Sorry for any miscommunication on my end in the previous posts.
 
Upvote 0
See if this does what you need.
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).EntireColumn.Insert
        Set rngCrit = .Cells(srcRowFirst, srcColLast + 2).Resize(2, 1)
    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 = "="
    
    ' Apply Filter and Copy to Destination
    rngSrc.AdvancedFilter xlFilterCopy, rngCrit, rngDest
    rngCrit.EntireColumn.Delete
    
    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
Thanks, @Alex Blakenburg for your time.

When I am running the macro it is only copying the header row from the sheet "Data Inv" to the "Pending" sheet.
 
Upvote 0
Then your column K is not being recognized as being blank. Please provide an XL2BB sample of your data ?
Is there a formula in column K, if yes at a minimum I need the formula.
 
Upvote 0
Thanks @Alex Blakenburg for your time

I have provided the XL2BB for both sheets.
In column K when the invoice is paid I write the date its paid like 11/12/2015 (dd/mm/yyyy)

For Data Inv Sheet
Pending Summary Testing1.xlsm
ABCDEFGHIJK
1POINVOICE NOCONTRACTSCHOOLDAYSRATEG TotalVATN TotalPERIOD
21001ABC-FEB15-001AA212WWNFKP1£60.00£60.00£12.00£72.0001st February 2015 - 28th February 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 201511/12/2015
71001ABC-FEB15-005EE212AGNMLJ1£39.75£39.75£7.95£47.7001st February 2015 - 28th February 201511/12/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
121001ABC-FEB15-010JJ212OCQPBP1£78.49£78.49£0.00£78.4901st February 2015 - 28th February 2015
131001ABC-FEB15-011KK212YDBZOG1£108.00£108.00£21.60£129.6001st February 2015 - 28th February 2015
141001ABC-FEB15-012LL212EVSHVD1£89.00£89.00£0.00£89.0001st February 2015 - 28th February 2015
151001ABC-FEB15-012LL212HMFAEK1£89.00£89.00£0.00£89.0001st February 2015 - 28th Febraury 201511/12/2015
161001ABC-FEB15-013MM212RDVBQO1£120.00£120.00£0.00£120.0001st February 2015 - 28th February 201511/12/2015
171001ABC-FEB15-013MM212FYCZPP1£120.00£120.00£0.00£120.0001st February 2015 - 28th Febraury 201511/12/2015
181001ABC-FEB15-014NN212BWJFLR1£58.00£58.00£0.00£58.0001st February 2015 - 28th February 201511/12/2015
191001ABC-FEB15-015OO212HJYCAR1£85.00£85.00£0.00£85.0001st February 2015 - 28th February 201511/12/2015
201001ABC-FEB15-016PP212FJYEKV1£78.00£78.00£0.00£78.0001st February 2015 - 28th February 201511/12/2015
211001ABC-FEB15-016PP213YWUBKL1£160.00£160.00£0.00£160.0001st February 2015 - 28th February 201511/12/2015
221001ABC-FEB15-016PP212ZJWDTG1£145.00£145.00£0.00£145.0001st February 2015 - 28th February 201511/12/2015
231001ABC-FEB15-017QQ212CAGRZP1£79.00£79.00£0.00£79.0001st February 2015 - 28th February 201511/12/2015
241001ABC-FEB15-018RR212CKPOPX1£89.00£89.00£0.00£89.0001st February 2015 - 28th February 2015
251001ABC-FEB15-018RR212OBTKVQ1£180.00£180.00£0.00£180.0001st February 2015 - 28th February 2015
261001ABC-FEB15-018RR212RLXQVO1£160.00£160.00£0.00£160.0001st February 2015 - 28th February 2015
271001ABC-FEB15-018RR212DIAQJA1£160.00£160.00£0.00£160.0001st February 2015 - 28th February 2015
281001ABC-FEB15-018RR212SACTYF1£160.00£160.00£0.00£160.0001st February 2015 - 28th February 2015
291001ABC-FEB15-019SS212IVKFEB1£115.00£115.00£0.00£115.0001st February 2015 - 28th February 2015
301001ABC-FEB15-020TT212FKTQTD1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
311001ABC-FEB15-020TT212HJRXYN1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
321001ABC-FEB15-020TT212MSFXJS1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
331001ABC-FEB15-020TT212CJQNNJ1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
341001ABC-FEB15-020TT212UTPZNX1£186.00£186.00£0.00£186.0001st February 2015 - 28th February 2015
351001ABC-FEB15-021UU212HQYYPP1£100.00£100.00£0.00£100.0001st February 2015 - 28th February 2015
36
Data Inv
Cell Formulas
RangeFormula
G2:G35G2=F2*E2
H2:H10,H13H2=G2*20%
I2:I35I2=G2+H2
H11:H12,H14:H35H11=G11*0%
F10F10=24.95*2



For Pending Sheet
Pending Summary Testing1.xlsm
ABCDEFGHIJK
1POINVOICE NOCONTRACTSCHOOLDAYS RATE G Total VAT N Total PERIOD
2
3
4
5
6
Pending
 
Upvote 0
Any reason your column K doesn't seem to have a heading ? If you give it a heading it will work fine.
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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