How do I change the code to reflect the result of Cell H1 dynamically?

ojmeier

New Member
Joined
Nov 5, 2017
Messages
32
I am trying to change the following code to open an existing WB depending on the result of cell H1. :

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Target.Address(0, 0) = "H1" And Target <> "" Then

Cancel = True

If Evaluate("isref('" & Target & "'!a1)") Then

Workheets(CStr(Target)).Select

'for opening new WB line below would be working. but it is not depending on result of H1!!

'Workbooks.Open "D:\ExcelVBA\341.xlsx"

Else

MsgBox "Sheet not found"

End If

End If

End Sub


Thanks
 
Try again with this:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  If Target.Address(0, 0) = "H1" And Target <> "" Then
    Dim sName As String
    Cancel = True
    If Evaluate("isref('" & Target & "'!a1)") Then
      Sheets(CStr(Target.Value)).Select
    Else
      MsgBox "Sheet not found"
    End If
    '
    sName = "D:\ExcelVBA\test\" & Target.Value & ".xlsx"
    If Dir(sName) <> "" Then
      Workbooks.Open sName
    Else
      MsgBox "File not found"
    End If
  End If
End Sub
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Thanks again for your input and solution. I tried it and it worked.. the only snafu was that I got the message "Sheet not found" and after clicking "OK" on the message it opened the WB.
 
Upvote 0
I took out some of the lines as per below, It works perfectly well. Thank you very much for your help. You helped an old guy to feel good. Thanks again.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address(0, 0) = "H1" And Target <> "" Then
Dim sName As String
Cancel = True
'If Evaluate("isref('" & Target & "'!a1)") Then
' Sheets(CStr(Target.Value)).Select
'Else
'MsgBox "Sheet not found"
'End If
'
sName = "D:\ExcelVBA\test\" & Target.Value & ".xlsx"
If Dir(sName) <> "" Then
Workbooks.Open sName
Else
MsgBox "File not found"
End If
End If
End Sub
 
Upvote 0
I am so impressed by your engagement for the task. Thanks again and all the best.... Let me know if I can do anything for you.
 
Upvote 0

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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