# Passing Cell Value Into VBA Function?

May 6, 2014
I want to select a cells value from a Data Validation Box ( Achieved ) then based off of that selection ( value ) pass it into a slicercache, as follows :

Select a value from a drop down list in cell A1 Based on selection update slicercache by passing the value of A1 to it

So far i have : The bold is the part where i need to replace with whatever is selected from cell A1. Must be possible ...

With ActiveWorkbook.SlicerCaches("Slicer_Contract_15")

.SlicerItems("BRUTE").Selected = True

End With

Would be best if this code ran on cell A1 click ...

View 1 Replies
ADVERTISEMENT
Mar 2, 2009

I'm using Excel 2003 and I'm trying to make my LINEST function and the source cells for a scatter graph use the cell range specified in another cell in the document. I figured out that it was simple to do the following:

I250 contains: 10, shows: 10

I251 contains: ="I"&I250, shows I10

But I can't figure out how to do this in a larger function. I've tried a few things but none of them work. Here is an example where I want it to do a LINEST with y values in cells Ja - Jb and x values in cells Ia - Ib (where a and b are integers specified in cells I250 and J250 respectively):

View 9 Replies
View Related
Feb 7, 2014

It seems to work. Are there any problems with it? Is there a better way?

Code:

Function myRangePassExample(RangeA As Range) As Double

Dim ArrayA As Variant, ArrayB As Variant

Dim iLo As Integer, iHi As Integer, i As Integer

Dim jLo As Integer, jHi As Integer, j As Integer

Dim Sum As Single

ArrayA = RangeA.Value

iLo = LBound(ArrayA, 1)

iHi = UBound(ArrayA, 1)

[code].....

View 9 Replies
View Related
Nov 25, 2008

This might be a dumb question but is it possible to pass a boolean variable as a parameter to another function?

I have the following code that produces some compile error ("expected ="):

View 2 Replies
View Related
Jul 15, 2014

I'm trying to pass a multidimensional array to a function that I have defined, but I receive an error about an object mismatch. Here is what I have in regards to the array and function. What should I change?

Code:

Dim diffArray() As Integer

Dim countArray() As Integer

Redim countArray(count,2)

diffArray = getRunningSum(countArray)

End Sub

Public Function getRunningSum(ByRef countArray() As Integer) As Integer()

'Code here......

End Function

View 9 Replies
View Related
May 13, 2009

I have a function which i am using as part of a macro. The macro itself works fine and locates a search string i type into an input box across ALL worksheets in an excel doc. However, i have a function which takes the results and prints it to the front page, but when i try to use the worksheet name which has been passed to the function, i get the error: Run-time error '42': object require. this happens when i get to a line of code which says

View 2 Replies
View Related
Jul 9, 2009

Passing an entire dimension of an array to a function....

View 13 Replies
View Related
Sep 28, 2007

I will post my code and point out where the error occurs.

Sub GetProd()

'Averages Daily Production over the month

Dim WellRange, MonthRange As Range

Dim Month_ As Integer

TotWells = Sheets("R").Cells(1, 2)

TotMonths = Sheets("R").Cells(1, 4)

Month_ = Sheets("R").Cells(2, 4)

For Flag = 1 To TotWells

Set WellRange = DefineWellRange(Flag)

WellRange.Select <-----Used this To check If Range gets passed back properly.

For c = 1 To TotMonths

Set MonthRange = DefineMonthRange(Month_, (WellRange)) <---Error Here

Next c

Next Flag

End Sub ...

So the error occurs in the main "GetProd" Sub at the point when it calls the DefineMonthRange Function, its a 424 "Object Required" Error. Just before this is called I have a "WellRange.Select" which I was using to make sure that WellRange is in fact a range, and it does select the appropriate area.

So My question is of course, why I get an object error even though I am passing a range to a function which is expecting a range?

View 4 Replies
View Related
Feb 13, 2014

see the attached. The array formulas in B2:C3 are operating on the table in E2:F5. These are quite simple formulas as you'll see.

