# Define Range in Function

#### mst3k4L

##### Board Regular
I recently changed a sub to a function, and the function no longer works.
Code:
``````Function Bo(p) As Double
Dim BoCoeff As Variant

' Start at bottom and looks for last filled cell
Sheet2.Range("A9", Range("A65536").End(xlUp)).Name = "RangeA"
Sheet2.Range("B9", Range("B65536").End(xlUp)).Name = "RangeB"

BoCoeff = Evaluate("LinEst(RangeB, RangeA^{1,2,3})")
Bo = p ^ 3 * BoCoeff(1) + p ^ 2 * BoCoeff(2) + p ^ 1 * BoCoeff(3) + BoCoeff(4)
'    Below only used in debugging
'    Bo = p ^ 3 * Sheet2.Range("A3") + p ^ 2 * Sheet2.Range("B3") + p ^ 1 * Sheet2.Range("C3") + Sheet2.Range("D3")

End Function``````
If I comment out the definition of RangeA and RangeB and use the new definition of Bo it works, so how should I define RangeA and RangeB?

### Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Why do you need to create the named ranges through vba? You could simply create dynamic ranges, which you then refer to in your code.

You could do it like this, but Excel will not see a dependency on Sheet2, so the results will not update if it changes:
Code:
``````Function Bo(p As Double) As Double
Dim sY          As String
Dim sX          As String

With Sheet2
ad = .Evaluate("LinEst(" & sY & ", " & sX & "^{1,2,3})")
End With
End Function``````

For that reason, I'd do this instead:

Code:
``````Function Bo(p As Double, rX As Range, rY As Range) As Double
Dim sY          As String
Dim sX          As String

ad = Evaluate("LinEst(" & sY & ", " & sX & "^{1,2,3})")
End Function``````
... called as

=Bo(A1, Sheet2!A9:A65536, Sheet2!B9:B65536)

I didn't test either one.

Why do you need to create the named ranges through vba? You could simply create dynamic ranges, which you then refer to in your code.
I tried that idea, but I was still having problems.

Code:
``````Function Bo(p) As Double
Dim BoCoeff As Variant

'Using predefined dynamic ranges
BoCoeff = Evaluate("LinEst(Bo_Data, Bo_Pressure^{1,2,3})")
Bo = p ^ 3 * BoCoeff(1) + p ^ 2 * BoCoeff(2) + p ^ 1 * BoCoeff(3) + BoCoeff(4)

End Function``````
With Bo_Pressure and Bo_Data defined as:

Code:
``````=OFFSET(Data!\$A\$8,0,0,COUNT(Data!\$A\$8:\$A\$65536),1)
=OFFSET(Data!\$B\$8,0,0,COUNT(Data!\$B\$8:\$B\$65536),1)``````

I double checked these dynamic ranges, and they do appear to give accurate areas, but I still get an "#VALUE!" error. What am I missing?

Replies
10
Views
273
Replies
5
Views
275
Replies
10
Views
641
Replies
2
Views
268
Replies
2
Views
371

1,202,987
Messages
6,052,932
Members
444,616
Latest member
novit19089

### 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.

### Which adblocker are you using?

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

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