导入前更改列的数据类型

时间:2019-06-19 19:40:57

标签: excel ms-access access-vba

我有一个将Excel文件导入访问表的表单,但是此Excel工作表中有一列包含数字和单词,并且在使用VBA导入此文件时,它会为该列分配Number数据类型并创建另一个名为导入错误的表,其中包含无法转换的行。

因此,我需要一种在导入文件之前 更改此列的数据类型的方法-我知道可以在导入之前将Excel中的此列更改为Text,但是有什么方法可以用代码写出来吗?

谢谢

Sub btn_GetFileName_Click()
' Delete Old Data
On Error Resume Next
 DoCmd.DeleteObject acTable, "Table1"
'get the file name
    Debug.Print "Getting File Name"
    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Set the starting look location
    Dim strComPath As String
    strComPath = "C:\"

    Dim strFilePath As String
    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .InitialFileName = strComPath
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Excel files", "*.xls*", 1

        If .Show = -1 Then
                strFilePath = .SelectedItems(1)

        Else
            'The user pressed Cancel.
            DoCmd.Hourglass (False)
            MsgBox "You must select a file to import before proceeding" ', vbOKOnly + vbExclamation, "No file Selected, exiting"
            Set fd = Nothing
            Exit Sub
        End If
    End With

    tblFileName = strFilePath
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Table1", tblFileName, True
    MsgBox "File Imported Sucessfully"
    Set fd = Nothing
End Sub

0 个答案:

没有答案