无法将CopyFromRecordset结果设置为Date或Number

时间:2015-06-18 19:48:12

标签: excel vba excel-vba ms-access-2010

我尝试使用var e : Element? = document.getElementById("text") e.offsetHeight //Error 将Access 2010中的数据集导入Excel。查询中有一些字段,其中一些值已转换为日期或数字,例如:

Range.CopyFromRecordset

以下是我用来生成包含此数据的工作表的代码:

iif(isdate([Install Date]),cdate([Install Date]),[Install Date]) 
AS [Install Date (cast)],
iif(isnumeric([Feature Number]),cdbl([Feature Number]),[Feature Number]) 
AS [Feature Number (cast)]

然而,当这个子运行时,数字列仍然被格式化为文本,并且日期列仍被视为字符串 - 当我应用自动过滤器时,过滤器列出每个单独的日期而不是按月分组它们年。

如何让这些列以所需格式结束?

1 个答案:

答案 0 :(得分:1)

解决:

'check if field is a date field
If InStr(UCase(rs.Fields(fldCtr).Name), "DATE") > 0 Then

    'format column as date
    Set columnRng = sht.Cells(1, fldCtr + 1)
    columnRng.EntireColumn.NumberFormat = "mm/dd/yyyy"

    'refresh column values with new formatting
    columnrng.EntireColumn.Value=columnrng.EntireColumn.Value


'check if field is number cast
ElseIf InStr(UCase(rs.Fields(fldCtr).Name), "(CAST)") > 0 Then

    'format column as number
    Set columnRng = sht.Cells(1, fldCtr + 1)
    columnRng.EntireColumn.NumberFormat = 0

    'refresh column values with new formatting
    columnrng.EntireColumn.Value=columnrng.EntireColumn.Value
End If