I want to have a faster process looking up cross references.
Right now I use VLOOKUP, and it works fine - but it takes time when it needs to be done multiple times everyday.
It is always the same sheet I use to lookup the cross references, so the only thing that changes is my input value in the VLOOKUP function.
Therefore I want a function where I only input 1 value to get the VLOOKUP value.
The idea is a function like:
=CROSS(ID)
where
CROSS = vlookup(ID, table_array, col_index_num,[range_lookup])
So the vlookup_value is replaced by ID.
I hope you can provide me with some answers - thanks in advance.
I have tried multiple different things, but with no success.
As I am new, I've googled and recorded macros to look for answers.
You could write a UDF (user defined function) for that, using the WorksheetFunction.VLookup method:
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, table_array, col_index_num, range_lookup)
End Function
I got it working as it should now!
The code ended up like this:
Sub crossref()
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, Worksheets("Sheet1").Range("E:F"), 2, 0)
End Function
Related
I have this function within my code:
Function get_header(ByVal rw As Range) As Scripting.Dictionary
Dim header As New Scripting.Dictionary
Dim used As Range
Set used = Range(rw.Cells(1, 1), rw.Cells(1, rw.Cells(1, rw.Columns.Count).End(xlToLeft).Column))
For Each cl In used.Cells
header.Add cl.Value, cl.Column
Next
Set get_header = header
End Function
What the function does is it takes the table header and creates a dictionary of column names and the respective indexes, so that the order of columns is not important for the rest of the code.
My question is: is it necessary to use a separate variable to store the value throughout the loop, or can I
edit the returned value ("get_header") whole time instead of only passing the value at the end or
use the With structure like this:
Function get_header(ByVal rw As Range) As Scripting.Dictionary
Dim used As Range
With rw
Set used = Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
End With
With get_header
For Each cl In used.Cells
.Add cl.Value, cl.Column
Next
End With
End Function
Also, why should I use any of these structures instead of the others?
Thanks for any advice, guys.
Wanted to comment this, but too long for a comment:
When I have a function that "calculates" the return value step by step, I usually prefer to have an intermediate variable for the following reasons (note that using extra variables comes at no cost during runtime, that's not an issue):
Naming: I don't like assigning values to something that is called getSomething.
when used right-sided, it avoids ambiguity between the "intermediate" value and a recursive call to the function: getSomething = getSomething + getSomething(p). The compiler can handle that, but my brain? Not so much. I find retVal = retVal + getSomething(p) is much clearer.
When running on an error condition in the middle of execution, I can easily do an Exit Function without thinking about what was already calculated.
But at the end of the day (and this valid also for the question if of if not to use a With-statement), there are two things that matters: (1) Which code is easier to read and understand. and (2) Which code is less likely to hold an error. As human brains don't work all the same: Find you personal style and stick to it.
I would favor your first example instead of editing the function's return value more than once in a single function call.
Depending on whether error trapping breaks or is silent (i.e. on error resume next), you may get different and unintended results. Importantly, you may get unintended results and not know it.
If a few cycles run fine, and the function result variable is updated a few times, then some error occurs with processing a later cycle (due to inputs, scope, range, etc issues for the later part of the input array, or even just memory or focus or other runtime issues), you will have a problem: the function returns a premature (therefore incorrect) result, but appears as though it has run properly.
I'd recommend coding to avoid this situation, whether or not errors are suppressed. An easy way to do this is to not set the function result object more than once per possible function execution path.
Your first code block seems to conform to this thinking already. Each of your alternate suggestions 1 and 2 do not.
I am building a User Defined Function.
I get an error
A Value used in the formula is of the wrong data type
I am trying to build a function that adding one comment, will also add the comment to another location as the comments always come in pairs.
Public Function AddComments(vesselCell As Variant, shopCell As Variant, comment As String) As Variant
Range(vesselCell).AddComment (comment)
Worksheets("Shop").Range(shopCell).Value = comment
End Function
I have singled it out to the third line of code causing the problem.
The setup currently is the Vessel Cell will be a comment added to the sheet, and then the Shop sheet has a section for comments as a column.
Assuming vesselCell and shopCell are both Range objects, Range(vesselCell) and Worksheets("Shop").Range(shopCell) are part of the problem.
Make them Range, not Variant.
vesselCell.AddComment comment
shopCell.Value = comment
Now, this code isn't legal in a UDF. Make your procedure a Sub procedure (remove the As Variant return type), and invoke it from other VBA code, or attach it to a shape or button's OnAction property.
User-Defined Functions take inputs, compute a result, and then return that result to the calling cell: your code not returning anything is a strong indicator that a Function isn't appropriate here.
I need to get my cell content in excel worksheet to another worksheet by using a user defined function. So I searched stackoverflow and found the following vba code.
Function GetValue(sheetName As String, cellAddress As String) As Variant
GetSheetValue = ThisWorkbook.Sheets(sheetName).Range(cellAddress)
End Function
This is the right thing i expected.But it doesn't work for me. I need to develop a user defined function as like follows.
=GetContent(Sheet1,B6)
I've tried many things but I can't do it. So I'm very appriciate any responses about this matter. (Provide Screenshots about my example)
Thank You
ScreenShot 02
ScreenShot 03
I've found the answer. My function must be correct as following code.
Function GetContent(sheetName As String, cellAddress As String) As String
GetContent = ActiveWorkbook.Sheets(sheetName).Range(cellAddress)
End Function
I didn't concern abot the function name and return as the same. But we have to must do it. Also in string data type we have use quotaions and Case sensitive. So thanks all of you for valuable help. It helps me to fid out the answer. Now wecan use the functions as follows.
=GetContent("Sheet2","b5")
Thank You!
I'm trying to learn a little VBA an I'm in the process of saving ranges as CSV files.
However, I'm having trouble with this line of code
Call SaveRangeAsCSV(SelectDown("B5:D5"), "C:\Test\test.csv", True)
And here is a snippet of the SelectDown function.
Private Function SelectDown(Range As String) As Range
SelectDown = Range(Range, ActiveCell.End(xlDown)).Select
End Function
I get the error: Expected array. I cannot seem to understand what the problem is. Any help would be appreciated :-)
It sounds like the function SaveRangeAsCSV is expecting an array, but you are passing in a Range. This function is not a built-in Excel function, so I can't check it. Maybe you could check what arguments it is expecting?
My function now looks like this and is working perfectly.
Private Function SelectDown(RangeAddress As String) As Range
Set SelectDown = Range(RangeAddress, ActiveCell.End(xlDown))
End Function
I create a Public vba function with 2 parameters(module).
When I call the function I type "=InvoiceAmount2(A9;B9)".
The first parameter turns blue. The second black.
I remake the same function using one parameter, the second I use into the function, that way it´s ok. But I need two parameters
This is how you call a user defined function with two parameters:
=MyFunction(A1,B1)
Sample Code:
Function MyFunction(rCellA As Range, rCellB As Range)
MyFunction = rCellA.Value + rCellB.Value
End Function
I found the problem.
I call the udf function in formula constructor. I informed the parameters (A9 and B9)
finally, the udf function filled the cell. "=InvoiceAmount2(A9;B9)".