Copy and Paste value if date matches

Aunty Jean

Board Regular
Joined
Apr 6, 2005
Messages
55
Sorry, not good at VBA, haven't done it for years now. Have searched threads and tried adapting but nothing working.

Have a workbook with first tab containing a template, the second tab is where data is entered for today and the sum of the items is carried to a third tab for that month
The month sheet has a date for every day in one column and captures the Total value for today in the adjacent column by comparing date (=IF(A2='Stock Check'!$A$2,'Stock Check'!$L$17,0))

Obviously this is blank when the date changes and therefore I need to capture the value for that date in say, column C to retain it's value when date changes.

Have managed to run a macro to copy values from column B to C and retain that value if changed in B but, I only need it to activate on each cell in C when it is today's date and save that value without it being overridden.

The macro would need to be able to be run for each month (separate tabs) and take into consideration some months have less days than others. I was thinking of active cell but got into a mess and looked like I would have to have a macro for each individual date......... So my idea was along the lines of a formula, IF(A2='Stock Check'!$A$2, copy B2 and paste value to C2, but only if B2 empty.. Aware that this can't be done in a formula, just wanted to give an idea of the logic.

Any suggestions welcome
 

Attachments

  • Stock Sheet.JPG
    Stock Sheet.JPG
    42.4 KB · Views: 76
Hello Jean,

The images below show exactly how the "Test File" should look, and where the values are pasted after the macro runs.

Sorry it is late - I posted it in the wrong thread. :)

Jamie
Tried recreating from your images and this is what I get. It only copies one number across and hangs on the selected on in cell B. Why does it not copy all across if C empty?
 

Attachments

  • image 1.JPG
    image 1.JPG
    58.5 KB · Views: 7
  • Image 2.JPG
    Image 2.JPG
    156.5 KB · Views: 7
  • image 3.JPG
    image 3.JPG
    145.5 KB · Views: 7
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
Hello Jean,

Your workbook looks fine

I created a new workbook, and it worked fine.

The only thing I can see from your images is ay cell A17. The £23.00. (I don't know if it is a textbox, or what is under it.) But that shouldn't effect it, as it only loops to row 16 - Z1 value.

Try changing the Z1 values to 10 (I can't see that changing anything, but worth a try.)

I can't see anything that is wrong, If it does Jan, it should do them all. (I guess you are a little bit nearer. :) )

I will have a think about it more.

Jamie
 
Upvote 0
Hello Jean,

Try making all the data match in every month, copy and paste it, with Z1 value 10.

Then if that works change some of the data, and delete the data in all the column Cs.

Jamie
 
Upvote 0
Hello Jean,

I am kind of at a loss, but I have one idea. On the 'EachMon' Macro, us the apostrophe at the start of a line (It turns it into a comment: Excel ignores comments).

Like below:-


VBA Code:
Sub EachMon()
Application.ScreenUpdating = False 'Turns off screen updating - runs faster

Sheets("Jan").Select 'selects each tab in turn and then runs the above code
Call PasteifEmpty

'Sheets("Feb").Select
'Call PasteifEmpty

'Sheets("Mar").Select
'Call PasteifEmpty

'Sheets("April").Select
'Call PasteifEmpty

Sheets("May").Select
Call PasteifEmpty

Sheets("Stock Check").Select 'selects the original sheet - change the name to fit
Application.ScreenUpdating = True 'Turns on screen updating again

Application.CutCopyMode = False
End Sub

Try ignoring Feb first and see if it stops running on Mar.
Then try ignoring all except Jan. See if it finishes and and selects Stock Check. (If it does, try it for only Feb)
Then try different combinations to see if you can see a pattern of where it stops.

The only thing different between my sheet and yours, that I can see, is the £23.00.

But if it does one iteration, then it should do them: they are all the same???

Let me know how you get on. :)

Jamie
 
Upvote 0
Hello Jean,

The images below show exactly how the "Test File" should look, and where the values are pasted after the macro runs.

Sorry it is late - I posted it in the wrong thread. :)

Jamie
Just tried running the macro on the original workbook and put a value in Z1 on Stock Check worksheet and found after running macro the cursor moves to cell C31 (Z1 value - 1) in the Stock Check sheet and not on the month sheet. Moved the macro to Jan tab and it jumps back to the Stock Check worksheet after running?
 
Upvote 0
Hello Jean,

The images below show exactly how the "Test File" should look, and where the values are pasted after the macro runs.

Sorry it is late - I posted it in the wrong thread. :)

Jamie
Just tried running the macro again using date 01/01/2022 in the Stock Sheet sheet and it does indeed copy the value to cell C2 in the Jan tab/sheet but, if I change the date in Stock Sheet to 02/01/2022 it does not copy the value to C3 in the Jan tab/sheet......
 
Upvote 0
Just tried running the macro on the original workbook and put a value in Z1 on Stock Check worksheet and found after running macro the cursor moves to cell C31 (Z1 value - 1) in the Stock Check sheet and not on the month sheet. Moved the macro to Jan tab and it jumps back to the Stock Check worksheet after running?
ignore this message, cannot delete it
 
Upvote 0
Just tried running the macro again using date 01/01/2022 in the Stock Sheet sheet and it does indeed copy the value to cell C2 in the Jan tab/sheet but, if I change the date in Stock Sheet to 02/01/2022 it does not copy the value to C3 in the Jan tab/sheet......
OK getting there - changed i to 2 here If Cells(i, 1).Value = Sheets("Stock Check").Cells(2, 1). and it now works in Jan tab for 01/01/2022, 02/01/2022 etc. but not for Feb Mar Apr.......
And just found Z1 field in Feb tab was in Y1!!!! moved it to Z1 and all working perfectly!!!!! Thank you so much.
 
Last edited:
Upvote 0
Hello Jean,

Excellent. :) It took a long time getting there. Now I can have a rest. :)

But the next code you use you will be able to read it and follow it better. :)

Don't forget to mark your question as answered, on the right hand side off the best answer. Or the question will remain open. :)

Jamie
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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