简体   繁体   中英

Excel vba - resize as date format

I have a line in code:

.Range("D2").Resize(UBound(vef, 1), 1) = vef

Where vef is an array with data. Can I edit this line so it pastes vef in Date format dd.mm.yyyy hh:mm:ss

That should be easy enough. All you need to do is work with the same range and change the numberformat of the cells. Please note: the value being pasted is unchanged (and the values in your array should be dates). Excel applies a formatting so that what is displayed is changed, but without losing the underlying information.

If your array is full of strings, you will want to look at converting them to Dates beforehand. Excel may do this implicitly (eg it may automatically format them, despite the original values being dates), but explicit conversion is preferred.

For your code, use this:

With .Range("D2").Resize(UBound(vef, 1), 1)
    .Value = vef
    .Numberformat = "dd.mm.yyyy hh:mm:ss"
End With

Finally, I would just point out that your Ubound(vef, 1) will only work if your array is 1 based. If it is 0 based, your resized range will be off by 1. The formula you can use to find the length of a dimension in an array is:

Length = (Ubound(Array, DimensionNumber) - LBound(Array, DimensionNumber)) + 1

So in your case:

Length = (Ubound(vef, 1) - LBound(vef, 1)) + 1

Good luck!

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