VBA code to jump to a cell using reference in current cell

gmcgough600

New Member
Joined
Nov 21, 2017
Messages
33
Office Version
  1. 365
Hi,

Looking for some help, if I have a simple formula like =A1 in cell B1 I want to write some VBA code so that if I select B1 then run a macro it will then select A1. Slightly more complex as what I have in B1 actually refers to another workbook e.g. ='[book1.xlsx]sheet1'!A1 and I want to select A1 in the other workbook (which will be open). Is this possible?

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Put the following code in the events of your sheet.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim rng As Range
  Dim bk As String, sh As String, cl As String
  If Target.CountLarge > 1 Then Exit Sub
  If Target.Address = "$B$1" Then
    bk = Split(Target.Formula, "[")(1)
    bk = Split(bk, "]")(0)
    sh = Split(Target.Formula, "]")(1)
    sh = Split(sh, "!")(0)
    cl = Split(Target.Formula, "!")(1)
    Set rng = Workbooks(bk).Sheets(sh).Range(cl)
    rng.Parent.Activate
    rng.Select
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0
Put the following code in the events of your sheet.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim rng As Range
  Dim bk As String, sh As String, cl As String
  If Target.CountLarge > 1 Then Exit Sub
  If Target.Address = "$B$1" Then
    bk = Split(Target.Formula, "[")(1)
    bk = Split(bk, "]")(0)
    sh = Split(Target.Formula, "]")(1)
    sh = Split(sh, "!")(0)
    cl = Split(Target.Formula, "!")(1)
    Set rng = Workbooks(bk).Sheets(sh).Range(cl)
    rng.Parent.Activate
    rng.Select
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
Thanks, I'd like the code to work for any cell with a formula similar to B1, not just for B1. Is that possible?
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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