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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
But what do you need?
Do you want to open a book with the data that is in the cell?
 
Upvote 0
But what do you need?
Do you want to open a book with the data that is in the cell?
Thanks DanteAmor. Yes, I would like to open on DoubleClick a n existing workbook with the result of cell H1. In the present code I can open a worksheet within the workbook. In other words once the result of cell H1 is determined I would like to go an open a Workbook named exactly as the result of H1. (There will be about 2,000 workbooks with a possible number combination in H1). Thanks for your help.
 
Upvote 0
Try 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\" & Target & ".xlsx"
    If Dir(sName) <> "" Then
      Workbooks.Open sName
    Else
      MsgBox "File not found"
    End If
  End If
End Sub
 
Upvote 0
Thank you DanteAmor. I got an compile error message. Did I mess up?
 

Attachments

  • StartB.PNG
    StartB.PNG
    50.7 KB · Views: 2
Upvote 0
Sorry, change the line for this:

sName = "D:\ExcelVBA\" & Target.Value & ".xlsx"

Change "D:\ExcelVBA\" by the name of your folder
 
Upvote 0
thank you DanteAmor. the file(s) I want to open are in the Folder "D:\ExcelVBA\"
All files are in that very same folder.
The line
sName = "D:\ExcelVBA\" & Target.Value & ".xlsx" should be the right target folder.
thanks. below the "startpage where I want to take H1 as the cell to determine the targeted Workbook
 

Attachments

  • topWS.PNG
    topWS.PNG
    19.1 KB · Views: 3
Last edited:
Upvote 0
below the "startpage where I want to take H1 as the cell to determine the targeted Workbook
I did not understand that part.
The macro you asked for is to open a book, the name of the book is in cell H1, so double click on cell H1 and open the book.
The macro opened the book, does it work for you already?
 
Upvote 0
I made a new folder and in that folder (D:\ExcelVBA\Test) I have the Start page with that H1 cell and in the same folder I have the WB "341.xlsx".When I double click H1 I get the same error message. Where am I wrong? I am sorry to be so dumb ...
 

Attachments

  • wewstarter.PNG
    wewstarter.PNG
    84.5 KB · Views: 1
Upvote 0

Forum statistics

Threads
1,215,515
Messages
6,125,277
Members
449,220
Latest member
Excel Master

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