I'm very new to VBA, yet I still find myself writing a UDF for excel and part of that process requires that a value be pulled from another sheet that is open in the same workbook. I don't know what I'm doing obviously, but I'm hoping someone else can point me in the right direction.
A sheet called "VIAL TYPES" has the data that I need in the 7th column.
For the purpose of this question, I would just like to print the value of VLookup in cell G9.
To run the function, I would manually write in the VialType in an adjacent cell. I expect the Vlookup function to recognize the type entered into cell (=VTEST(cellIwrotethevialtypein)), find it's match in the "VIAL TYPES" sheet and then print the value in the 7th column back in the active sheet. Basically, I want it to do exactly what VLOOKUP does, I just need this to work in vba so that I can execute additional things with the value.
I get #Value! in the cell now.
Function VTEST(VialType As String)
ActiveWorkbook.ActiveSheet.Range("G9").Value = Application.WorksheetFunction.VLookup(VialType, ActiveWorkbook.Worksheets("VIAL TYPES").Range("A1:A309"), 7, False)
End Function
Related
I created this worksheet function and it works perfectly.
=LARGE({1;2;3;4;5;6;7;8;9;10;11;12;13;14;0;16;0;0;0;20;21;0;0},1)
The problem is how I created it. I started out with the function below which, unfortunately, does not work. It returns 0 instead of 21. That is unfortunate because it's precisely this function that I need.
=LARGE(ISBLANK(R$1:R23)*ROW(R$1:R23),1)
I converted the function I want to the function that works by selecting ISBLANK(R$1:R23)*ROW(R$1:R23) in the formula bar and pressing F9. Of course, this action merely displays the array specified by the formula. Therefore the working function and the not working function are technically identical. If you can't tell me how that can be then please try telling me why it is so.
Of course, I tried all the remedies that appeared "obvious". I tried entering the formula as an array formula (no need in Excel 365). I added -- before the ISBLANK function and tried that with the function in parentheses. All to no avail.
BTW, my purpose is to use the number of the blank sheet row above the cell with the formula in an OFFSET function to return a value from another column in that row. Imagine a block of cells with a title and wanting to reference the title row from any cell in the block, where the title row is identifiable because it has blank cells in the columns where the block has data.
So here's what I'm trying to do, all nested together.
I'm trying to Match one string in the sheet that this command is in a cell in, comparing it to a range of strings in a different sheet (for example, in the main sheet, I have a value 1337 in cell I1670, and I'm trying to find '1337' in a different sheet, known as 'Sheet1'). That returns to me what Row the matching cell is. This function is:
=MATCH(I1670, Sheet1!G:G, 0)
Then, since in know that in the other sheet, the range is in Column G (Numerically, column 7), I'm trying to get the exact address of that using the Address function:
=ADDRESS(MATCH(I1670, Sheet1!G:G, 0), 7))
Then, I'm trying to get the value within that Cell, so I use the Indirect function:
=INDIRECT(ADDRESS(MATCH(I1670, Sheet1!G:G, 0), 7))
I know this has problems. For starters, I'm trying to access the cell in another sheet, but Indirect, to my knowledge, would probably be looking for the resulting address in the current Sheet, not Sheet1. I'm not sure how to format it within this function to have it look in Sheet1.
When I just do =ADDRESS(MATCH(I1670, Sheet1!G:G, 0),7), I get $G$6383, which is in fact the cell I want, Cell G6383, it's just that I don't know how to extract the info in it.
If anyone can tell me what I'm doing wrong, or what direction to look for, thanks! I tried looking and could not find a similar question, but it might be because I'm a huge Excel novice. I apologize if this has been asked before.
Try using
=INDIRECT(ADDRESS(MATCH(I1670, Sheet1!G:G, 0),7,,,"Sheet1"))
ADDRESS has an optional input for a sheet reference. If it's omitted the function will refer to the sheet that it is on Source
Is there a way to execute a formula when a cell is clicked or get adress of selected cell and then if it matches execute formula without using VBA?
There is something that almost does want you want natively in Excel without VBA.
The CELL function allows you to access some (fairly limited) information about a particular cell. If you choose, you can direct the function to give you information about a particular cell, or you can let the function give you information about the last cell which had information entered in it.
For example, the following tells you the width of cell A1:
=CELL("width",A1)
Whereas the following (without a second argument provided) tells you the address of the last cell where data was entered:
=CELL("address")
For your purposes, you can use this information in other formulas to act based on where you have most recently entered data, like so:
=IF(CELL("address") = "$A$1", "A1 was the last cell entered!", "")
I have a table of results in MS Excel 2010 with formulas in each cell that I need to edit. I have INDIRECT cell references in the formulas that do not update when copied to other cells. So, I believe the easiest way in this case for me to edit all the formulas in the table is with two find-and-replaces (changing the beginning and end of each formula to have what I need). However, when I try to do the find and replace, Excel shoots a "The formula you typed contains an error" pop-up at me and refuses to execute the replace. I know there's an error, I'm not finished with the formula yet! So:
1) How can these errors be disabled?
2) Is there a way to make indirect cell references update the way a normal cell reference would (eg. make INDIRECT("'"&F5&"'!L7") update L7 to L8 in the next row) so that I can copy the new formula throughout the table of results, and avoid the find-and-replace entirely?
Sorry guys, I realize this is a somewhat strange question for stackoverflow.
1) I don't know.
2) I'm not 100% sure from the question, but I can think of two scenarios:-
(a) You have a list of references which your INDIRECT formula uses, and you want it to select each one in turn as you pull the formula down. This is trivial so it's probably not what you meant:-
=INDIRECT(A$1&"!"&A2)
assuming your sheet name is in A1 and the references are in A2,A3 etc.
(b) You have a reference to the beginning of a range of cells in sheet2, and when you pull the INDIRECT formula down, you want it to automatically get the next cell in the range:-
=INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN(INDIRECT($A$2)),4,1,$A$1),"1","")
&(ROW(INDIRECT($A$2))+ROW()-ROW($C$2)))
where this formula is placed in cell C2 and pulled down.
The idea is that you break the cell reference down into its column name (using the excellent suggestion here ) and its row number, then get the row number to increment using the ROW function.
Sheet 2:-
Sheet 1:-
very new to this and only just managed the vlookup, anyway,
i have a workbook which contains two Sheets. On Sheet1 I have the following formula (=VLOOKUP(CZ54,Sheet2!$A$1:$Y$176,3,FALSE)) which displays the ID number from Sheet2. This works correctly but how do I get to appear as a hyperlink so once clicked it goes to the specific cell it got from the vloopkup on Sheet2?
I have tried =HYPERLINK(VLOOKUP(CZ54,Sheet2!$A$1:$Y$176,3,FALSE) but I get the error "Cannot get the specified file".
I would be grateful if anyone can point me in the right direction, thank you.
Ok... this looks crazy. This should work for you. I tested it on my machine and it works:
=HYPERLINK(MID(CELL("filename"),SEARCH("[",CELL("filename")), SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))+1) & ADDRESS(MATCH(CZ54,Sheet2!$A$1:$A$176),3,,,"sheet2"),VLOOKUP(CZ54,Sheet2!$A$1:$Y$176,3,FALSE))
Let's break it up a little just to explain some of the parts:
MID(CELL("filename"),SEARCH("[",CELL("filename")), SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))+1)
Returns a string [currentFileName], which is the required format for a hyperlink within a workbook.
MATCH(CZ54,Sheet2!$A$1:$A$176),3,,,"sheet2")
Returns the row number of value of CZ54 in the first column of your VLookup range.
ADDRESS(MATCH(CZ54,Sheet2!$A$1:$A$176),3,,,"sheet2")
Returns the address of the row found, and the column(3, from your vlookup) in sheet2.
So combined:
MID(CELL("filename"),SEARCH("[",CELL("filename")), SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))+1) & ADDRESS(MATCH(CZ54,Sheet2!$A$1:$A$176),3,,,"sheet2")
Returns the actual address for the hyperlink, which is the first parameter of the HYPERLINK formula.
The second parameter is the Vlookup you provided, and voilĂ .
Edit: A warning if anyone tries to use this for a similar issue, if the lookup_array used in Match or Vlookup does not start with Row 1 or column 1 respectively, you will need to offset the values sent to ADDRESS with a +(starting row or column - 1). Example: Match(CZ54,$A$2:$A$176)+1 instead of MATCH(CZ54,Sheet2!$A$1:$A$176).
Here's a more compact version of Daniel's formula.
Using Hyperlink to link to another sheet in the same workbook, you can replace the reference to the workbook name with #.
Eg =HYPERLINK("#Sheet2!$C$10","Click Here")
So, applying this you get
=HYPERLINK("#"&ADDRESS(MATCH(E21,Sheet2!$A$1:$A$176),3,,,"sheet2"),VLOOKUP(E21,Sheet2!$A$1:$Y$176,3,FALSE))
HYPERLINK is looking for text so if you were to do something like
=HYPERLINK(left(VLOOKUP(CZ54,Sheet2!$A$1:$Y$176,3,FALSE),255))
it should return a usable hyperlink