Excel 将修剪应用于单元格…值生成应用程序定义的错误

Excel 将修剪应用于单元格…值生成应用程序定义的错误,excel,vba,ms-access,trim,Excel,Vba,Ms Access,Trim,下面的代码应将文本文件导入Excel 我想修剪一些列,因为一些字段有空格而不是空值,这给我在导入access时带来了问题,因为这些字段的数据类型是日期 我在代码的修剪部分遇到了应用程序定义的错误 Dim xl As excel.Application Dim wbk As excel.Workbook Dim wst As excel.Worksheet Dim DFPath 'DFPath reference folder, not the file DFPath = Forms!DailyF

下面的代码应将文本文件导入Excel

我想修剪一些列,因为一些字段有空格而不是空值,这给我在导入access时带来了问题,因为这些字段的数据类型是日期

我在代码的修剪部分遇到了应用程序定义的错误

Dim xl As excel.Application
Dim wbk As excel.Workbook
Dim wst As excel.Worksheet
Dim DFPath
'DFPath reference folder, not the file
DFPath = Forms!DailyFileMacro!OutFilePath
Dim DFname
DFname = Forms!DailyFileMacro!OutFileName

Set xl = CreateObject("Excel.Application")
With xl
    .Visible = True
    Set wbk = .Workbooks.Add
    Set wst = wbk.Worksheets(1)
    With wst.QueryTables.Add(Connection:="TEXT;" & Forms!DailyFileMacro!InFilePath & Forms!DailyFileMacro!InFileName, Destination:=Range("$A$1"))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 3, 3, _
        1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False

    Dim rowcount As Long
    rowcount = Range("A1").SpecialCells(xlCellTypeLastCell).Row
    For i = 2 To rowcount
        If Cells(i, 19).Value Like "Adjustment*" Then
            Cells.Offset(0, 1).Value = Trim(Cells.Offset(0, 1).Value)
            Cells.Offset(0, 2).Value = Trim(Cells.Offset(0, 2).Value)
        End If
    Next i
End With

End With
wbk.SaveAs Filename:=(DFPath & DFname), FileFormat:=56
wbk.Close SaveChanges:=False
xl.Quit
基于
If单元格(i,19).Value
,我想您指的是两行
Trim

Cells(i, 20).Value = Trim(Cells(i, 20).Value)
Cells(i, 21).Value = Trim(Cells(i, 21).Value)

单元格
按原样调用(没有行/列引用或父级
范围
引用)引用整个
工作表
,因此
偏移
1或2列是不可能的,并且总是会引发运行时错误。

单元格.Offset(0,1)和
单元格.Offset(0,2)
-如果循环考虑
单元格(i,19)
,您的意思是
单元格(i,20)
单元格(i,21)
?是的。。。我现在才意识到它有什么问题。谢谢你帮我抓住它!