VBA open worksheet based on formula value in a cell

blossomthe2nd

Active Member
Joined
Oct 11, 2010
Messages
450
Hi All

I am using a formula in excel cell (E13) to determine what spreadsheet should be opened.

Then I have a Drop down in Cell D13 that either triggers the open or not . See code below but I am getting an error on the varCellValue - any guidence would really be appreciated

Thanks


A

HTML:
Sub Worksheet_Change(ByVal Target As Range)
 
    Dim WatchRange          As Range
    Dim IntersectRange      As Range
    
    Dim varCellvalue As Long
    varCellvalue = Range("E13").Value
    Set WatchRange = Range("D13")
    Set IntersectRange = Intersect(Target, WatchRange)
    
 If Not (IntersectRange Is Nothing) And (Target = "Capitalisation") Then
        Workbooks.Open Filename:= _
        "S:\Business Support\" & varCellvalue & ".xls"
        
  End If
    On Error GoTo 0
    

If Not (IntersectRange Is Nothing) And (Target = "Part Payment") Then
        Workbooks.Open Filename:= _
        "S:\Business Support\" & varCellvalue & ".xls" 
       
 End If
    On Error GoTo 0

End Sub
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,389
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
What is the error? What is varCellValue when you get the error? varCellValue is dim'd as a long - do your workbooks really have just a number as a file name (e.g. 2014.xls)?
 

blossomthe2nd

Active Member
Joined
Oct 11, 2010
Messages
450
When I run the debugger its like the varCellValue is 0 rather than the text result of the formula
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,389
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
When I run the debugger its like the varCellValue is 0 rather than the text result of the formula
Can you answer my other questions? If the formula is returning text then you don't want to dimension varCellValue as a Long.
 

blossomthe2nd

Active Member
Joined
Oct 11, 2010
Messages
450
Hey Joe

Sorry I was away for weekend - yes it is giving text back - so I changed varCellValue to string and the code worked -

Thanks for your help

A
 

Watch MrExcel Video

Forum statistics

Threads
1,123,400
Messages
5,601,467
Members
414,452
Latest member
Dannysamworth

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
Top