# User Defined Function With If Statement Does Not Work / Incorrect Code

Mar 7, 2014I am trying to create the following function but I cannot seem to get it working correctly.

[Code] .....

I am trying to create the following function but I cannot seem to get it working correctly.

[Code] .....

ADVERTISEMENT

Essentially i get a Runtime 5 error on the bold bit of code....

I am trying to define a range .... by using thexlUp function find the last row ( which works ) i then pass this variable into the sort code and get the error its probably very simple to fix, all it does i sort columnA but finds the last cell .... instead of the whole column,,,,

Sub Macro7()

'

' Macro7 Macro

'

' Keyboard Shortcut: Ctrl+e

NumberOfRow = Sheets("Sheet1").Range("A3000").End(xlUp).Row

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear

ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Sheets("Sheet1").Range("A2", Cells(NumberOfRow)), _

SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

[Code] .......

when you type in a builtin function in the formula bar such as =DAY(), a small callout box appears below telling you the syntax - in this case "DAY(serial_number)". Is it possible to achieve this for a user defined function?

I am trying to create a function using the functions of IF and "And", but for any reason I don't know how to add the Add function

My function written in excel will looks like =IF(AND(A80="00",C80<>"8300"),"yes","no")

LOB Cost Center

00 1000

00 2000

00 8300

01 5000

02 8300

Function Allocation(LOB As Integer, CostCenter as Integer) As String

If LOB = 00 And CostCenter <> 8300 Then

Allocation = "Yes"

ElseIf LOB = 1, 2, 3, 4, 5 Then

Allocation = "No"

End If

End Function

I have created a custom add-in with custom functions and am trying to make it user friendly for other users. By running the following macro I can add functions to different categories, but I was wondering if there was a way to do this automatically so that anyone that adds this add-in will have the functions show up in the proper category without having to manually run this macro.

View 6 Replies View RelatedI can't get a simple function to work. I attached a screen shot of my function. It's very simple -- adds two numbers.

View 4 Replies View RelatedI am trying to write a function which return the difference of sum of range A and sum or range B. But I keep getting error with my codes.

Code:

Function deltaPipeline(YTDPplComm As Range, LastYearPpln As Range)

deltaPipeline = Sum(YTDPplComm) - Sum(LastYearPpln)

End Function

I am stepping through a sub in module one that for some reason keeps jumping to a user defined function in another module. I've checked and and the function isn't being called.

View 9 Replies View RelatedI'm coding an UDF.

The main thing is, that if in the choosen cell are a specify value, than in another cell the interior color should change.

I don't know how can I change the interior color in function.

The code, buit it's not working:

Function Alerting(rCell As Range, tCell As Range)

If rCell.Value >= 5 Then

Alerting = " alert"

Cells(tCell.Row, tCell.Column).Interior.ColorIndex = 3

End If

End Function

I just hit F5 and use it in Excel with no problem. This time a person who developed the function had me install it as Add-In. I do not know if it is supposed to give any advantages (versus simply typing function in VBE window + activating it by hitting F5 key), but the function works extremely slowly. Is this Add-In

View 6 Replies View RelatedI have a function in VBA of the type. Function MyFunc(Indx As Integer, k As Long, Rho As Range, A As Range) As Variant .... End Function

which is called as a user-defined function from within the Excel worksheet. When called with the last two arguments being a range (i.e. Result = MyFunc(1,98,A1:A2, B1:B2)) it works fine. However, when I try to directly use an array constant instead of a range (i.e. Result = MyFunc(1,98,{10,11}, {20,30}), it returns a #VALUE error.

I thought I could fix it by redefining the last two arguments as arrays of type double, but this didn't work either (i.e. Function MyFunc(Indx As Integer, k As Long, Rho() As Double, A() As Double) As Variant .... End Function ).

I created an addin that puts a formula in a cell to make use of a UDF in the addin. It works fine.

But when I make changes to my addin, save it as new name, load the new addin (same as the old but with new code for other things) and then open the workbook that had the UDF working, it now changes from

=customUDFfunction(1,2,3)

to

='C:Documents and SettingshomeuserApplication DataMicrosoftAddInsAddinV123.xla'!customUDFfunction(1,2,3)

The code I'm using to create the formula in the cell is

Cells(x, y).Formula = "=customUDFfunction(1,2,3)"

I'm assuming that when I create the formula in the cell it gets tagged somehow with the addin name and location. How to just put the formula with keeping it specific to the addin that created it?

A client has a registration workbook for each student which contains 3 types of worksheets, A) Transcript B) Registration and C) Grade sheets. Each file has only 1 Transcript sheet and 1 to many pairs of Registration and Grade sheets.

I have a GPA user defined function which obviously can only be used on a Grade sheet. I would like to return an Error if the user trys to use the udf on a non Grade sheet.

Currently I am just returning 1/0 but the smart tag returns #Name. Can I control the text displayed somehow?

The below macro(found in VBA and MACROS for Excel) works well but I need a few modifications

Option Explicit

Function SortConcat(Rng As Range) As Variant

'Rng —The range of data to be sorted and concatenated.

Dim MySum As String, arr1() As String

Dim j As Integer, i As Integer

Dim cl As Range

Dim concat As Variant

On Error GoTo FuncFail:

'initialize output

SortConcat = 0#

'avoid user issues

If Rng.Count = 0 Then Exit Function

'get range into variant variable holding array

ReDim arr1(1 To Rng.Count)

'fill array

i = 1

1)My array is 128 cells wide(AT3:FQ3) and i get a type mismatch--seems to work well for 36 cells

2) These cells contain dates and numbes---all i want to sort and/or display is the numbers

