Pass Function Result Between Subs

Oct 11, 2006

I have a simple/dumb question... How do you "capture" a value that is returned by a custom function.

I have tried searching the forums for this & I know that I should by all rights know how to do this by now... but I just can't figure it out.

Here is a Function that I copied from Ktrasler in this thread:
Week Numbers

Public Function MyWeek(DateArg As Date) As Byte

Const BaseDate = "30/12/2001"

DateArg = CDate(DateArg) - (Weekday(DateArg) - 1)
MyWeek = Int(((DateArg - CDate(BaseDate)) / 7) Mod 52)
If MyWeek = 0 Then MyWeek = 52

End Function

I know how to pass variables to Functions, and tried this one out & it worked splendidly for my needs, but how do I take the value of "MyWeek" and use it in the subroutine that I am calling it from?

View 3 Replies


ADVERTISEMENT

Amending VBA Sub / Function To Pass Results To Other Subs

Feb 5, 2014

Basically someone has coded a VBA script to pull a users details from AD when a button is clicked, using the staff id as the search criteria.

I've amended the criteria so it uses Environ$(USERNAME) to find details of the current logged on user, but i am struggling to get the resulting information out of the Sub to be used in other Subs! It currently just fills a cell with the required info.

View 1 Replies View Related

Pass Arguments Between Subs

Jul 11, 2007

I am still learning and was wondering what is wrong with this. I have cut some of the code out as it pretty long. This is my main procedure:

Public Sub cal_parameters_Click()
'istat(i) = 0 bonded
' = 1 slip
' = 2 soil yielded

Dim istat(1 To 999) As Single

Call secondary_loop 'calls subprocedure secondary_loop
End Sub

It then calls secondary_loop:................

View 6 Replies View Related

Pass Workbook Variable Between Subs

Jul 11, 2007

This is the code after editing to make it more clear

Public Sub 1()
BookA= activeworkbook. name
BookB=Application.Workbooks.Add
Workbook(BookB).activate
End Sub

Public Sub 2()
BookB=Activeworkbook.name

With BookB. sheets(1)
.range("A1")=BookA.sheets(1).range("B1")
End Sub()

At the end of public sub 1, BookB is the active workbook. What I want to do in public sub 2 is to copy some data from BookA to BookB. Unfortunately, when moving from public sub 1 to public sub 2, BookA needs to be defined again. The code above is the code that I use in my add-ins. I figured out for non add-ins code I can define BookA with thisworkbook.name when BookB is active as I before work with BookA. This does not apply for add-ins as thisworkbook will refer to my add-ins code. Is there anyway of keeping definition of BookA is constant from one public sub to another public sub? This is simplified code. In fact, I can't merge public sub 2 with public sub 1 due to some reason which I don't say it here.

View 6 Replies View Related

Series Of Subs Called From Function

Apr 15, 2013

I have a series of procedures I am calling from a function that each run fine when executed individually. These procedure are titled: DataValidation, Test1, Test2

The function will not run past Test2- I do not get the msgbox "Stage 2"

Code:
Public ArrayD
Public ArrayD2 As Variant
Public RngD3 As Range

Public Function Test(arg2 As Range)

DataValidation arg2

[Code] ............

View 4 Replies View Related

Pass Formula Result To Variable

Jul 26, 2007

