VBA Goto Syntax

SeliM

Board Regular
Joined
Aug 10, 2023
Messages
51
Office Version
  1. 365
Platform
  1. Windows
Good morning

Thanks for taking time to read my enquiry

I have a WB that holds Criteria for assessing tenders, the wording of the tender output from another system need to exactly match the wording of the assessment system.

Where it does not match I need the system to goto the address of the assessment system.

I have found a very simple goto script:

Sub jumpToCell()
Dim aAddress() As String
aAddress = Split([Sheet2!D3].Text, "!")
Application.Goto Reference:=Worksheets(aAddress(0)).Range(aAddress(1))
End Sub


Which on a test WB works perfectly.

However, when I endeavour to change names of WS to a text say Sheet1 to A and Sheet2 to B I get a Run time error 9.

Would you be able to advise on how to remedy pleas.

R
Mel
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
If you change Sheet2 to A the code will go to the cell referenced in the contents of Sheet A cell D3, what do you have in that cell?
 
Upvote 0
Thanks Mark

Sheet2 (or A) $D$3 holds a formula that gives an address (of mismatched text) -
Sheet1!$c$3
(formula - =IF(C3<>Sheet1!C3,"Sheet1!$c$3",""))

Sheet1 & Sheet2 works fine
changing name of WS and varying formula & script draws run time error

Hoe this clarifies.
Mel
 
Upvote 0
You'll only get that error if the result in D3 does not match a valid sheet name/cell.

With
Book1
DEFGH
1
2
3Sheet1!$c$3Sheet1!$c$31
4
5
A
Cell Formulas
RangeFormula
D3D3=IF(H3=1,F3,"")


in sheet A

the code
VBA Code:
Sub jumpToCell()
    Dim aAddress() As String
    aAddress = Split([A!D3].Text, "!")
    Application.Goto Reference:=Worksheets(aAddress(0)).Range(aAddress(1))
End Sub

Goes to Sheet1 cell C3 fine for me running the code from Sheet4
 
Last edited:
Upvote 0
With
Book1
CD
3999Sheet1!$c$3
A
Cell Formulas
RangeFormula
D3D3=IF(C3<>Sheet1!C3,"Sheet1!$c$3","")

and
Book1
BCD
2
3888
Sheet1


I get the same result, the code goes to Sheet1 cell C3 fine
 
Upvote 0
Thanks very much Mark
using default Sheet1 Sheet2 etc. - perfect
Changing sheet2 to Match Text is fine.
Changing the go to ws is the challenge - t doesnt like 2 word WS names apparently

I have through experimenting found the error 9 relates to changing Sheet1 to "Weighting Table"; I used 'Weighting Table' which it doesn't like, Weighting is fine.

Curious to know what the answer would be for 2 word WS names- maybe simply changing the ws name might fix it on the actual WB - I'll see if it works ok

Thanks for your time

Mel
 
Upvote 0
Formula as per the below, note the single quotation marks
Cell Formulas
RangeFormula
D3D3=IF(C3<>'weighting Table'!C3,"weighting Table!$c$3","")


Do the same in the sub if that is two words, in the below the sheet with the formula is called "And again"

VBA Code:
Sub jumpToCell()
    Dim aAddress() As String
    aAddress = Split(['And again'!D3].Text, "!")
    Application.Goto Reference:=Worksheets(aAddress(0)).Range(aAddress(1))
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,867
Messages
6,175,069
Members
452,611
Latest member
bls2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top