How do I get more information from an additional column if the first one does not have it? - vba

I would just like to say that I'm very new to VBA and more complicated formulas so all the help will be appreciated! Thank you!
To clarify a bit more with the title. I currently have a Macro that is reading a formula to give me information from another worksheet. I'll give an example of the formula that is working:
=IF(A2 = ""No Specific Program"", A2,IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),....!$C:$I,4,FALSE),""NO DATA"")))
This is the original formula that I'm using to get the information, column A is my Parts owned by Program, And my column B is the actual Program. So when I run the Macro it does give me most of the information, but when it runs into "No Specific Program" even when there is something for Column B showing what program the part is in, it will give me No specific Program.
Also for reference the F2 it is Concatenating is a PIN number which will help determine who owns the part.
I've been stumped on this trying to get the code to work and I've tried place THEN and ELSE within the statement and it just gives a FALSE statement
EDIT:
The Code above works, it's when I use this version of the Code:
=IF(A2 = ""No Specific Program"",THEN,IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),'.....'!$C:$I,4,FALSE),""NO DATA"",Else,IF(A2 = ""No Specific Program"",THEN,IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),'.....'!$C:$I,4,FALSE),""NO DATA"")))))
I get the False or errors when I try different variations. Here is an example of the columns. Column A is where I have the original formula reading from, but then it says No Specific Program, while Column B shows the Program. So I'm trying to get the formula to read that as well as column A to capture all the information I need :
Columns Example
EDIT:
It starts breaking after the ELSE statement.
Edit:
=IF(A2 = ""No Specific Program"",
IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(B2,F2),'\NW\Data\TechIntegration\Sustaining
Team\Data
Mining\DataMining[GAD_PIN_TABLE.xlsx]Sheet1'!$C:$I,5,FALSE),
""NO GAD
DATA"",
IF(F2 = """",""No
PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),'\NW\Data\TechIntegration\Sustaining
Team\Data
Mining\DataMining[GAD_PIN_TABLE.xlsx]Sheet1'!$C:$I,5,FALSE),""NO GAD
DATA"")))))
Just trying to make it easier to see the formula.

I'd split this down to make it simpler to follow, use a holding cell then refer to that in your top formula (The one you know works already)
Stick this in another column, say Z for example, then everywhere you refer to A2 in your working formula, change it to Z2
=IF(A2=""No Specific Program"",IF(B2=""No Specific Program"",""No Specific Program"",B2),A2)
This will only give you "No specific program" if both a2 and b2 contain "No specific program" which I think is what you're after. In your second example in the columns example link, it will return NG

Related

Excel - Call function from other cell

First of all I would like to tell you that I have just basics with VBA or Visual Basic. So probably there will be better solution in VBA, but I was unable to find a way. That's why that my attempt is so crude. Also my english can be somewhat broken sometimes. I'm sorry for that.
Below you can find screenshots from my excel sheet. You can notice functions are in czech language. You can find translation below.
Screenshot with actual results:
Screenshot with actual functions:
Translated functions:
AA =COUNTIFS(A:A;X2;D:D;D129)
AB =IF(AA129=1;CELL("address";INDEX(D:D;MATCH(D129;D:D;0)));"NULL")
AC =IF(AA129=1;REPLACE(AB129;2;1;"M");"NULL")
AD =IF(AA129=1;CONCATENATE("=IF(";"""";M129;"""";"=";AC129;";1;0)");"NULL")
AF =ISNUMBER(AE129)
AG =IF(AF129=TRUE;CONCATENATE(AE129);0)
Description:
Every day someone downloads .csv files from JIRA and uploads it to excel. That csv contains test cases. Column X has the date when this csv was uploaded, Column D has the ID of the test case. Column M has the date of the last change on that test case.
Functions description:
AA: It searches if there is the same test case in previous day. If yes, then result is 1. If no, Result is 0.
AB: Locates the exact cell location of the same test case from previous day.
AC: Serves for translation from ID column (D) to last change column (M).
AD: Composes translated information from AC, so it will create a function, which I would like to use further. That function will compare last change date with the test case from previous day and current day. Result should be 1 or 0 depending on if there was any change.
AF: Checks if AE is number (AE should be the column where I would like to run
that function from AD).
AG: If AF is true, then it copies value from AE. Else it's 0
Problem is:
I can't find a way to trick excel to take the text from AD and use it as function in another cell. Tried to program it in VBA, but failed.
What I need:
Translate result from AD from text to function and then run it in AE.
If there is a more efficient way to program this, I am open to suggestions. But I would like to have an answer for my question as well. I want to know if it's possible because I will make more complicated cases later while I learn to work with VBA.
Thanks for the response.

SUMIFS returns 0 using dynamic criteria, with criteria range and sum range on another sheet

Anyone,
I've chatted with and called excel customer service with no luck. I used the formula builder (please see attached screenshot) to make sure each element of the formula is correct and returns the value for the criteria I'm trying to reference.
Everything is accurate, but it returns a value of 0. When I do the same thing in the actual sheet the data is stored in (and click a criteria cell within the criteria range) it returns the accurate value?! I'm not sure why it won't work on the other sheet. The values I am using to select are dynamic and change with a drop down. I have another, advanced, workbook (I did not create) that does the same thing and completes an even more complicated formula, but actually works so I'm not sure why this is returning a 0 value.
Photos and code/syntax: Dynamic Selection, Example 2 of it working, Example 1 of it working, Formula Builder, CountIFs, Advanced Spreadsheet working, VLOOKUP
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,FMCOP!$C$20,'GFEBS Pull'!H:H,FMCOP!B23)
or:
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,'FMCOP'!$C$20,'GFEBS Pull'!H:H,'FMCOP'!B23)
When I type ' around FMCOP sheet name, they disappear? I've also tried to lock the columns on the 'GFEBS Pull' sheet with no luck. Cell B23 is not locked because I'm going to copy the formula down to reference other cells. Any help is appreciated!
In this screenshot you can clearly see that both FMCOP!C20 ansd FMCOP!B23 have prefacing spaces; e.g. " HHC".
Since " HHC" will never match "HHC", fix the data returned from 'the lower table in the same screenshot'.
A Text-to-Columns, Fixed Width, Finish should do this. You could adjust the original formula like,
=SUMIFS('GFEBS Pull'!Q:Q, 'GFEBS Pull'!G:G, TRIM(FMCOP!$C$20), 'GFEBS Pull'!H:H, TRIM(FMCOP!B23))
I would caution against the latter 'bandaid' fix. Fix the original data; do not apply bandaids on-the-fly.

Vlookup with three comments depends on found value vba code or formula ( very hard to find solution)

I have very difficult scenario where i need to mention three comments by tow V lookup. let me explain you situation.
We are getting three sheets in workbook with values every weekly.
sheet1 range A1 to A5 value , lookup values sheet2 range A:B and lookup value sheet3 range A:B. yes i can able to put two V lookup , but problem is 3 comments i need to show in single output.
When i lookup values found in sheet2 need comment as "found value in sheet2" , if not it has to lookup sheet3 range if found then it has to give comment as "found value in sheet3" and if value not found need to give comment as " invalid data".
through vba can is it possible and three comments should be sheet1 in a single output based on values?
am looked in google i can use IFSA formula and apply formula but i cant provide three comments ...
For us its really hard every weekend , Please help me with VBA Code or Any formula at one shot for three comments ?
Really appreciate your help
You can actually have up to 7 Nested (IF) functions in Excel - Nested Formula Limit
You need to make sure that you properly close each function. This can be seen when editing the formula, it will highlight the opening/closing parentheses as you arrow through the formula.
If it tries to select a range while using the arrow keys, hit F2 to toggle between Edit/Enter modes.
I think you want this...
=IF(ISNA(VLOOKUP(A1,Sheet1!A1:B4,2,FALSE)),IF(ISNA(VLOOKUP(A1,Sheet2!A1:B4,2,FALSE)),IF(ISNA(VLOOKUP(A1,Sheet3!A1:B4,2,FALSE)),"Invalid Data", "Sheet 3"),"Sheet2"),"Sheet1")
Actually, on second thought, I assume you want whatever is in Column B
=IFNA(VLOOKUP(A1,Sheet1!A1:B4,2,FALSE),IFNA(VLOOKUP(A1,Sheet2!A1:B4,2,FALSE),IFNA(VLOOKUP(A1,Sheet3!A1:B4,2,FALSE),"Invalid Data")))
If I understand correctly, you need to determine if the first VLOOKUP is found, and if not, use the second VLOOKUP. Thats super simple. Assuming the formula Profex provided doesn't work:
=IF(IFERROR(VLOOKUP("A1", Sheet2!A1:B4, 2, False), "Err")="Err",IFERROR(VLOOKUP("A1", Sheet3!A1:B4, 2, False), "Not Found"), VLOOKUP("A1", Sheet2!A1:B4, 2, False))

Inconsistent recognition of ranges (No errors thrown)

This is code from Excel 2010. All of the code resides within the workbook itself. Information is gathered using internal forms and the code is run.
I have code that retrieves data from a spreadsheet and populates an object with that data. The row number is dynamic as it is dependent on the form input. The column is by the header, not the column number. The following code works perfectly but for two anomalies:
cTank.RowForTankSpecs = rNum
cTank.MP = .Cells(rNum, Range("MP").Column).Value
cTank.Form = .Cells(rNum, Range("formName").Column).Value
cTank.TankProcess = .Cells(rNum, Range("Process").Column).Value
cTank.Location = .Cells(rNum, Range("Location").Column).Value
cTank.TankName = .Cells(rNum, Range("Tanks").Column).Value
cTank.tankID = .Cells(rNum, Range("TankID").Column).Value
First:
The cTank.TankName is retrieving information from a column named "Tanks". That column does not exist. The actual column header is "Tank". But, it is retrieving the correct information. If I change the name to what it really is (Tank), it does not work.
Second:
When the cTank.TankID line is executed, I get the following error on the Range("TankID"):
Runtime Error 1004: Method 'Range' of object '_Global' failed
This one has the appropriate header (column header), but it is not recognizing the range.
I have tried simple things such as changing the order of the code, but it doesn't help. As earlier stated, the other lines work. Later in the program, information is gathered in the same manner but using another worksheet from the same workbook, and none of them are working. I've double checked that strings are strings and integers are integers, etc. I've double checked the column headers match the range names. Nothing seems to jump out at me.
I would appreciate any input you may have on the situation.
Thanks in advance.
Steve
Ok. Being pretty sure my code was correct, I went to the spreadsheet itself. For some reason it was recognizing only certain columns and it was recognizing one of them incorrectly. So I started highlighting the columns that worked and also the columns that didn't. What I noticed was that on the columns that were being recognized, that column header was displayed where the cell location is normally displayed whereas on the columns that were not being recognized, the cell location (i.e. A1, A2, etc.) for the header was being displayed and not the header title itself. The incorrect label was showing up for one of them. As it turns out, the mislabeled column was one that I had used for a form dropdown menu. So, I checked the name manager, and the ones that were working were listed. So anyway, using the name manager, I added named ranges using the headers. Now, when I select the columns, the column header(named range) appears in that window and now, the code works.
Thanks guys for your input. I really appreciate it.
Two things you can do:
Do not use use Range, but as it seems you are using names, use Names("Yourname").Referstorange.
OR
Make sure your names are set up correctly using the Name Manager in Data Ribbon.

Column references in formulas

I am a little stuck at the moment. I am working on an array of data and need to find a way to input column numbers into formulas.
-I have used the match function to find the corresponding column number for a value.
ex. "XYZ" matched with Column 3, which is equivalent to C1:Cxxxxxx
-now for inputing the C1:Cxxxxxx into a formula to get data for that particular column, I would like to be able to directly reference the Column 3 part, because I plan on using this workbook in the future and the column needed to run the calculation may or may not be column 3 the next time I use it.
- is there any way to tell excel to use a formula to tell excel which column to use for an equation?
so a little more detail, I have the equation
=AND(Sheet3!$C$1:$C$250000=$A$4,Sheet3!$B$1:$B$250000=$B$4)
instead of specifying to use column C, is there a way to use a formula to tell it to use C?
EDIT: more additional info;
"i am basically running the equivalent of a SQL where statement where foo and bar are true, I want excel to spit out a concatenated list of all baz values where foo and bar are true. ideally i would like it to ONLY return baz values that are true, then I will concat them together separately. the way I got it now, the expression will test every row separately to see if true; if there is 18K rows, there will be 18K separate tests.. it works, but it's not too clean. the goal is to have as much automated as possible. *i do not want to have to go in and change the column references every time I add a new data arra*y"
Thanks
You can use INDEX, e.g. if you have 26 possible columns from A to Z then this formula will give you your column C range (which you can use in another formula)
=INDEX(Sheet3!$A$1:$Z$250000,0,3)
The 0 indicates that you want the whole column, the 3 indicates which column. If you want the 3 can be generated by another formula like a MATCH function
Note: be careful with AND in
=AND(Sheet3!$C$1:$C$250000=$A$4,Sheet3!$B$1:$B$250000=$B$4)
AND only returns a single result not an array, if you want an array you might need to use * like this
=(Sheet3!$C$1:$C$250000=$A$4)*(Sheet3!$B$1:$B$250000=$B$4)
You could use ADDRESS to generate the text, you then need to use INDIRECT as you are passing a string rather than a range to the fomula
=AND(INDIRECT(ADDRESS(1,3,,,"Sheet3") & ":" & ADDRESS(250000,3))=$A$4
,INDIRECT(ADDRESS(1,2,,,"Sheet3") & ":" & ADDRESS(250000,2))=$B$4)
Obviously replace the 3s and 2s in the ADDRESS formulae with your MATCH function you used to get the column number. The above assumes the column for $B$1:$B$25000 is also found using `MATCH', otherwise it is just:
=AND(INDIRECT(ADDRESS(1,3,,,"Sheet3") & ":" & ADDRESS(250000,3))=$A$4
,Sheet3!$B$1:$B$25000=$B$4)
Note a couple of things:
You only need to use "Sheet3" on the first part of the INDRECT
Conditions 3 and 4 in the ADDRESS formula are left as default, this
means they return absolute ($C$1) reference and are A1 style as
opposed to R1C1
EDIT
Given the additional info maybe using an advanced filter would get you near to what you want. Good tutorial here. Set it up according to the tutorial to familiarise yourself with it and then you can use some basic code to set it up automatically when you drop in a new dataset:
Paste in the dataset and then use VBA to get the range the dataset uses then apply the filter with something like:
Range("A6:F480").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Sheets("Sheet1").Range("A1:B3"), Unique:=False
You can also copy the results into a new table, though this has to be in the same sheet as the original data. My suggestion would be paste you data into hidden columns to the left and put space for your criteria in rows 1:5 of the visible columns and then have a button that gets the used range for your data, applies the filter and copies the data below the criteria:
Range("A6:F480").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Sheets _
Range("H1:M3"), CopyToRange:=Range("H6"), Unique:=False
Button would need to clear the destination cells first etc, make sure you have enough hidden columns etc but it's all possible. Hope this helps.