I have come up with the following formula to pull sums of data by month. I am using this because it is inconvenient to use the DSUM formula (I can't put all the criteria together in a simple way):

{=SUM(IF('Sheet1'!$B$2:$B$20="Person1",IF( DATE(YEAR('Sheet1'!$A$2:$A$20),MONTH('Sheet1'!$A$2:$A$20)+1,)=C2,'Sheet1'!$D$2:$D$20,0),0))}

I would like to, in a macro, assign a variable to this result - but cannot figure out the VBA code for it.

View 5 Replies View Related

Pass The Result To A Sub Which Takes An Array Of Dates As A Parameter

Dec 27, 2008

I delcare an array of dates and assign it with a function that returns an array of dates. Then I want to pass the result to a sub which takes an array of dates as a parameter. However, I am getting a type mismatch error on this line

View 2 Replies View Related

Get Range To Pass Into Function

Jun 11, 2009

I am attempting to call a Function from a cell and pass it a range and an Integer. The Integer works fine, but I cannot get the Range to pass into the function correctly.

View 8 Replies View Related

Pass Value From Function To Procedure

Aug 11, 2006

If I have code like this

Private Sub cmdHex_Click()
Dim HexNum As String
HexNum = Me.txtHexVal
'Call a function
ToBinary (HexNum)
'Now do something else
End Sub

And the function ToBinary creates a value called BinVal. How do I pass it back to my subprocedure to do something with it.

View 4 Replies View Related

Pass TextBox To Custom Function (UDF)

Mar 10, 2008

I have a simple function defined in one of my worksheets (Sheet1):

Function AddFuel(fuel As String)
MsgBox fuel
End Function

How would I be able to call this from a form button event?

Private Sub CommandButton1_Click()
Sheet1.AddFuel(TextBox1)
End Sub

Everytime I try running this code I receive the error: Run-time error '1004'; Application-defined or, Object-defined error. I've even tried Application.Run("Sheet1.AddFuel", TextBox1) but still no luck. I think this is a pretty common question but I couldn't find any answer to it on the forums.

View 2 Replies View Related

Pass Variable To Evaluate Worksheet Function

Nov 15, 2013

I understand that the SumProduct doesn't have the same capabilities in VBA as it does in worksheet formulas. I am trying to construct it in an evaluate statement in VBA but the problem is I am using it in a userform, so the conditionals are passed from two combo boxes. Here is the worksheet formula

Code:
=SUMPRODUCT((Table1[Home]=C3)*(Table1[Away]=M3)*(Table1[Goals (H)]>Table1[Goals (A)]))

The values C3 and M3 refer to a cell known as "Home" & "Away". It's comparing to the two teams selected to a table with game stats to show wins and losses. The user form has a combo box list with those teams. Combo Box 1 passes to a variable known as hTeam and Combo Box 2 passes to a variable known as aTeam. I need those values passed through to C3 and M3. Here's what I constructed but get a type mismatch

Code:
Evaluate("=SUMPRODUCT((Table1[Home]=" & chr(34) & hTeam & chr(34) & ")*(Table1[Away]
=" & chr(34) & aTeam & chr(34) & ")*(Table1[Goals(H)]>Table1[Goals(A)]))")

View 4 Replies View Related

Pass Ranges To User Defined Function

Dec 28, 2006

I am trying to write a User Defined Function in VBA to perform a simple two-dimensional table lookup. I have the temperature distribution in a solid given in a table in Sheet2. The first column of the table contains the time values, the first row contains the spacial values (radii), and the intersections contain temperature values. These ranges are named times, radii, and temperatures. In Excel, I can perform the lookup using: =index(temperatures, match(time, times, 1), match(radius, radii, 1))

But how do you do this in VBA? More specifically, how do you deal with passing ranges to the WorksheetFunctions?

Function temperature_ref(time_range As Range, radii_range As Range, temperatures_range As Range, time As Double, raduis As Double)

r = WorksheetFunction.Match(time, time_range, 1)
c = WorksheetFunction.Match(radius, radii_range, 1)
temperature = WorksheetFunction.Index(temperatures_range, r, c)
End Function

This function would be called (from any worksheet) as..................

View 4 Replies View Related

Pass Vlookup Result Of TextBox To Another TextBox

Feb 13, 2008

I am trying to create a user form that has a series of text boxes that will all have a VLOOKUP function in them based off of input from a the first text box. If I can just get the code for the first one, I think I can figure out the rest. We will say that the file that the user form is contained in is 'Agent Administration' and the file that I want the VLOOKUP to pull from is called ' Roster for Auto Population'.

View 7 Replies View Related

Pass The Contents Of A Listbox To A Function To Sort It Into Alphabeticalorder

Mar 3, 2008

I'm trying to do is pass the contents of a listbox to a function to sort it into alphabeticalorder. I get a error saying object required

Sub PopulateAll()
Dim lgLastRow As Long
Dim lgRowCounter As Long
Dim intCounter As Integer
Dim olb As MSForms.ListBox

For intCounter = 0 To UBound(vAllEnv)
Me.lstAll.AddItem (vAllEnv(intCounter))

Next intCounter

Set olb = frmOptions.lstAll.........................

View 9 Replies View Related

Pass Multiple Tables Into Custom Interpolate Function

Jan 14, 2010

I have 5 worksheets each with tables that are formatted the same on each sheet. I have named each table as a named range (ex:filter5tsd15BSF). I have a userdefined function that interpolates values for a single table in a vba module. I can make this function work for a single table by calling the function as =Linterp(filter5tsd15BSF,C12) so I know the function is OK. However, I really rather program this some way that all the tables get passed to the function and then a select case for the conditions for which filter and which TSD (15 or 25) will then select the correct table.

View 2 Replies View Related