The formula in B3 is identical to that in B2 apart from the fact that the first array is wrapped in an IFERROR (there are actually no errors in the array, but that's not the point).

As you can see, the formula in B2 evaluates E3 as ="", and so the corresponding entry in F3 is ignored in the sum. Quite normal.

However, the identical formula, but with an IFERROR first acting on Array1 now evaluates the same cell as <>"", and so F3 is this time included in the sum.

What action has the IFERROR had on this array? Clearly, E3 is not an error, so a "blank" should still be passed, unchanged, to the function.

I have included identical versions of the formulas, but with Named Ranges replacing the actual range references. The reason is that, with these versions, if you step through the formulas with Evaluate Formula, for example with the formula in C2, you can see that Excel "temporarily interprets" the blank in E3 as a zero (this is nothing new, though I've never fully understood why it can't display a "" here as well) before then equating this "zero" as being a "blank".

Perhaps somehow, after being passed to the IFERROR, this "zero" is processed so that, when passed on, even though it still “displays” as a zero, for some reason it is no longer considered equivalent to "".

View 8 Replies
View Related
Dec 4, 2012

Passing Named Range into User Defined Function

MrExcel.com | Excel Resources | Excel Seminars | Excel Products mcm91201

Depending on time of day and computer I am sitting in front of I am using:

WinXP Pro SP2 with Excel 2003

Win7 Pro SP2 Excel 2007

Win7 Pro SP2 Excel 2010 on PC

Win7 Pro SP2 Excel 2010 on Mac Mini running Boot Camp

OSX Excel for Mac 2011

I have only tried this on Win7 Pro SP2 Excel 2007 but need it to work on all.

I enter the values 0, 1, 2 ... 89, 90 in cells A1 to A91

I select A1:A91 and name the range 'angle'

I create a user defined function:

Public function sindeg(value As Double) as Double

sindeg = sin(worksheetfunction.radians(value))

end

I want 'value' for the function in a cell to be replaced by the corresponding value in the same row (or column) in the named range 'angle'. For example (using commas as column separators). This works for Excel functions like sin, cos, radians, etc.

********** Worksheet Contents **********

A1 = 00, B1 = sin(radians(0)), C1 = sin(radians(A1)), D1 = sin(radians(angle)), E1 = sindeg(0), F1 = sindeg(A1), G1 = sindeg(angle)

A2 = 01, B2 = sin(radians(1)), C2 = sin(radians(A2)), D2 = sin(radians(angle)), E2 = sindeg(1), F2 = sindeg(A2), G2 = sindeg(angle)

A3 = 02, B3 = sin(radians(2)), C3 = sin(radians(A3)), D3 = sin(radians(angle)), E3 = sindeg(2), F3 = sindeg(A3), G3 = sindeg(angle)

......

A91 = 90, B91 = sin(radians(90)), C91 = sin(radians(A91)), D91 = sin(radians(angle)), E91 = sindeg(90), F91 = sindeg(A91), G91 = sindeg(angle)

Column A = input. Columns B, C, D, E and F all calculate the same value by row. Column G fails with a #VALUE. In row 1 the value of angle[1] = 0 therefore column D = C = B = sin(0) = 0. In row 2 angle[2] = 1 therefore B = C = D = 0.017452

How can I get the user defined function sindeg(value) in column G to accept the named range variable 'angle' like the Excel function radians(value) accepted it in column D?

This functionality should work horizontally as well as vertically. For example enter 'angle' A1 to CM1 then have sindeg(angle) filled from A2 to CM2. It should also work in the case where the named range 'angle' is a single cell.

I am sure that this is a simple variable type definition problem in my user defined function: should the input variable be defined as type Range? Or something more exotic?

The brute force approach is to have the function determine the input value by passing in the named range, working out dimensions, calculating offset between the cell the function is in and top (left) of named range, then counting down (right) to pick the correct value. However I cannot see adding all that code to EVERY function. Occam's Razor says there has to be an easier way since Excel built in functions seem to do it readily.

View 8 Replies
View Related
Jul 25, 2006

code to pass a range say (A1:A3) as an arguement among other arguements to a custom function and then reading this as an array inside the custom funtion

View 4 Replies
View Related
Aug 11, 2006

I have the following codes in my spreadsheet, they are pretty much the same and both used to work perfectly. However, now the first code returns a Type Mismatch '13' on line beginning mynum= and the second one works perfectly.

Sub addtasks()

Application.DisplayAlerts = False

myrow = Cells. Find(" Total P&C Estimate").Row - 3

mycell = Cells(myrow, 2)

mynum = Right(mycell, Len(mycell) - InStr(mycell, "#")) + 1

With Range(Cells(myrow, 2), Cells(myrow + 2, 2))

.EntireRow.Copy

.EntireRow.insert Shift:=xlDown

End With

Application.CutCopyMode = False

Cells(myrow + 3, 2) = "Task#" & mynum

Application.DisplayAlerts = True

End Sub

View 8 Replies
View Related
Jun 6, 2014

I'm trying use a worksheet that contains named ranges as the source in a macro that will dynamically create pivot tables. The named range could be anything so it can't be hardcoded in the vba and has to come from the worksheet.

In my example, the string strI = "policy" which I know because the msgbox displays the string.

Code:

Dim strI As String

strI = Chr(34) & rngIn.Cells(1, 4).Value & Chr(34)

MsgBox strI

Application.Goto (strI)

But when I try to use strI in place of a hardcoded named range, it always gives me (Reference is not valid) error using the string variable in the following code but never when it's hardcoded in as "policy".

Code:

'This errors out

Application.Goto (strI)

'This works

Application.Goto ("policy")

View 2 Replies
View Related
Apr 8, 2008

I have a formula which is using named ranges-

=SUMPRODUCT(xxx-30,Ship_30)/SUM(Ship_30)

where Ship_30 name references the following-

='sheet 1'!$C$20:$C$38

The value of 20 is static, the value of 38 is dynamic. Each new time period adds another value (i.e. 39,40,41)

Having to edit each name range reference in the name manager is not acceptable since there is potentially over 30 to edit each month.

I would like to edit a cell which would have contained the value 38 (or C38) with a new value of 39 (or C39) so the range reference would be updated with the new value.

The goal would be to have a couple of rows with the following that could be edited with new values

View 9 Replies
View Related
Jul 26, 2012

how to remember or pass the place where the user doubleclicks. I am trying to make a form show up when a certain range is chosen. For now this range is static, but it will be dynamic in the future. Once the user double clicksa cell in this range, the form pops up.

Code:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Not Application.Intersect(Target, Range("M4:AF23")) Is Nothing Then

Cancel = True

[Code]....

View 3 Replies
View Related
Oct 19, 2013

Not sure if possible but im trying to pass a range to a formula in excel from another cell.

[URL] .......

There is a sum formula in range B5 that sums the range (B9:13). I would like to have a formula in cell D5 that passes the new range in cells(D9:D13) to the formula in cell B5 and give the result in cell D5. Is this possible using Excel functions or would i need to create some sort of user defined function for this?

View 9 Replies
View Related
Nov 28, 2006

I am having a problem with a run time 13 error on a variable assignment. The variable is being assigned a value from a cell that contains a formula, and I am suspicous that perhaps the mismatch is coming thru this. The de-bugger stops on:

Area = ActiveSheet.Cells(count + 41, 8). Here is the complete code (with all my poor coding skills!)

Sub Reservoir()

Dim Inflow As Single

Dim Withdrawal As Single

Dim Evap As Single

Dim Area As Single

Dim Spill As Single

Dim Stor_1 As Single

Dim Stor_2 As Single

Dim count As Integer

Dim max As Integer

max = ActiveSheet.Cells(9, 4)

For count = 1 To max * 12

Stor_1 = ActiveSheet.Cells(count + 40, 11)

Inflow = ActiveSheet.Cells(count + 41, 4)

Withdrawal = ActiveSheet.Cells(count + 41, 6)..........................

View 2 Replies
View Related
Apr 10, 2013

I have been searching for hours trying to find a solution to no avail. What I need to do is make sure that when I press a button on a form, the value in a combo-box should be populated as "text format" not as a number. The reason for this is that when my numbers start with a 0, the leading zero gets dropped and I can't run the rest of my code as the items don't match...

I have tried substituting the "Value" to "text" in : Me.cboItem.value and have tried for hours trying to name dim variables as a String and pass this into the cboItem...with no luck...

VB:

Private Sub CommandButton1_Click()

Dim iRow As Long

Dim ws As Worksheet

Set ws = Worksheets("Order")

'find first empty row in database

iRow = ws.cells.Find(What:="*", SearchOrder:=xlRows, _

[Code]...

The below populated the combo box cboItem:

VB:

Private Sub OptionButton1_Click()

With Me.cboItem

.Clear

.List = Application.Transpose(Worksheets("Item List").Range("List1"))

End With

End Sub

View 1 Replies
View Related
Sep 13, 2006

how to pass a cell content into a formula as a named range? For instance, I have 3 named range (Budget, Actual, Forecast) and I use the named range in vlookup formulas. But instead of using the named range in the formula directly, I want to refer to a cell where I can type in which named range I want to use. Can this be done? I am attaching a sample spreadsheet to clarify what I mean.

View 6 Replies
View Related
Mar 14, 2008

I want to know how to index a drop down list to be used for calculations.

Up until now I was using

Range("B1").Select 'the cell where the drop down list is

ActiveCell.Formula = SiteID 'site id

But for whatever reason it trims a trailing zero from SiteID when setting it in the second line. SiteID is a VBA string.

Indexing it would work, as would preserving the zero so help on either one is appreciated. Auto Merged Post Until 24 Hrs Passes;Correction, I lied. I was using this line to set it.

Range("B1").Select 'the cell where the drop down list is

ActiveCell.FormulaR1C1 = SiteID 'site id

View 6 Replies
View Related
Mar 20, 2009

Another interesting dilemma to solve. Using this formula:

View 2 Replies
View Related
May 14, 2013

Function Haversine has correct value in debugger but in cell it has the same value as Haversine2. Is this a known bug?

Public Function Haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double

Dim temp As Double

[Code]....

View 9 Replies
View Related
Jul 17, 2008

I have created a List in excel of various tasks. (See attached example.) Each row contains one cell with a function which equals the cell above it and to the right. So the function for cell C3 would be D2. The only importance is that the cell mirrored is always the cell one above and to the right. However, now I would like to be able to rearrange this List, but the one up and to the right function will now be all over the place.

Is someone aware of a function which will target a relative location instead of an exact cell?

View 13 Replies
View Related
Mar 2, 2008

I have been looking around have this much code from this site, modified. What I am trying to do is calculate numbers by a position with cells D through K having numeric values. I have 'hardcoded' the cells (D2, E2, etc in the code below), but in reality I only want the current row (so if the formula is on the 2nd row, I want D2, if it is on the 3rd row, I want D3).

My problem is obviously the formula isn't working because I am not correctly tying back to the spreadsheet (Positioncalc.xls). When I put the formula in the spreadsheet it works, but in my script I get 0 everytime.

My Script:

Function Position(rCell As Range, Optional RightPosition As Boolean)

Dim vResult

Select Case rCell.Text

Case "QB"

vResult = (2*D2) + (2*E2) + (2*F2) + (4*G2) + (2*H2) + (1*I2) + (4*J2) + (3*K2)

Case Else

vResult = "Invalid Position"

End Select

If RightPosition = True Then

Position = vResult

Else

Position = "Position not valid"

End If

End Function

So, when I put =Position(A2,True) I expect to see the formula results of those cells calculated based on the position (QB, HB, etc with their unique formulas).

My next challenge after this is to highlight certain cells based on the Position. So if A2 = QB, I want cell D2 boldface and Red, etc. I have seen some scripts on colors and such here, so I might be able to figure it out.

View 9 Replies
View Related
Jul 7, 2006

How do I pass values between procedures? I have two command buttons in the worksheet.

If I have a procedure A that asks user to input a value "x", when a command button is pressed.

Then another command button is pressed that passes the value onto another procedure B, which iterates a msgbox as many times as the number x,

How do I pass that value x from one procedure to another?

View 7 Replies
View Related
Apr 27, 2007

I am building a tool that uses Pivot Tables (so I can't use track changes) and I have found cool code that will insert timestamp and username in the comment when a change occurs. But I need to modify the code to also take the old value and put it into the comment as well. Here is the code to register the change into the comment:

View 11 Replies
View Related
Oct 12, 2007

I'd like to know how to send a different value to a cell based on what is selected in the combobox. Like a vlookup, but from within the box.

Something like :

View 13 Replies
View Related
Jul 19, 2009

Passing Array to VBA. I have the following

View 2 Replies
View Related
Dec 17, 2009

I think this is a relatively easy issue, but I don't know how to do it. I call the Sub SortRange in another Sub SortDeliver. A range (ran5) is defined in Sub SortRange that I also need to use in Sub SortDeliver. How do I pass the range or get the second sub to recognize the range?

View 5 Replies
View Related
Dec 28, 2009

I've a userform (UserForm1) that on closing will open one of 5 other userforms. Which of the userforms that opens will be determined at runtime, depending on an option a user has previously selected. The name of the userform is held in a cell in a workbook and is opened as follows:

VBA.UserForms.Add(sheets("Sheet1").Range("A1")).Show

where the value in Sheets("Sheet1").Range("A1") is "UserForm3"

UserForm1 contains TextBox1. How can I pass the value in TextBox1 to a textbox in UserForm3 when the name of UserForm3 is held as a variable in a cell in a worksheet?

View 9 Replies
View Related