Opening a second workbook breaks the UDF in the first workbook

rtemen

New Member
Joined
Sep 23, 2013
Messages
36
I have a workbook, Book-A, that I have defined a UDF.
When I open a second workbook, it breaks the UDF on the first workbook.

How do I stop this?

Rich
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Correct the code in the UDF. :)
 
Upvote 0
Correct the code in the UDF. :)

I have no idea what to 'Correct'. Here is my code, please let me know what to correct.

Code:
Function myPrice(myPart As String, myVol As Integer)
    
    Application.Volatile
    
    Dim myCol As Integer
    Dim myCavity As Integer
    Dim myCycleTime As Integer


    myCol = Application.WorksheetFunction.Match(myPart, Range("Item_Name"), 0)
    myCavity = Application.WorksheetFunction.Index(Range("Molding_Data"), Application.WorksheetFunction.Match("Cavities", Range("pRows"), 0), myCol)
    myCycleTime = Application.WorksheetFunction.Index(Range("Molding_Data"), Application.WorksheetFunction.Match("Cycle Time", Range("pRows"), 0), myCol)


    myPrice = myCavity * myCycleTime / myVol
    
End Function
 
Upvote 0
I was trying to get you to post the code - hard to fix something you can't see...

You need to qualify your Range objects with the correct workbook and worksheet or, preferably, pass the ranges as arguments to the UDF.
 
Upvote 0
I was trying to get you to post the code - hard to fix something you can't see...

You need to qualify your Range objects with the correct workbook and worksheet or, preferably, pass the ranges as arguments to the UDF.

I am rather new to this, so here is my next attempt. The line set wb = thisworkbook doesn't work.

Code:
Function myPrice(myPart As String, myVol As Integer)
    
    Application.Volatile
    
    Dim myCol As Integer
    Dim myCavity As Integer
    Dim myCycleTime As Integer
    Dim wb As Workbook
    Dim ws As Worksheet
    
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet1")
    
    With ws
    
    myCol = Application.WorksheetFunction.Match(myPart, .Range("Item_Name"), 0)
    myCavity = Application.WorksheetFunction.Index(.Range("Molding_Data"), Application.WorksheetFunction.Match("Cavities", .Range("pRows"), 0), myCol)
    myCycleTime = Application.WorksheetFunction.Index(.Range("Molding_Data"), Application.WorksheetFunction.Match("Cycle Time", .Range("pRows"), 0), myCol)
    
    End With


    myPrice = myCavity * myCycleTime / myVol
    
End Function
 
Upvote 0
It looks like I figured it out.
I had Googled the subject and found many examples showing the 'Set wb = ThisWorkbook'.
I finally tried
Set wb = Workbooks("AdvancedQuote.xlsm")
and now all seems well.

Rich
 
Upvote 0
ThisWorkbook refers to the workbook containing the code. If that's the workbook you want to refer to, it is better to use that than its name, in case that changes.
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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