Public Function Lohn(ID)
Lohn = Application.WorksheetFunction.VLookup(ID, ThisWorkbook.Worksheets("Arbeiter").Range("A:D"), 2, 0)
End Function
I cant access the Worksheet "Arbeiter" its just doesn't give me a Value back can some one help me
There is nothing more code in the Section if anyone wants to know
ID is numeric
I got it to work after changing from Application.WorksheetFunction.VLookup to Application.VLookup
Be careful to load in an ID with the same data type as the value in the cell. It gave me error 2042 when i used a string as the ID but was able to work properly when I put in an integer.
Here is your function after my changes:
Public Function Lohn(ID)
Lohn = Application.VLookup(ID, ThisWorkbook.Worksheets("Arbeiter").Range("A:D"), 2, False)
End Function
Related
I'm trying to come up with a script that looks up a function in named range "Hard_Constr_Cost". Saves the function in variable "HC_Baseline" and then copies it to another range.
The reason why I don't just set one range equal to another is that after the function is stored I want to run a separate loop overwriting the value in range "Hard_Constr_Cost". At the end I want to overwrite with the original function stored in the variable "HC_Baseline".
Below is the script I created so far but I'm getting an out-of-range error. Does anyone know how to fix this? Any help is appreciated.
Sub HC_Sensitivity()
Dim HC_Baseline As String
HC_Baseline = Sheets("SU").Range("Hard_Constr_Cost").Formula
Sheets("SU").Range("T5") = HC_Baseline
End Sub
i have created user defined function to calculate football match results.
My Root Function looks that:
Function calculatePoints(personTypes As Range, matchesResults As Range) As Integer
calculatePoints = getAllPersonPoints(personTypes, matchesResults)
End Function
getAllPersonPoints function:
Private Function getAllPersonPoints(personTypes As Range, matchesResults
AsRange) As Integer
Dim x As Long
Dim y As Long
Dim isTheSurest As Boolean
getAllPersonPoints = 0
For x = 1 To personTypes.Rows.Count
For y = 1 To personTypes.Columns.Count
isTheSurest = isTheSurestResult(personTypes.Cells(x,
y).DisplayFormat.Interior.PatternColorIndex)
getAllPersonPoints = getAllPersonPoints +
getPoints(matchesResults.Cells(x, y).Value, personTypes.Cells(x, y).Value,
isTheSurest)
Next y
Next x
End Function
When i am trying to call this function by setting manually parameters: personTypes range and matchesResults ragne - everythink works fine.
But when i am trying to call it from sheet i got #VALUE error in selected cell.
But at function form there is correct result:
A have been trying to debug return value and always i got correct value. I have problem only with error in return cell.
Any ideas ?
The issue is that DisplayFormat object does not work with UDF's
See MSDN article
The usual solution to this is to evaluate the Conditional Format conditions to determine which one is active. For example, see cpearson.com
I resolved problem by code:
personTypes.Cells(x, y).Interior.ColorIndex
instead of:
personTypes.Cells(x,y).DisplayFormat.Interior.PatternColorIndex
This solution works.
Nom_1 is the name of a cell in my worksheet, and is used in the following code.
Someimes that cell is left empty and I don't want the NormInv function to get called because it will return an error if the inputs are empty.
I get the error: Unable to get the NormInv property of the WorksheetFunction class
This leads me to believe my if statement is incorrect and it is allowing for the code to enter and execute even if Nom_1 is empty.
If Not IsEmpty(Nom_1) Then
internal_1 = Application.WorksheetFunction.NormInv(Rnd(), N_1, Std_1)
End If
Am I correctly checking if the cell is empty?
Just Expanding on Barranka's comment,
You need to make sure all cells with the names Nom_1 ,N_1,Std_1,internal_1 exist in the workbook
then use Range("Nom_1") instead of Nom_1
Sub MyNormInv()
If Not IsEmpty(Range("Nom_1")) Then
Range("internal_1") = Application.WorksheetFunction.NormInv(Rnd(), Range("N_1"), Range("Std_1"))
End If
End Sub
You could use
If Range(Nom_1).Value <> vbNullString Then
internal_1 = Application.WorksheetFunction.NormInv(Rnd(), N_1, Std_1)
End If
However, know that this will return False if you have a formula returning a zero length string in the cell. Even though the cell is not "empty" it will still be seen as blank because the value is "".
I'm pretty new to this so apologies in advance
I'm half way through a userform in Excel and I'm trying to cut some fat off my code by using Call - I have 12 buttons that all do the same thing, the only difference is that each buttons sub is dependant on the buttons caption. My problem is that I can't figure out a way to use a String I've already declared in the Buttons Sub, then use it in the called Sub. I know you can do it, but my googling skills have failed me :(
Please could someone show me how to do this? Hope that all makes sense...
Here is a very small snippet of my code, but you get the jist:
Public Sub CommandButton4_Click()
Dim Name As String
Name = CommandButton4.Caption
Call Sort1
End Sub`
And the other one (Also tried this as function for the sake of trial and error)
Public Sub Sort1(Name As String)
Label11.Caption = Name
Sheets(Name).Select
End Sub
What you're referring to is passing an argument to another subroutine or function. Let's say you want to use a function a lot of times to get the first letter of a string. A sample of this is:
Function LeftOne(StrSample As String) As String
LeftOne = Left(StrSample, 1)
End Function
The above function can be used inside another function or subroutine provided you meet its requirement: StrSample. By declaring StrSample As String in the arguments field of the function, you are basically requiring that any calls to this should require a string to be passed to it. Anything else would throw an error.
The full line LeftOne(StrSample As String) As String can be read as: "I am function LeftOne. Pass me a string and I'll return to you a string after doing something with it." Note that the name StrSample is an arbitrary name.
Anyway, calling the above is as simple as:
Sub MsgInABox()
Dim StrToFeed As String
StrToFeed = "BK201"
MsgBox LeftOne(StrToFeed) 'Returns B.
End Sub
In your example, if you want to pass Name to Sort1, your attempt is absolutely correct.
Let us know if this helps.
You hat to give your sort1 procedure the parameter name.
call sort1(name)
or
call sort1(CommandButton4.Caption)
Have searched and have come up short on any solutions to this. I am relatively new to VB for the record. The variable minDate here is declared in the module outside of the procedure. I have tried declaring it inside the procedure, using set, let, and passing the argument as a range variable. Nothing.
Sub SocialTimeSinceFirstComment()
'
' SocialTimeSinceFirstComment Macro
'
Range("A11").Select
ActiveCell.FormulaR1C1 = "=MIN(SocialTransform!C[4])"
minDate = Application.WorksheetFunction.Min(Workbook.SocialTransform!.Range("c4").End(xlDown))
Apparently you have a worsheet named SocialTransform so use this:
Dim ws as Worksheet
set ws = ThisWorkbook.Worksheets("SocialTransform")
Dim minRange as Range
set minRange = ws.range(ws.Range("c4"), ws.Range("c4").End(xlDown))
minRange.Select 'use this when testing so you can see exactly what is included
minDate = Application.WorksheetFunction.Min(minRange)
Hopefully you have minDate declared as a date Dim MinDate As Date otherwise you'll just get a number; which could of course be transformed back to the corresponding date.
Also I might explain that the error you got was not because of minDate but because SocialTransform! is not an object of Workbook. You need to use Worksheets("SocialTransform")
EDIT: Actually you can use the code name to reference a sheet within the workbook like this: Debug.Print Sheet1.Name
In this example the user renamed the first sheet to "Data" and the Sheet1.Name will return "Data". The only way to change the code name is by change the "(Name)" property in the VBA editor window.
See: Worksheet.CodeName Property (Excel)
The error message is clear, and you don't "clearly have an object". (The compiler is almost always more aware of the code and syntax than we are, so if it says something is wrong you should probably believe it until you can prove otherwise.)
Workbook.SocialTransform!.Range in the last line of code you posted is invalid (WOrkbook.SocialTransform! isn't valid code the way you're using it), and therefore it doesn't return an object. However, you're referencing it as one, which generates the error.
It's valid inside quotes as you're using it in the line that precedes it.