【发布时间】:2018-04-20 15:07:35
【问题描述】:
好的,我知道这是一种非常愚蠢的方法,但我有一个用户表单,允许用户输入 3 件事:文本文件所在的路径、文本文件的名称以及从文本文件中导入数据。我要做的是按照用户输入的路径,通过用户输入的名称获取文本文件,并将文本文件包含的任何内容放入用户输入的范围内。
我知道使用 Application.GetOpenFilename 可以很容易地简化它,但是你必须做你必须做的事情。我需要做的是能够指定路径、指定文本文件以及将文本文件中的所有文本加载到带有逗号分隔符的范围中的位置。目前,该程序要么在“.Refresh BackgroundQuery:=False”行中断,要么根本不做任何事情,我不知道如何前进。我的代码如下-为混乱道歉。如果您能提供帮助,请告诉我!
Private Sub OKButton_Click()
Dim filePath As String, destin As String, fileName As Variant
destin = DestinationTextBox.Value
fileName = TextFileNameTextBox.Value
filePath = Dir(FileLocationTextBox.Value)
'filePath = Application.GetOpenFilename("Text Files (*.txt), *.txt")
'oh, if only
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & _
filePath, Destination:=Range(destin))
.Name = fileName & ".txt"
.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 = xlTextQualifierNone
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "" & Chr(10) & ""
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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
End Sub
【问题讨论】:
-
你的帖子我看了两遍,不知道是什么问题。可以edit转转吗?
-
我已经编辑了,如果您需要更多说明,请告诉我!