Pass Multiple Non-contiguous Ranges Into A User Defined Function

Dec 23, 2009

I am trying to call a function that calculates forecast error (wMAPE). This function needs to be able to handle passing in non-contiguous ranges. I can't seem to figure out how to do that.


Function wMAPE(Forecasts As Range, Actuals As Range, Weights As Range) As Variant
Dim Denominator As Double
Dim Numerator As Double
Dim i As Long
Dim Fcst As Variant
Dim Act As Variant
Dim Wt As Variant

If Forecasts.Cells.Count Actuals.Cells.Count Then MsgBox ("Error: Arrays not same size")
If Forecasts.Cells.Count Weights.Cells.Count Then MsgBox ("Error: Arrays not same size")

Denominator = 0............

View 9 Replies View Related

Run Macro If Result Of IF Function Is True

Dec 5, 2012

I want to run a macro if the result of an IF function is true.

E.g. cell J55 contains =IF(H55>I55,"Goodbye","")

H55 contains =NOW()

I55 contains =DATE(2012,12,31)

Now by changing the dates etc. it prints Goodbye.... simple enough, but what I want is for it to run a macro that selects a range from the previous year's figures and simply changes the fill colour on the range. The macro for this also works fine.

View 3 Replies View Related

Function That Will Show The Result In Column AD

Nov 6, 2008

I have a huge worksheet that contains four character payer code in column D for example: 9081, M897, 0235. I am looking for a function that will show the result in column AD

IF payer code in column D starts with a 9 (9???) - GOVT
IF payer code in column D starts with a 7 (7???) or M (M???) - MNGD
IF payer code in column D starts with a 2 or 0 (zero) – COMM
IF payer code in column D starts with either Z, I, C - PTR

I tried IF function, but it didn’t work for me.

View 3 Replies View Related

Can IF Function Result In Its Answer In A Different Cell?

Jan 15, 2010

Can this Formula work? Where if c2<e5 then its value is a number. The formula below is what i tried but it didnt work

=IF((C2<E5);"c5=value=f5")

View 2 Replies View Related

IF Function To Return No Data As A Result

Nov 29, 2007

I've created a Database using excel which feeds data to a pivot table I created based on it. My objective is to create a new item in this pivot table (Pivot Table > Formulas > Calculated Item) which calculates the average data for the past 6 months.
The formula I used in order to get a result is this:

=IF(((May+June+July+August+September+October)/6)=0;;(May+June+July+August+September+October)/6)

