VBA代码不会跳转到单元格
Hy我正试图做一个跳转到一个单元格的代码。
地址存储在一个variables中。
我试过2个选项,但都不能和variables一起工作。
他们与具体的单元格地址工作正常。
Dim stMsg As String Dim x As String x = Left(stMsg, Len(stMsg) / 2) 'x = 'Sheet2'!$C$8 '1 Option Application.Goto Reference:="x" 'this works with format: Sheet2!R8C3 '2 Option With Range("x") 'this works with format: 'Sheet2'!$C$8 , which is exactly x .Parent.Activate .Activate End With
它给了我
方法'范围'或object'_Global'失败的“错误。
stMsg
是在macros的第一部分中find的variables。 第一部分在一个单元格中find一个公式,并在公式中findstMsg
公式,它们是2, stMsg
存储它们,这就是为什么我用x分割它。 stMsg
的值是'Sheet2'!$ C $ 8'Sheet2'!$ C $ 8
我如何使它与x一起工作?
x
是你的variables,你试图用它作为"x"
,这是一个只包含字母x的文本
所以你的代码应该更像这样:
Dim x As String x = Left(stMsg, Len(stMsg) / 2) '''Option 1 Application.Goto Reference:=x '''Option 2 With Range(x) .Parent.Activate .Activate End With
并为两个选项工作:
由OP提供的input: 'Sheet2'!$C$8'Sheet2'!$C$8
Sub test_tombata(stMsg As String) Dim x As String x = Left(stMsg, Len(stMsg) / 2) 'x = "'Sheet2'!$C$8" 'Debug.Print x '''Option 1 Application.Goto Reference:=Sheets(Replace(Split(x, "!")(0), "'", vbNullString)).Range(Split(x, "!")(1)) '''Option 2 With Range(x) .Parent.Activate .Activate End With End Sub
并为两个选项工作:
由OP提供的input: 'Sheet2'!$C$8
Sub test_tombata2(x As String) '''Option 1 Application.Goto Reference:=Sheets(Replace(Split(x, "!")(0), "'", vbNullString)).Range(Split(x, "!")(1)) '''Option 2 With Range(x) .Parent.Activate .Activate End With End Sub
代码尝试两个:
Sub TEST_test_tombata() test_tombata "'Sheet2'!$C$8'Sheet2'!$C$8" test_tombata2 "'Sheet2'!$C$8" End Sub
以下的希望是你的,假设K9是包含目标范围:
Dim rw, col As integer rw = Range(Range("K9").Value).Row col = Range(Range("K9").Value).Column Application.Goto ActiveSheet.Cells(rw + 0, col - 0)