Using Output of CDate function to copy and paste date in another workbook

KiwiGrue

New Member
Joined
Oct 24, 2021
Messages
25
Office Version
  1. 365
Platform
  1. MacOS
I have developed a macro to seek user input on the date of the month for data to be extracted from one worksheet to be pasted to another. Once I have pasted the relevant data to the correct cells/columns I need to provide a date in column A for each new row.

The macro I have is ...

Sub DateInputBox()
Dim dte As Date

mbox = InputBox("Enter date of month ending", "Month oif Data")

If IsDate(mbox) Then
dte = CDate(mbox)

Else
MsgBox "This is not a date! Try again."

End If

Debug.Print dte

End Sub


I have tried various options unsuccessfully to access the date to copy and paste it in the other workbook in column A (this macro is embedded rather than called in the main subroutine with the destination being ... cells(irTarget+2,1).select ...).

Any assistance and pointers would be appreciated.

Cheers
Alistair
 

Attachments

  • 1635316458606.png
    1635316458606.png
    205 bytes · Views: 4

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Your code looks a bit curious to me, especially this part:
VBA Code:
If IsDate(mbox) Then
    dte = CDate(mbox)
Else
    MsgBox "This is not a date! Try again."
End If
The CDate function converts a text value to a date value and the IsDate function checks to see if a value is a date.
If a value is already a date, there is no need to use the CDate function on it.

So I don't think you need that part in your code. I think you can modify your check like this:
VBA Code:
    If Not IsDate(mbox) Then
        MsgBox "This is not a date! Try again."
        Exit Sub
    End If

You also want that "Exit Sub" in there so it quits the macro and doesn't continue on and perform the rest of the code.

However, it seems like you may have other issues/questions about your problem, though they are not that clear from your post.
If that is the case, can you describe these other issues in more detail?
 
Upvote 0
If a value is already a date, there is no need to use the CDate function on it.
True but wrong ! As it may depend on the Regional settings like on my side without CDate function Excel so badly converts the text as a date​
'cause by default VBA understands only text dates as US format !​
 
Upvote 0
True but wrong ! As it may depend on the Regional settings like on my side without CDate function Excel so badly converts the text as a date'cause by default VBA understands only text dates as UK / US format !
Ah, OK. Not a problem for me on this side of the pond.
 
Upvote 0
Ah, OK. Not a problem for me on this side of the pond.
Ah okay thanks guys maybe I was not clear enough ... once the user has input the date via the inputbox how do I 'access' it and paste it into another worksheet range? Joe4 I shall try your suggested code to see if that gives the correct regional date format in New Zealand. Cheers
 
Upvote 0
once the user has input the date via the inputbox how do I 'access' it and paste it into another worksheet range?
If you wanted to put it in cell A10, you would just put the following at the end of the code you currently have:
VBA Code:
Range("A10") = dte
Is that what you were looking for?
 
Upvote 0
Solution
If you wanted to put it in cell A10, you would just put the following at the end of the code you currently have:
VBA Code:
Range("A10") = dte
Is that what you were looking for?
Many thanks it pointed me in the right direction ... I am pasting in another active worksheet and amended code ... Cells(irTarget + 2, 1) = dte ... does the job and is dynamic.

Cheers
Alistair
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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