第一行数据未显示正确的数据

时间:2019-01-24 23:12:53

标签: access-vba

我正在循环并链接excel文件。有时,最上面一行具有标题名称的数据。问题在于,当列名称类似于“ date”,“ upc”,“ quatity”时,则当其链接到单元格中时,显示的是“ #Num!”。我该如何解决这个问题,以便看到“日期”和其他字段名称。

   Set wkb = objXL.Workbooks.Open(strFileName)
   ShtCount = wkb.Sheets.count
   i = 1
   x = 1

   For Each wks In wkb.Worksheets
        ' Link each sheet of the entire Spreadsheet file -----------------------
        'DoCmd.TransferSpreadsheet acLink, , _
        '      shortFilename & "_~" & wks.Name, strFileName, False, wks.Name & "$"

        'Progress Bar :::::::::::::::::::::::::::::::::::::
        RetVal = SysCmd(acSysCmdInitMeter, "Linking FILE: " & PathFilename & " - SHEET: " & Trim(str(x)) & " of " & Trim(str(ShtCount)), ShtCount)
        'Update the progress meter.
        RetVal = SysCmd(acSysCmdUpdateMeter, i)

        'TabName = wks.Name
        DoCmd.TransferSpreadsheet acLink, , _
              "TmpLinkXLS", strFileName, False, wks.Name & "$"

        ' Get the list of field names
        'Set tdf = db.TableDefs("[" & shortFilename & "_~" & wks.Name & "]")
        Set tdf = db.TableDefs("TmpLinkXLS")

        ' ---- Pause 1 second ------
        Const cTIME = 1000 'in MilliSeconds
        Call sSleep(cTIME)

        myfields = ""
        For Each fld In tdf.Fields
            myfields = myfields & "[" & fld.Name & "],"
        Next
        'myfields = Mid(myfields, 1, Len(myfields) - 1)
        i = i + 1
        RetVal = SysCmd(acSysCmdInitMeter, "Inserting FILE: " & PathFilename & " - SHEET: " & Trim(str(x)) & " of " & Trim(str(ShtCount)), ShtCount)
        'Update the progress meter.
        RetVal = SysCmd(acSysCmdUpdateMeter, i)

        ' Insert the data from the sheet into the local XLSData Table --------------------------
        'strsql = "Insert Into XLSData(" & myfields & "Fullimagepath,TabName" & ") Select " & myfields & Chr(34) & strFileName & Chr(34) & " as Fullimagepath, " & wks.Name & " as TabName From [" & shortFilename & "_~" & wks.Name & "] "
        strsql = "Insert Into XLSData(" & myfields & "Fullimagepath,TabName" & ") " & _
                    " Select " & myfields & Chr(34) & strFileName & Chr(34) & " as Fullimagepath, " & Chr(34) & wks.Name & Chr(34) & " as TabName " & _
                    " From TmpLinkXLS As x1 " & _
                    " Where Len(Trim(x1.F1)) > 0 OR Len(Trim(x1.F2)) > 0 OR Len(Trim(x1.F3)) > 0 OR Len(Trim(x1.F4)) > 0 OR Len(Trim(x1.F5)) > 0 OR Len(Trim(x1.F6)) > 0 OR Len(Trim(x1.F7)) > 0 OR Len(Trim(x1.F8)) > 0 OR Len(Trim(x1.F8)) > 0 "
        CurrentDb.Execute strsql, dbFailOnError

        ' ---- Pause 1 second ------
        'Const cTIME = 1000 'in MilliSeconds
        Call sSleep(cTIME)

        i = i + 1
        RetVal = SysCmd(acSysCmdInitMeter, "Dropping TmpLinkXLS", ShtCount)
        'Update the progress meter.
        RetVal = SysCmd(acSysCmdUpdateMeter, i)

        If TableExists("TmpLinkXLS") Then
            CurrentDb.Execute "Drop Table TmpLinkXLS", dbFailOnError
        End If

        Set tdf = Nothing

        RetVal = SysCmd(acSysCmdInitMeter, " ", 0)
        RetVal = SysCmd(acSysCmdRemoveMeter)

        i = 1
        x = x + 1

        'DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        '      wks.Name, strFileName, True, wks.Name & "$"
   Next

1 个答案:

答案 0 :(得分:0)

您必须以某种方式知道然后指定第一行包含标题,并将该参数设置为 True

DoCmd.TransferSpreadsheet acLink, , "TmpLinkXLS", strFileName, True, wks.Name & "$"