VBA_Excel采用日期变量并将其作为数字粘贴到单元格内

时间:2014-05-17 13:31:30

标签: excel vba format excel-2010 paste

好的,我有这个小代码

If Trim(apriori) <> "" Then
        With Sheets(yolk).Range("1:1")
            Set Rng = .Find(apriori, .Cells(.Cells.Count), xlValues, xlWhole, xlByRows, _
                            xlNext, False)
            yolk = yolk - 1
            'e = 1
            If Not Rng Is Nothing Then
                'Application.Goto Rng, True
                Rng.Offset(26, 0).Copy 'Worksheets("EXTRACTIONS").Range("B2").Offset(, e)
                some_date = Format(apriori, "dd/mmmm/yyyy") 'XX
                MsgBox some_date
                Worksheets("EXTRACTIONS").Range("B2").Offset(, e).Value = some_date 'XX
                Worksheets("EXTRACTIONS").Range("B3").Offset(, e).PasteSpecial xlPasteValues 'EDIT
                e = e + 1
                'activeCell kane offset ekei pou theloume
                'prwta offset theloume to address pou tha kanei to offset
                'timy = ActiveCell.Address
            Else
                MsgBox "Nothing found"
            End If
        End With
End If

价值apriori是美国格式的date value,我想要的是将其转换为日期查看日历的方式,例如:

12/31/2010 to 31/December/2010

所以我用

format(apriori, "dd/mmmm/yyyy")

它完成了这项工作,我有了日期,因为我希望她在msgbox some_date

但是,一旦我尝试使用

将值粘贴到单元格
Worksheets("EXTRACTIONS").Range("B2").Offset(, e).Value = some_date

该单元格中填充了数字(!!!!),如41,274或41,679 ......(?)

如何在VBA代码中更正此错误?

1 个答案:

答案 0 :(得分:2)

您应该更改单元格的数字格式,如下所示:

Dim some_date As Date
some_date = Now

With Worksheets("EXTRACTIONS").Range("B2").Offset(, e)
    .NumberFormat = "dd/mmmm/yyyy"
    .Value = some_date
End With