I want to use the date function in an Excel VBA script using a parameter value.

D = "=Date(y,m,d)"

where d, m, y are parameters that are fetched during running the macro.

The problem for me is that I have to maintain the date format for the cell like "dd/mm/yyyy hh:mm:ss".

有帮助吗?

解决方案

In VBA use Dateserial() like:

Sub dural()
    y = 2014
    m = 3
    d = 2
    Range("A1").NumberFormat = "dd/mm/yyyy hh:mm:ss"
    Range("A1").Value = DateSerial(y, m, d)
End Sub

Note the order of parameters!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top