通过VBA访问excel,数字显示错误

时间:2014-06-20 08:58:20

标签: excel vba

我创建了一个小的VBA代码,用于从访问excel中导出数据,导出时所有数字格式都在excel中显示为日期。我检查了Access数据库中的格式吧。你能告诉我如何将它们显示为数字吗?所有数字列都显示为日期,因此如果该代码行检查所有列并显示正确/默认格式,那将是很好的。

参考我的代码,

Private Sub Command4_Click()


 'Defining database variable
  Dim dbnxair As DAO.Database
  'Defining Excel variable
   Dim excelnxair As Excel.Application
  'Defining workbook variable
  Dim wrkbooknxair As Excel.Workbook
 'Defining Worksheets
 Dim wrksheetnxair As Excel.Worksheet
'Defining recordset
 Dim recsetnxair As Recordset
'Defining Querydef
 Dim querynxair As QueryDef
 'Defining query as string
 Dim strSQL As String
 'Defining condition string
Dim strlistcondition As String


'Opening the database
 Set dbnxair = CurrentDb()

'Creating the Excel object
Set excelnxair = CreateObject("Excel.Application")
 'Opening the worksheet
  excelnxair.Visible = True

  'Creating a new workbook
 Set wrkbooknxair = excelnxair.Workbooks.Add
 wrkbooknxair.SaveAs "C:\Users\z003cnxt\Desktop\new1.xls"

  'Creating a new worksheet
 Set wrksheetnxair = excelnxair.Sheets(1)

 'Creating a query
Set querynxair = dbnxair.CreateQueryDef("")

 For Each varItem In Me!List2.ItemsSelected
   strlistcondition = strlistcondition & ",'" & Me!List2.ItemData(varItem) & "'"
Next varItem

strlistcondition = Right(strlistcondition, Len(strlistcondition) - 1)

strSQL = "SELECT * FROM [Std Table] WHERE ([Std Table].Country IN(" & strlistcondition    & "));"

'Adding Sql statement
 querynxair.SQL = strSQL



'Creating and opening the recordset
 Set recsetnxair = querynxair.OpenRecordset()

  'Copying data to excel sheet
  excelnxair.Cells.CopyFromRecordset recsetnxair


  'Adding field names in each columns
 For lvlcolumn = 0 To recsetnxair.Fields.Count - 1
   excelnxair.Cells(1, lvlcolumn + 1).Value = recsetnxair.Fields(lvlcolumn).Name
 Next

 wrksheetnxair.Cells.AutoFilter
wrksheetnxair.Cells.EntireColumn.AutoFit


  Set dbnxair = Nothing
 Set recsetnxair = Nothing
  Set querynxair = Nothing
 Set wrkbooknxair = Nothing
Set wrksheetnxair = Nothing
Set excelnxair = Nothing

For Each varItem In Me!List2.ItemsSelected
  Me!List2.Selected(varItem) = False
Next varItem


End Sub

1 个答案:

答案 0 :(得分:0)

我通过在我的代码中将所有数字格式设置为sFormat来解决我的问题。但我仍然没有找到dateformat的解决方案。

此致 jeeva