简体   繁体   中英

Excel VBA - Can I assign part of a Cell Value to a variable?

I have a date in a particular cell (B3) - Date - 12/03/2016. I am trying to assign only the Year part of the date, ie 2016 to a variable. Is it possible?

you can use:

Dim yourvariable%
yourvariable = int(mid([B3],7,4))

or

Dim yourvariable%
yourvariable = int(Right([B3],4))

or

Dim yourvariable%
yourvariable = year(cdate([B3]))

Consider:

Sub dural()
    Dim v As Variant

    v = Split(Range("B3").Text, "/")(2)
    MsgBox v
End Sub

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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