and the problem is that the answer is 0 when there is no data for all 6 months (for example, there is a customer who hasn't baught anything during the past 6 months), the pivot table displays 0, instead of not showing this customer at all. My goal is for the IF function not to return any data if the result is 0, so that the pivot table doesn't show items with no data.
I also tried:
=IF(((May+June+July+August+September+October)/6)=0;"";(May+June+July+August+September+October)/6)

and also using ;" ";

but these formulas return #VALUE! in the pivot table, given that the Data is "SUM OF Sales" and "" is not a number.

How can I get the formula not to show anything at all?

In addition to this, I was wondering if it is possible to make this formula more automated, so that it calculates the average of the past 6 months, taking into account some type of reference month (rather than having to edit the formula each time a new month is analyzed).

View 9 Replies View Related

Result Of Address Function In Formula

Nov 13, 2006

How do you use the result of the "Address" function as ranges for other formulas?
I have 2 cells A1 and A2, each containing an "Address" function to find the start cell and end cell of a range.

A1 shows the result "$J$6" and A2 "$AB$6". These are working fine:

A1: =ADDRESS(ROW($A6),MATCH(HLOOKUP('cleaned up'!$I$2,pivot03!$2:$2,1,FALSE),$2:$2,0),1)

A2: =ADDRESS(ROW($A6),MATCH(HLOOKUP('cleaned up'!$I$5,pivot03!$2:$2,1,FALSE),$2:$2,0),1)


I want to get the average of this range using cells A1 and A2, but Excel treats the formula as an error: =Average(A1:A2) shows the error #DIV/0!

Going back one step and putting the formulas within cells A1 and A2 into the average formula doesn't work at all with a colon:.....................

View 3 Replies View Related

Run Macro Based On SUM Function Result

Jan 27, 2008

i have the formula "= sum(A1:A10)" in cell A4 & would like to call macro when the value changes. The code i'm using below work's if I manually type in a value, but isn't working with the formula.

Private Sub Worksheet_Change(ByVal Target As Range)
' Checks for cell value change
If Intersect(Target, Range("A4")) Is Nothing Then
Exit Sub
' If cell value change, calls msgbox based on criteria
Else
If Range("E4").Value = "C" And Range("A4").Value > "30" Then
Call MsgBox1
End If
If Range("E4").Value = "F" And Range("A4").Value > "38" Then
Call MsgBox2
End If
End If
End Sub

View 4 Replies View Related

Index Function With Multiple Ifs Won't Return More Than First Result

Aug 11, 2014

I am attempting to get an INDEX function with multiple "IF" statements to return more than one result. Building multiple IF statements. That portion of the below formula works. However, I can only get the INDEX formula to return the first, single result.

Formula:
=IFERROR(INDEX(Func_Area,SMALL(IF(Const_Start<=DATE(YEAR($B$2),MONTH($B$2),DAY($B$2)), IF(Cost_End>=DATE(YEAR($C$2),MONTH($C$2),DAY($C$2)),ROW(Func_Area)-MIN(ROW(Func_Area))+1,"")),ROWS($A$4:A4)))," ")

Func_Area,Const_Start,Cost_End are all Named Ranges on Sheet "Proposed". My intention is to return a list of projects on Sheet Test1 for all projects starting construction after a certain date and completing construction before a certain date. I suspect something in my ROWS fuction or ROW-MIN+1 is wrong.

This formula returns the first result in the first row, but every subsequent row is blank...see attached spreadsheet.

View 3 Replies View Related

MATCH Function Returns Unexpected Result

Dec 3, 2013

In the attached file I used =MATCH(TRUE,INDEX($B$2:$B$10="",0)) to retrieve the location of the first empty cell.

When using the "Evaluate Formula" tool, it is clear that the position in the array created by the INDEX function is the 6th. Nevertheless, the final outcome is 9, being the last cell in range.

View 3 Replies View Related

Designating Function's Result As Row Number For Cell?

Feb 5, 2014

I have been trying to get the row number of a cell to be decided by the integer result of a function. My thought was to do something like this .........(D(MATCHxxxxxx))....... With "D" being the column and "MATCHxxxxx" representing some function that results in an integer.

View 3 Replies View Related

Function To Indicate Whether An Expression Returns A Non-zero Result (2007)

Jan 28, 2010

I have this COUNTIFS expression buried in a larger formula:

COUNTIFS(Table[C7],Table[C7]&"",Table[C21],">=1")

and it always returns a value of zero or some positive integer.

I'm now looking for a little "function" that I can wrap around this expression that will:

A) indicate any non-zero result as 1 or TRUE

and

B) indicate any zero result as 0 or FALSE

Something like...

AREYOUNONZERO( (COUNTIFS(Table[C7],Table[C7]&"",Table[C21],">=1") )

I'm sure there's a fairly efficient way to do this... but I'm totally stumped at the moment!

View 7 Replies View Related

Sort Two Columns Of Function Results Using Result Value?

Feb 4, 2013

How can I sort two columns of function results using the result value? Excel is trying to sort on the function in the cells rather than the results themselves.

View 9 Replies View Related

Worksheet Match Function With A Row Count For Result

Mar 20, 2014

I need some code to search "database" sheet column B:B for value "SearchString" and then either select the cell (0, 86) to the right or count the rows so I can move the the cell in the correct row.

View 14 Replies View Related

How To Make The Result Of A TODAY Function Static?

Mar 22, 2006

I have a problem with the today Function. It appears that it changes each day. But that isn't what I want! I'd like to have a funtion that puts the current Date in a field when Data is being added in the Row and then having this date static the next day. Here is what I had so far but I have no clue to make the date static:

=IF(C10>0;TODAY();IF(D10>0;TODAY();" "))

in this case it checks for information in field C10 and D10 and if there is information it will add a Date like 2006-03-22. But new day the field will change to 2006-03-23 and that is not what i want. I want it to stay the same when data is put in and the date is being presented.

View 12 Replies View Related

Function To Accept 2 Criterias And Return Result

Sep 8, 2008

Name

Day

Total
AlanMon400AlanThur600TomMon200TomWed300JamesMon1000Output4002001000

What function/formula can I use if I want that function to accept 2 criterias and returns me a result. Based on the above example, if the criterias are Alan & MOn, it will return 400 and if the criterias are Tom and Mon, it will return 200 and if the criterias are James and MOn, it will return 1000. Bascially, I want a function that can accepts 2 criterias. I do not want to sum up the total of the rows that are associated with one of the criteria i.e Alan. For Alan, I want 400 instead of 1000

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved