我想完成一個腳本,將允許用戶選擇另一個Excel文件,當單元格被雙擊,然後該Excel文件被用來放入一個公式到主要的excel文件。Excel VBA:公式沒有從字符串輸入正確
我無法單獨使用單元格值,因爲在腳本完成時能夠在編輯欄中看到文件路徑是必需的。所以問題是輸入的公式與它應該從中拉取的字符串文本不匹配。
爲了便於說明,我使用的名爲FormulaPath的字符串最後是一個以「...\00975-006-00[00975-006-00.xls]QuoteDetails'!
」結尾的公式,這將是正確的公式。
但是,當我使用它來輸入公式爲範圍:
Range("A1").Formula = "=" & FormulaPath & "$C$100"
實際公式最終被輸入爲"...[00975-006-00[00975-006-00.xls]Quote Details]00975-006-00[00975-006-00.xls]Q'!$C$100
通知的重複?
我現在在手機上,所以請原諒我,如果格式是古怪的。下面的完整腳本。謝謝!
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim ImportWB, QuoteWB As Workbook
Dim AdInsWS, AdInsCostWS As Worksheet
Dim ImportPathName As Variant
Dim FormulaPath As String
Set QuoteWB = ThisWorkbook
Set AdInsWS = QuoteWB.Sheets("Ad-Ins")
Set AdInsCostWS = QuoteWB.Sheets("Ad-ins cost")
If Not Intersect(Target, Range("B:B")) Is Nothing Then
'set default directory
ChDrive "Y:"
ChDir "Y:\Engineering Management\Manufacturing Sheet Metal\Quotes"
'open workbook selection
ImportPathName = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*", Title:="Please select a file")
If ImportPathName = False Then 'if no workbook selected
MsgBox "No file selected."
ElseIf ImportPathName = ThisWorkbook.Path & "\" & ThisWorkbook.Name Then 'if quote builder workbook selected
MsgBox "Current quote workbook selected, cannot open."
Else
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open Filename:=ImportPathName, UpdateLinks:=False
Set ImportWB = ActiveWorkbook
FormulaPath = "'" & ImportWB.Path & "[" & ImportWB.Name & "]Quote Details'!"
AdInsCostWS.Range("B3").Formula = "=" & FormulaPath & "$C$100"
ImportWB.Close
End If
Cancel = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End If
End Sub
仙geesh ...我可以相信就是這樣。我不完全理解爲什麼它會使公式如此錯誤,並且在我忽略該內容時將其添加到路徑的額外部分中,但是感謝您解決此問題。 –