Note: some numbers are in parenthesis and other are either single or double digits. Example: numbers are (8/8),8,10,(HG6),4,6,etc. And Dates are 04/06 format (no year, and no parenthesis)

3) All empty cells in the array show up in the above SortConcat function as zero, and are un-necessary

4) The final output should be no more than 6 concatenated numbers, so column width is not an issue, and the bubble sort is really not necessary but would be a nice feature

I want to build something like this....

function getdb(a,b,c,d,e,f........x)

getdb = a & "," & b & "," & c &....& x

end function

where x is the total number of variables.

So if in excel, I run getdb (a,b) that it will know that the funciton has only 2 variables but if i run getdb(a,b,c,d,e,f,g,h,i,j) that it knows.

I am trying to avoid getdb(a,b,,,,,,,,,,,,,,,,,,,) as I bet the user will not know how many ","s to use

Suppose I have a UDF: function fubar(rg as range) as variant

and I place: =fubar($B$2:$D$11) in cell $A$1

When will fubar be calulated? Is it once initially, and then whenever there is a change in any of the cells within B$2:$D$11.

A hyperlink to an explanation would be just as welcome as a typed reply

I am presently trying to debug a large and repetitive piece of code and find that I keep ending up in a function within the same module. I assume this is because the function is being triggered as I manipulate data. If this is the case, does anyone know how to suspend the function while I debug? I guess I could comment it out as I do not need the data it is returning but during normal running, this is not possible and I expect if it is fired off then it will slow things down considerably if my code keeps getting interrupted by interreupts from the function.

View 2 Replies View RelatedI 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..................

how can I generate a user defined function(UDF) that triggers excel's solver? I have a set of 5 constraints and one objective that is to be maximized. I want to perform a senstivity analysis for my objective changing these 5 constraints. So I want the UDF to take all the 5 constraints and return me the optimized value (obtained from excel's solver) for the objective cell.

View 8 Replies View RelatedI have dates & times in column A1:A20. In B1:B20 I have the corresponding temperatures for each date. I have set up the following dynamic ranges to refer to these ranges.

DateRange refers to A1:A20

TempRange refers to B1:B20

I have also made a user defined function that will determine if a date/time is between two times. Eg., If 21/05/06 07:30 is between "07:00" and "17:00". This function isn't concerned with the date, just if the time falls between the start and end times.

Function BetweenTimes(dDate As String, dStartTime As String, dEndTime As String) As Boolean

dDate = CDate(dDate)

dStartTime = CDate(dStartTime)

dEndTime = CDate(dEndTime)

BetweenTimes = False

'If the end time is before the start time, see if date/time falls between start and end..........

Is it possible to create a User Defined Function that replicates the "Copy/ Paste Special" function? I tried recording a Macro and using that as the basis for the User Defined Function but it didn't work.

View 7 Replies View RelatedIndicate that a user-defined function can only be based upon the calculations that can be placed in a single cell. If you have too many calculations to put them into a single cell, e.g., an entire page of calculations based upon a few starting parameters that eventually yield a single value, then how do you reuse this entire page of calculations?

Is there another Excel mechanism that allows an entire page of calculations to used as a stored procedure?

I've got a couple of user-defined functions that I coded in via one workbook (wkb A), but would like these functions to be accessible to any workbook (wkb B...Z). How is this done so that when I start a new blank wkb I can use these user-defined functions there as well?

View 9 Replies View RelatedPassing 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.

In my spreadsheet, one user defined function i.e. pfizer is there which I want to convert into macro and use it in column H of sheet2 [sheets("sheet2").columns("H:H").select]

What should be the VBA code for this.

when I have started the macro with keyword as application. . the function is not populated.

-->I am able to populate the data with the formula like this = pfizer("I1")

I am in need of creating a User Defined Function in Excel that will sum numeric values based on two text criteria. I have a large set of data in an Excel worksheet that includes a column for " Market Type" and a column for "Location ID". I would like to develop a User Defined Function that allows a new or inexperienced user of the template to use the User Defined Function to select first a "Market Type" then select a "Location ID" and get the sum of the amount those values represent. A SUMPRODUCT function in Excel works for this, but can be awkward or intimidating for a new or inexperienced user. For that reason, I am specifically looking for a User Defined Function that will simply the formula for them while at the same time maintaining the degree of accuracy I need.

Herewith I attached a sample excel file.

Structure file which i attached herewith include all the cost center details. normally it is a big file contain more than 1000 cost center details. Structure File.xlsx If i want a cost center name for some cost center code, i have to open this file and find a specific name.

What i plan is develop a macro function called "CC".

If I type =CC("D232") I need to print cost center name for D232.

But the problem is i don't want to open the structure file, without open that file, when i type this function i need a result.

I am working with a fiscal year that starts in December and ends in November. I want to make a user defined function that will return the fiscal year of a date. I've created the below code, but it returns a zero.

Function FiscalYear(DateFY)

If Month(DateFY) = 12 Then

Year (DateFY) + 1

Else

Year (DateFY)

End If

End Function

E.g. bond price is quoted in 32 so 101-16 will be 101.50

I only know how to add them to my addin but how to write the code.

Function BONDPX()

End of Function

I have a really long function which counts the number of unique values in a selected range, and ignores blank cells. The function is like this (only works as an array function):

{=SUM(IF(FREQUENCY(IF(LEN(A1:A15)>0,MATCH(A1:A15,A1:A15,0),""),IF(LEN(A1:A15)>0,MATCH(A1:A15,A1:A15,0),""))>0,1))}

I want to convert this into a user-defined function COUNTUNIQUE, which only takes one argument: the range (A1:A15 in the above example)

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