关闭文件浏览器后出错

马克斯·范德比岑

我创建了一个非常好的工作 vba 代码,用于从 Windows 文件资源管理器导入 csv 文件。但是,当我在打开文件之前关闭资源管理器时,会弹出一个 1004 错误对话框。它说找不到刷新外部范围的文本文件。底部的线应该是原因:

.Refresh BackgroundQuery:=False

有谁知道如何摆脱这个错误?

Dim ClickCount As Integer

Sub CommandButton1_Click()

Dim sht As Worksheet
Dim LastRow As Long
Dim begin As String
Dim myInput As Long

ClickCount = ClickCount + 1

If ClickCount > 1 Then GoTo Line1 Else GoTo Line2

Line1:
myInput = MsgBox("Gebruikers zijn reeds geimporteerd. Records worden mogelijk dubbel opgeslagen. Wilt u toch doorgaan met importeren?", vbCritical + vbYesNo, "Import error")
    If myInput = vbYes Then
    GoTo Line2
    Else
    Exit Sub

Line2:

Set sht = ActiveSheet

  LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
begin = "$A" & "$" & LastRow + 1

Dim fileName
  fileName = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv")

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & fileName, _
        Destination:=range(begin))
        .Name = "User import 1.0"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
  End If
End Sub
蒂姆·威廉姆斯
Dim fileName
fileName = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv")

If fileName = False then Exit Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章