我创建了一个非常好的工作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
答案 0 :(得分:0)
Dim fileName
fileName = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv")
If fileName = False then Exit Sub