以下代码,引用到了与活动工作薄同一目录中的123.xlsx
如果123.xlsx在D盘根目录D:\123.XSLX,与活动工作薄不在一起,我该如何更改代码?
我试这将sFile = "123.xlsx"改为sFile = "D:\123.xlsx",但不成功。
俺VBA新手一个,请大侠来多多指教!
Option Explicit
Function FUN(rC As Range)
Dim i!, sFile$, sPath$, a, b
Dim rF1 As Range, rF2 As Range, oWb As Object, oSh As Worksheet
sPath = ThisWorkbook.Path
sFile = "123.xlsx"
a = Split(rC, Chr(10))
ReDim b(UBound(a))
On Error Resume Next
Set oWb = Application.Workbooks(sFile)
On Error GoTo 0
If oWb Is Nothing Then
Set oWb = GetObject(sPath + "\" + sFile)
End If
Set oSh = Workbooks(sFile).Sheets("Sheet1")
For i = 0 To UBound(a)
Set rF1 = oSh.Range("A:A").Find(Mid(a(i), 21, 3))
Set rF2 = oSh.Range("A:A").Find(Mid(a(i), 24, 3))
If rF1 Is Nothing Or rF2 Is Nothing Then FUN = "Error": Exit Function
b(i) = oSh.Cells(rF1.Row, 2) & "-" & oSh.Cells(rF2.Row, 2)
Next
FUN = Join(b, Chr(10))
Set oSh = Nothing
Set oWb = Nothing
Erase a, b
End Function
这一条是路径
sPath = 你要的路径
比如
sPath = "C:\文件夹名字"