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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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