Sheet1中的A1单元格公式:
=Sheet2!B6
在VBA中,我得到了单元格A1公式,但我希望这样做:
Dim rng1 as range
Set rng1 = worksheets("Sheet1").formula
我知道上面的内容不对,范围需要一个对象,而不是一个公式。
答案 0 :(得分:1)
如果您做了类似的事情该怎么办:
Sub t()
Dim rng As Range, formulaSheet$, formulaCell$
Dim theFormula$
theFormula = Sheets("Sheet1").Cells(1, 1).Formula
theFormula = Right(theFormula, Len(theFormula) - 1)
Debug.Print theFormula
formulaSheet = Left(theFormula, WorksheetFunction.Search("!", theFormula) - 1)
formulaCell = Right(theFormula, Len(theFormula) - WorksheetFunction.Search("!", theFormula))
Debug.Print formulaSheet & ", " & formulaCell
Dim myRange As Range
Set myRange = Sheets(formulaSheet).Range(formulaCell)
myRange.Select
End Sub
答案 1 :(得分:0)
有一点不清楚Sheet1!A1中是否有值Sheet2!B6
或者在Sheet1中实际具有公式=Sheet2!B6
!A1。
对于Sheet1!A1中的值,
Dim rng1 as range
Set rng1 = range(worksheets("Sheet1").range("a1").value)
debug.print rng1.address(0, 0, external:=true) '<~~ Sheet2!B6
对于Sheet1!A1中的公式,
Dim rng1 as range
Set rng1 = range(mid(worksheets("Sheet1").range("a1").formula, 2))
debug.print rng1.address(0, 0, external:=true) '<~~ Sheet2!B6