下面是一個檔案路徑,我需要打開作業簿但是這個檔案路徑發生了變化,所以我有一個輸入框,它給出了檔案路徑并變暗??為位置
Workbooks.Open ("C:\Users\AylingB\OneDrive - TGE\Desktop\coding test for calcs\C5663-TD37-CAL-1900-0005_A TANK DOME ROOF STRUCTURE.xlsm")
所以我有這個
A = Range("p1")
B = Range("p2")
C = Range("p3")
Dim location As String
location = "(" & """" & A & B & C & """" & ")"
Debug.Print location
Workbooks.Open location
p1-3 是在某些單元格中拆分的檔案路徑(不幸的是,這不能改變)
這不起作用,但是即使它等于完全相同的路徑(包括括號和引號),是否有任何方法可以做到這一點,而不必每次都去 vba 代碼并更改它
我已經嘗試串聯將所有內容組合在一起。我也試過帶和不帶括號的帶和不帶語音標記的我只是有點迷失,就像我除錯時一樣。列印代碼看起來完全一樣,但只有在完全輸入時才有效
uj5u.com熱心網友回復:
該函式將要求打開 FileDialog 并要求您選擇檔案 - 從與包含代碼 ( )GetFileName的檔案相同的檔案夾開始。ThisWorkbook
Test展示了如何使用它 - 并將對它的參考存盤在變數中。
GetFileName是您應該復制到專案中的功能。
Sub Test()
Dim MyFilePath As String
MyFilePath = GetFileName 'Ask for the filename & path
Dim MyFile As Workbook
'Check that a file was selected, and it wasn't this file.
If MyFilePath <> "" And MyFilePath <> ThisWorkbook.FullName Then
Set MyFile = Workbooks.Open(MyFilePath) 'Open the file and set a reference to it.
'Rest of your code.
'Use "MyFile" whenever referencing the workbook.
Dim MySheet As Worksheet
Set MySheet = MyFile.Worksheets(1)
Dim LastRow As Long
LastRow = MySheet.Cells(Rows.Count, 1).End(xlUp).Row
MsgBox MyFile.Name & " contains " & MyFile.Worksheets.Count & " worksheets." & vbCr & _
"The last row in " & MySheet.Name & " column A is " & LastRow
End If
End Sub
Private Function GetFileName() As String
Dim FD As FileDialog
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.InitialFileName = ThisWorkbook.Path & Application.PathSeparator
.AllowMultiSelect = False
'Not needed unless want to limit to specific file types.
.Filters.Clear 'On it's own will set filter to "All Files", "*.*"
.Filters.Add "Excel Files", "*.xlsx, *.xlsm, *.xls"
.Filters.Add "Other Files", "*.csv, *.someotherextension"
.FilterIndex = 2 'Display "Other Files" as default.
If .Show = -1 Then
GetFileName = .SelectedItems(1)
End If
End With
Set FD = Nothing
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521995.html
標籤:擅长vba
上一篇:VBA宏非常慢
下一篇:如何從串列中洗掉單元格中的值
