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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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)?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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