Get Cell value by ID in Excel (VBA) - excel

Good day, I set some IDS to some cells in Excel using this:
ActiveCell.id = "whateverID"
I don't know if those IDs are saved, and now I need to get the Values of those Cells but by ID, I don't have any idea on how to get it done, I need it because I want to get the values of specific cells using their IDs if such cells change their position, hope you understand.
Or if you know a better way to get it done the better.

I need it because I want to get the values of specific celss using their IDs if such cells change their position, hope you understand.
For that don't use ID's. Name the cell. For example
ThisWorkbook.Sheets("Sheet1").Range("A1").Name = "whateverID"
Now even if that range moves, you can always use the following to write/read that cell
WRITE
ThisWorkbook.Sheets("Sheet1").Range("whateverID").Value = "Something"
OR
READ
Debug.Print ThisWorkbook.Sheets("Sheet1").Range("whateverID").Value
You might also want to read more about Define named cell references or ranges

Related

How to extract whole rows of data in Excel if it contains a subtring within a string

The entire workbook is a few sheets long, however essentially I'm working with a base sheet that has around 8000 or so lines data with about 10 columns or so. The end goal of this project is to be able to input a start date, end date and a keyword and then be filtered one last time with another keyword. So far, I've been able to filter down the original data within the date range and within the first keyword. The problem arises now when the keyword is within a block of text that varies and is never quite the same. For example, one row contains
12T Q1FY23 Unscheduled/Emergency Maintenance
While another row contains
12T Q4FY23 ERT Spill Stations
There are hundreds of variations of this, but there, including ones that don't start with "12T". The starting data is subject to change so I can't quite use tables in excel and filter it that way, as once you apply a filter then the table won't update if new data is input as the source data, unless there is a way to do this and I just don't know how. So ultimately, I need the same filter that can be used on a table that says "contains" and/or "does not contain" as formulas. Formulas seem to work well with this dynamically/subject-to-change source data, so I'd like to keep it with formulas, as I have done with the filtering previously with the date range and then with the other keyword. The difference between what I want now and what I did for that other keyword is that it was a static keyword that isn't embedded within a string like the "12T". Please let me know if this is too vague or if there's any more material needed to help answer this question. Attached is a sample image of a what I'm working with on the original sheet. I'd like to be able to extract the rows containing only "12T", and not the one's "12T-M", for example, using only a formula. Assume that the data starts at A3 and ends at C8. I should also mention, just to be completely clear, I'm trying to copy these rows dynamically into another sheet so that it can be nicely viewed with only the relevant information and data.
To be extra clear, I first filter it the original data with the following formula:
=INDEX(Sheet1!$A$6:$N$6796, SMALL(IF(COUNTIF('12T'!$H$11,Sheet1!$G$6:$G$6796), MATCH(ROW(Sheet1!$A$6:$N$6796),ROW(Sheet1!$A$6:$N$6796)), ""), ROWS(B3:$B$3)), COLUMNS(Sheet1!$A$6:A6))
The "Sheet1" referral contains the original data and "12T" refers to the sheet that contains the filtering keywords (the dates and the number keyword). This formula extracts all of the rows of the original dataset in Sheet1 that contain a specific keyword, in this case its "5351 - Facilities: Maintenance: Building". These extracted rows of data are deposited as an array (Entered with ctrl+shift+enter) in a new sheet labeled "Xtract".
In this same sheet, I then filter out this array with the date range in mind. With the starting and ending date, I first calculate the number of instances that a date falls within the date range with the following formula.
=SUMPRODUCT(($A$2:$A$671>=Q2)*($A$2:$A$671<=Q3))
I use this result in the following formula in conjunction with the filtered data (filtered with the previous keyword) to filter it further so that I only get the rows of data that have their date in the date range.
=FILTER(A2:O671,(A2:A671>=Q2)*(A2:A671<=Q3),"No data")
This is also entered as an array, and is also in the "Xtract" sheet. With this filtered data set, I want to filter it one last time, so that only the rows of data that contain, for example, "12T" or "728M" in one of the cells (in which the respective cell can be written as "12T Q1FY23 UEM") can get extracted and placed into a final array. All of this is automatically updated simply by entering the values in this section I have shown below.
I can't use a table to filter the data, at least not that I know of, because if I filter a table by this logic ("contains '12T'" and "does not contain '-M'" to get only rows that contain 12T but not 12T-M or anything that's not 12T) then once I change the date range or the other keyword, the table won't update properly. If there's anything else I can add to help clarify, please let me know.
Add a column to the left containing formulae: "=find("12T ",B1) and copy down.
Note the space after T.
Rows matching that will have 1; rows not matching will have #VALUE! so you can sort on them.
P.S. if #VALUE! is ugly, you can use =NOT(ISERROR(FIND("12T ",B2)))
After a lot of searching and referencing my old work/internet, I found the formula to answer my problem. I understand this might not be the most clear since I can't quite provide the excel workbook I'm working with, but the goal of this was to automate all of the filtering so that no matter if data is added or not, when you change the filters, it will stay updated correctly. From the filtered data that I had already worked with, all I had to do to put it into another sheet was use the following formula:
=FILTER(XtractFilters!T2:AF900,ISNUMBER(SEARCH("12T *",XtractFilters!T2:T900)))
This finds all of the data containing a specific substring, which in this case specifically was "12T ", denoting the space as well. So all of the filtered results are then filtered once again so that only the rows where "12T " was found get returned. The range is just the entire range of data and then the column is the one containing the text where "12T " could be found.

