简体   繁体   English

将此较长的Excel日期公式转换为VBA

[英]Convert this long Excel date formula into VBA

I have this Excel formula below; 我下面有这个Excel公式;

DATE(LEFT(A1,3),MID(A1,6,1),MID(A1,7,1))

I would like to convert this Excel formula into a VBA user-defined function. 我想将此Excel公式转换为VBA用户定义的函数。

Public Function getDate(input_date As String)
    'code to convert DATE(LEFT(A1,3),MID(A1,6,1),MID(A1,7,1)) to VBA
End Function

This is tricky because there are functions used inside function parameters. 这很棘手,因为在函数参数内部使用了一些函数。 Like LEFT used inside DATE. 就像DATE中使用的LEFT。

EDIT: If possible, I would like to use the Excel formula directly inside the VBA function with minimal modification. 编辑:如果可能的话,我想在VBA函数内部直接使用Excel公式,而只需进行最小的修改。 Is it possible to do this in Excel VBA? 是否可以在Excel VBA中执行此操作?

It's not really any different to what you are doing in Excel, at least as far as calling functions within functions: 它与您在Excel中所做的操作实际上没有任何区别,至少就在函数内调用函数而言:

Public Function getDate(input_date As String) As Date
    getDate = DateSerial(1900 + CInt(Left(input_date, 3)), _
                         CInt(Mid(input_date, 6, 1)), _
                         Cint(Mid(input_date, 7, 1)))
End Function

The CInt calls aren't strictly necessary, VBA will coerce the expressions returned from Left and Mid to numeric values, just as Excel does - but I like to explicitly show the conversions in these sort of statements. 不一定严格要求CInt调用,就像Excel一样,VBA会将从LeftMid返回的表达式强制转换为数值-但我想在这类语句中明确显示转换。 But if you don't want them, you could use the following, which is basically identical to the Excel formula except for DateSerial instead of DATE . 但是,如果您不希望使用它们,则可以使用以下命令,该命令与Excel公式基本相同,除了DateSerial而不是DATE (The VBA Date function just returns today's date.) (VBA Date功能仅返回今天的日期。)

Public Function getDate(input_date As String) As Date
    getDate = DateSerial(1900 + Left(input_date, 3), Mid(input_date, 6, 1), Mid(input_date, 7, 1))
End Function

The addition of 1900 to the year is required because Excel handles that differently to VBA. 由于Excel处理方式与VBA不同,因此必须在年份中加上1900。 Excel treats a year of 20 as 1920, and a year of 104 as 2004. VBA uses a windowing approach where years less than 30 are treated as 20yy, and years between 30 and 99 are treated as 19yy, and years greater than or equal to 100 are treated as 0yyy. Excel将20年定为1920年,将104年定为2004年。VBA使用窗口化方法,其中将少于30年的年份视为20yy,将30到99年之间的年份视为19yy,并将大于或等于30年的年份。 100被视为0yyy。


And, although I would STRONGLY discourage its use, you could use the exact same EXCEL formula within VBA using Evaluate : 而且,尽管我强烈建议不要使用它,但是您可以使用Evaluate在VBA中使用完全相同的EXCEL公式:

Public Function getDate(input_date As String) As Variant
    getDate = Evaluate("DATE(LEFT(""" & input_date & """,3),MID(""" & input_date & """,6,1),MID(""" & input_date & """,7,1))")
End Function

You can try something like this... 您可以尝试这样的事情...

Public Function getDate(input_date As String)
    getDate = CDate(Evaluate("DATE(LEFT(" & input_date & ",4),MID(" & input_date & ",5,2),MID(" & input_date & ",7,2))"))
End Function

Sub Test()
MsgBox getDate("20170521")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM