我在单元格 A1 中有一个字符串,它是外部单元格(即另一个工作簿)的单元格地址,
'[data transfer utility.xlsb]Sheet1'!$B$5
在 vba 中,我想创建一个范围对象,myRange
它对应于存储在单元格 A1 中的地址。
我尝试了各种解决方案,但都没有奏效。
在 VBA 中:
ExecuteExcel4Macro("'C:\[Book1.xlsx]Sheet1'!" & Range("A1").Address(, , xlR1C1))
在 Excel 中:
='C:\[Book1.xlsx]Sheet1'!$A$1
VBA:
函数参考(strAddress 作为字符串)作为范围 将 intPos 调暗为整数,intPos2 调暗为整数 暗淡 strWB 作为字符串,strWS 作为字符串,strCell 作为字符串 intPos = InStr(strAddress, "]") strWB = Mid(strAddress, 2, intPos - 2) intPos2 = InStr(strAddress, "!") strWS = Mid(strAddress, intPos + 1, intPos2 - intPos - 2) strCell = Mid(strAddress, intPos2 + 1) 参考 = Workbooks(strWB).Worksheets(strWS).Range(strCell) 结束功能
谁进入这个黑暗的地方寻找希望,请使用:
Function GetRangeReference(strAddress As String)
Dim exclamationPos As Integer
Dim strWBWS, strWB, strWS, strCell As String
exclamationPos = InStrRev(strAddress, "!")
If (exclamationPos > 0) Then
strWBWS = Mid(strAddress, 1, exclamationPos - 1)
If (Mid(strWBWS, 1, 1) = "'" And Mid(strWBWS, Len(strWBWS), 1) = "'") Then
strWBWS = Mid(strWBWS, 2, Len(strWBWS) - 2)
If (Mid(strWBWS, 1, 1) = "[" And InStr(strWBWS, "]") > -1) Then
strWB = Mid(strWBWS, 2, InStrRev(strWBWS, "]") - 2)
strWS = Mid(strWBWS, InStrRev(strWBWS, "]") + 1)
Else
strWB = ActiveWorkbook.Name
strWS = Mid(strWBWS, InStrRev(strWBWS, "]") + 1)
End If
Else
strWB = ActiveWorkbook.Name
strWS = strWBWS
End If
strCell = Mid(strAddress, exclamationPos + 1)
Else
strWB = ActiveWorkbook.Name
strWS = ActiveSheet.Name
strCell = strAddress
End If
Set GetRangeReference = Workbooks(strWB).Worksheets(strWS).Range(strCell)
End Function`