Assign a value to a variable [Excel]

I'm making a basic inventory list for my place of work and want to make it easier for future inputs and changes. So far I have inputted all of the model numbers for our inventory, and I was wondering how I could assign a price to the model number, so that it automatically fills it into the cell two cells right. I'm also working with around 50 model numbers, so it would be a lengthy function.
Example:
B E
778740 125.99
778739 120.99
778740
Is there a way that the second "778740" could be automatically filled in? I'm fairly new to excel and I can't find this problem, although I know it is very simple and exists. Even if some one could link me it that would be very much appreciated. Thank you!
Suppose your headers are in row 1, model numbers in column A and prices in column B. You could use the following formula in cell B3 and drag it on down as far as you need. Then what you fill in in column A matches something above, the cell next to it will automatically populate. And otherwise it stays blank, letting you know it is on you to fill it in (though once you do, the formula will no longer be there, so if you later fill in a match above, the magic is gone).
=iferror(indirect("B" & (1+match(A3,A$2:A2,0))),"")
The match function returns an error if nothing is found, and ifError will handle that by leaving your cell blank. If a match is found, we get the value from the corresponding row in column B.

How to select cell range dynamically in sumproduct in excel

I am trying to use Sumproduct dynamically on a sheet that will be updated regularly.
My motive is to not manually select the columns ( i.e. not to use the shift key and down key to select the range A2:A6 as seen in the attached snapshot)
I want to have the formula "=SUMPRODUCT(--(A2:A6="TX"))" pick up the A2:A6 dynamically since my data in worksheet say A is everchanging. So that when the data has grown to A8, it can be somehow picked up dynamically than manually using the keys.
Can this be done? Please let me know if my question is not clear.
Screenshot of my xls
Regards
SM
Yes, you could define the range dynamically. However, given your current formula, I see no reason why you should not switch to COUNTIF here, i.e.:
=COUNTIF(A:A,"TX")
a function for which the use of entire column referencing (A:A here) has no detriment to performance (which is not at all the case with SUMPRODUCT).
Regards
Shitty recommendations guy, here.
Can you insert a new line above all in your sheet? You could put it really wherever you want and you can even hide it, the first line just works for me.
If you can, just make it an "count.values" function on the entire column with an appropriated subtraction to adjust the count to the right length of your data. E.g. Removing this auxiliary cell value plus a header would be "=count.values(A:A)-2".
Later, on your VBA, you could retrieve this value to a variable and concatenate it to your main function. Lets say you made the auxiliary on cell A1 (coordinates 1, 1)
Dim auxLine As Integer
auxLine = cells(1, 1).value
whatever.formular1c1 = "=SUMPRODUCT(--(A2:A" & auxLine & ="TX"))"

Excel VLOOKUP #N/A

