Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA提示输入不带定界的DAT、添加新图纸和导入DAT_Vba_Excel - Fatal编程技术网

VBA提示输入不带定界的DAT、添加新图纸和导入DAT

VBA提示输入不带定界的DAT、添加新图纸和导入DAT,vba,excel,Vba,Excel,该代码提示用户选择一个Excel文件和五个不同的DAT文件。Excel文件加载到工作表中,然后为每个要导入的DAT文件添加新工作表。Excel文件加载正确,但程序在第一次尝试导入DAT文件时出错 错误:运行时错误“1004”:应用程序定义的错误或对象定义的错误 这就是发生错误的地方: ActiveSheet.QueryTables.Add(Connection:= _ DIFN, Destination _ 以下是代码的其余部分: ' Prompt user for files

该代码提示用户选择一个Excel文件和五个不同的DAT文件。Excel文件加载到工作表中,然后为每个要导入的DAT文件添加新工作表。Excel文件加载正确,但程序在第一次尝试导入DAT文件时出错

错误:运行时错误“1004”:应用程序定义的错误或对象定义的错误

这就是发生错误的地方:

ActiveSheet.QueryTables.Add(Connection:= _
    DIFN, Destination _
以下是代码的其余部分:

' Prompt user for files
    CAFN = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")
    DIFN = Application.GetOpenFilename("ESDI DAT File (*.dat), *.dat")
    FOFN = Application.GetOpenFilename("ESFO DAT File (*.dat), *.dat")
    FSFN = Application.GetOpenFilename("ESFS DAT File (*.dat), *.dat")
    IPFN = Application.GetOpenFilename("ESIP DAT File (*.dat), *.dat")
    PPFN = Application.GetOpenFilename("ESPP DAT File (*.dat), *.dat")

' Load Combined All
    Dim x As Workbook
    Dim y As Workbook

    '## Open both workbooks first:
    Set y = ActiveWorkbook
    Set x = Workbooks.Open(CAFN)

    'Now, transfer values from x to y:
    With x.Sheets("Sheet1").UsedRange
        'Now, paste to y worksheet:
        y.Sheets("Start").Range("A1").Resize( _
            .Rows.Count, .Columns.Count) = .Value
    End With

    'Close x:
    x.Close

    y.Sheets("Start").Name = "Combined All"

' Load DAT files
    ActiveWorkbook.Worksheets.Add.Name = "ESDI"
    With ActiveSheet.QueryTables.Add(Connection:= _
        DIFN, Destination _
        :=Range("$A$1"))
        .Name = "ESDI"
        .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 = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 1, 1, 9, 1, 9, 9, 1, 9, 9, 9)
        .TextFileFixedColumnWidths = Array(3, 7, 7, 2, 10, 8, 3, 8, 40, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

假设要打开的文件是文本文件

With ActiveSheet.QueryTables.Add(Connection:= _
    DIFN, Destination _
    :=Range("$A$1"))
应替换为

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & DIFN, _
                                 Destination:=Range("$A$1"))

这是为我工作的代码谢谢你给YowE3K和Scott Holtzman给我指明了正确的方向

' Prompt user for files
Dim CAFN As String
Dim DIFN As String
Dim FOFN As String
Dim FSFN As String
Dim IPFN As String
Dim PPFN As String

CAFN = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")
DIFN = Application.GetOpenFilename("ESDI DAT File (*.dat), *.dat")
FOFN = Application.GetOpenFilename("ESFO DAT File (*.dat), *.dat")
FSFN = Application.GetOpenFilename("ESFS DAT File (*.dat), *.dat")
IPFN = Application.GetOpenFilename("ESIP DAT File (*.dat), *.dat")
PPFN = Application.GetOpenFilename("ESPP DAT File (*.dat), *.dat")

' Load Combined All
Dim x As Workbook
Dim y As Workbook

'## Open both workbooks first:
Set y = ActiveWorkbook
Set x = Workbooks.Open(CAFN)

'Now, transfer values from x to y:
With x.Sheets("Sheet1").UsedRange
    'Now, paste to y worksheet:
    y.Sheets("Start").Range("A1").Resize( _
        .Rows.Count, .Columns.Count) = .Value
End With

'Close x:
x.Close

y.Sheets("Start").Name = "Combined All"


' Load DAT files
    ActiveWorkbook.Worksheets.Add.Name = "ESDI"
    MsgBox ">>>" & "TEXT;" & DIFN & "<<<"
    With Sheets("ESDI").QueryTables.Add(Connection:="TEXT;" & DIFN, Destination:=Range("$A$1"))
        .Name = "ESDI"
        .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 = xlFixedWidth
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 1, 1, 9, 1, 9, 9, 1, 9, 9, 9)
        .TextFileFixedColumnWidths = Array(3, 7, 7, 2, 10, 8, 3, 8, 40, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh
    End With

当您将DIFN传递到连接时,它的值是多少:=参数?C:\Users\Public\Documents\esdipr.data DIFN是否声明为?它需要是字符串才能工作。如果不是字符串,CStrDIFN可能会工作。在上述代码开头之前将Dim DFIN添加为字符串,仍然会收到错误。是的,抱歉。这是模糊的字符串。谢谢,我仍然得到错误。该文件是一个.dat文件,如果有区别的话。文件扩展名实际上没有任何区别,只是文件中包含的内容。With语句后面的所有代码使我认为它一定是一个以列分隔的文本文件。如果它不是文本文件,您仍然需要指定它是什么类型的文件-请参阅。啊,是的,它是文本。我刚刚在新工作簿中运行了以下代码,并且工作正常:ActiveWorkbook.Worksheets.Add.Name=ESDI With ActiveSheet.QueryTables.AddConnection:=text;L:\xxxx.txt,Destination:=范围$A$1.Name=ESDI结束,并引发以下问题:C:\Users\Public\Documents\esdipr.DAT是否存在?可以去吗?它不是只读的吗?可能还会提出其他问题,但这是一个好的开始。