通过VBA自动导入CSV时出错

时间:2018-05-28 13:22:04

标签: excel vba excel-vba csv import

我最近做了一个xlsm。我自动打开CSV文件的文件,该文件位于与我的工作簿相同的地图中。在我的电脑上它工作正常,但在与我正在使用的家伙的电脑上,它只是没有打开。他收到“1004”应用程序定义或对象定义的“错误”。

以下是导入CSV文件的宏代码。也许在我所指的道路上出错了?我的朋友有另一台电脑,所以......我不知道。感谢帮助。

Sub Auto_Open()
'
' Imports CSV

'
    Range("A9:BU156").Select
    Selection.Delete Shift:=xlToLeft
    Range("A8").Select
    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\Name\Dropbox\YGH\BYMKEW8 - Deel II\Uitwerking - kopie\Eerstejaars studenten ingeschreven hbo - kopie.csv" _
        , Destination:=Range("$A$9"))
        .Name = "Eerstejaars studenten ingeschreven hbo - kopie"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 65001
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierNone
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Sheets("Blad1").Select
    Range("A9:R9").Select
    With Selection.Font
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0
    End With
    With Selection.Font
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
    End With
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True
    Columns("A:A").ColumnWidth = 11.86
    Columns("F:F").ColumnWidth = 25.86
    Columns("G:G").ColumnWidth = 18
    Columns("H:H").ColumnWidth = 21.14
    Columns("I:I").ColumnWidth = 15.86
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 6
    Columns("J:J").ColumnWidth = 17.14
    Columns("J:J").ColumnWidth = 21.57
    Columns("J:J").ColumnWidth = 34.43
    Columns("J:J").ColumnWidth = 26
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 1
        Range("A9:R125").Select
    ActiveWorkbook.Names.Add Name:="List", RefersToR1C1:="=Blad1!R9C1:R125C18"
    Range("A1").Select
End Sub

1 个答案:

答案 0 :(得分:1)

你的朋友可能没有这条路径:

C:\Users\Name\Dropbox\YGH\BYMKEW8 - Deel II\Uitwerking - kopie\Eerstejaars studenten ingeschreven hbo - kopie.csv

因此得到1004这一行:

.Refresh BackgroundQuery:=False