是否有可能在vba中有单元格(c,8).value内的datevalue命令?
我是VBA新手。 希望有人能帮忙。 目前,细胞(2,8)= 31-Dec-14,我想在细胞(2,3)中有31-Dec-15。 这是要做到最后一个loggingx。
我现在拥有的:
Dim x As Long x = Range("A65536").End(xlUp).Row For c = 2 To x If Cells(c, 11).Value = "Success" Then Cells(c, 3).Value = Cells(c, 8).Value End If Next c
我想要replace单元格(c,8).Value将显示与function相同的结果,
=Text(Date(Year(H2)+1,Month(H2)+1,0),"dd-mmm-yy")
VBA中的公式:
Format(DateSerial(Year(Cells(c, 8).Value), Month(Cells(c, 8).Value) + 1, 0), "dd-mmm-yy")
我们使用Format()
代替TEXT()
和DateSerial()
代替DATE()
所以:
Dim x As Long x = Range("A65536").End(xlUp).Row For c = 2 To x If Cells(c, 11).Value = "Success" Then Cells(c, 3).Value = Format(DateSerial(Year(Cells(c, 8).Value), Month(Cells(c, 8).Value) + 1, 0), "dd-mmm-yy") End If Next c
虽然上面将返回一个dateforms的string,而不是一个真正的date。 如果你想要一个可以在其他公式中使用的date,你可能想要在接收单元上使用一个NumberFormat。
Dim x As Long x = Range("A65536").End(xlUp).Row For c = 2 To x If Cells(c, 11).Value = "Success" Then Cells(c, 3).Value = DateSerial(Year(Cells(c, 8).Value), Month(Cells(c, 8).Value) + 1, 0) Cells(c, 3).NumberFormat = "dd-mmm-yy" End If Next c
这将返回正确的格式,并将date保留为可用于未来公式的数字。