用户点击单元格,提示用户定位文本文件。 单元格值包含文件位置的path

我有一个文本数据文件,我下载到我的电脑上的位置。 现在,在我的VBA脚本中,我有硬编码的path。 我想要的是,如果这是可能的,用户在这种情况下点击工作表“Main”中的一个单元格(C5),当他/她这样做时,popup一个对话框提示用户导航到文本文件并select它。 用户select文本文件后,我想单元格C5值保存文本文件的文件path。

我想C5的值被replace为我的VBA脚本中的硬编码文件path:

With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;\\psf\Home\Desktop\Temp\sample.txt", Destination:=Range( _ "$A$1")) .Name = "fills" .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 = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = True .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileOtherDelimiter = "|" .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With 

我想要C5的值进入连接后的部分:=我不知道如何去做的部分,并希望帮助/input。

这可能吗?

下面的代码将在用户右键单击 C5时运行

第1部分

  • 右键单击您的工作表标签
  • 查看代码
  • 复制并在下面的代码中过去

 Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) Dim strFileToOpen As String If Target.Address(0, 0) <> "C5" Then Exit Sub strFileToOpen = Application.GetOpenFilename _ (Title:="Please choose a file to place in C5 (pick Open)", _ FileFilter:="Text Files *.txt (*.txt)," If strFileToOpen = "False" Then [c5].Value2 = "No file selected" Else [c5].Value2 = strFileToOpen End If End Sub 

第2部分

更改

 With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;\\psf\Home\Desktop\Temp\sample.txt", Destination:=Range( _ "$A$1")) 

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