What I am trying to achieve is matching two projects with eachother "Project No" in two tables, and return the "project name" in second table. When I do this I receive this error inside the cell error #N/A.
I have formated both tables into number value, but this does not solve the problem.
Anyone that has an idea what I can do in order to solve this problem.
Here is the first table (just a snapshot to give an idea).
Here is the second table (just a snapshot to give an idea).
The #N/A error indicates that Excel cannot find the value you've specified in the list in which you've told it look. When I run into this, there are a few things I do to troubleshoot:
1) Make sure both columns are in the same format.
You mention you've already done this (good job!) but sometimes formatting gets stuck in Excel. One thing you can do is to highlight each range and use the text to columns feature (under the data tab) to convert it all to the same format. Another option is to copy the range into Notepad or some other plain text editor and then copy it back into Excel to strip out any lingering formatting.
2) Double check your ranges.
Make sure your Vlookup is looking to the correct range, and make sure that range is locked (i.e. $A$1:$D$100 rather than A1:D100), or else your ranges are going to slide around as you move drag the formula down your table. If the range refers explicitly to a table address (e.g. Table1[#All]), then make sure every data point if validly within that table.
3) Make sure the range lookup parameter is set to FALSE
Range lookup is the last parameter of the vlookup, and determines whether the vlookup will find an exact match (set range lookup to FALSE) or a partial match (set range lookup to TRUE). Setting range lookup to TRUE can sometimes create errors like the one you've described. I always set it to FALSE by default unless I need specifically need to set it to TRUE.
4) Use the find and replace window to manually evaluate the formula
If everything looks correct, copy the value you want to look up and paste it into the find and replace window to search for it. If Excel can't find it and you can see it in your list, something is definitely wrong with the formatting or else your file may have some degree of corruption. If Excel can find the value, double check that it is within the range your vlookup is looking through.
If fist table is the one with vlookup formula and second is a source it may mean that such Project No. doesn't exist in your source table. Did you check that possibility?

Excel, Specifying cell contents as a result of other cells

I don't use excel often, and I haven't really found a good solution to my problem. (which is probably really simple).
I would like to have a cell with a function in my spreadsheet that shows another cell value value that depends on yet another cell value.
Such as:
The Best Deal heading simply uses the formula
=MAX(D3,D1000)
But under Best Deal I would like to display the Name Test1 rather than the numeric value.
Another thing that would be nice to know, is if there is a way to know the maximum row with data in it. So rather than =MAX(D3,D1000) something like =MAX(D3,Max(RowCount_InD))
Obviously that function wouldn't work as I wrote it, but hopefully this pseudo code gives you an idea of what I mean. The purpose is that if more entries are added, it would be able to handle them.
I know this is possible, but I'm having some trouble. Hopefully I can get some help here.
Thanks!
The easiest way to do this is to use a combination of Index and Match. Match will find the position of the maximum value, and then Index will look in column A and return the data in that same position. So, your formula would be:
=INDEX(A$3:A$1000,MATCH(MAX(D$3:D$1000),D$3:D$1000,0))
Put that formula in F3. No hidden columns required.
Sorry, I missed the part about the expanding range. You can do that by using Count or CountA along with Offset. The new formula would be:
=INDEX(A3:OFFSET(A3,COUNTA(A:A)-1,0),MATCH(MAX(D3:OFFSET(D3,COUNT(D:D)-1,0)),D3:OFFSET(D3,COUNT(D:D)-1,0),0))
More complex, but it is basically the same except that it will expand as you add new values at the end.
There is only one 'simple' way I can think of this, but it requires hidden columns (sorry).
set E1 = A1 and fill down all the way (Basically you are making a copy of column A in column E but you are using a formula so it will always be the same)
Then under 'Best deal' - put this formula:
=VLOOKUP(MAX(D3,D1000),$D:$E,2,FALSE)
Then hide column E so it doesn't look like a mess. This way you do not need any fancy macro's and it will work everywhere because it is a normal formula.
"Another thing that would be nice to know, is if there is a way to know the maximum row with data in it. So rather than =MAX(D3,D1000) something like =MAX(D3,Max(RowCount_InD))"
This is a called a dynamic named range. Create a name for the ratio data, and set up the formula for the name range to be this:
=OFFSET($D$3, 0, 0, COUNTA($D$3:$D$1048576), 1)
More info here: http://www.ozgrid.com/Excel/DynamicRanges.htm
Then, assuming you name this named range ratio_data, your function could be referring to =MAX(ratio_data) in combination with index-match as suggested by #Tim Mayes. The range will expand automatically as you add more data.
=INDEX(A$3:A$1000,MATCH(MAX(ratio_data),ratio_data,0))
Ideally, you can replace the A3:A1000 by a dynamic named range as well.

Resources