Query several ranges and add automatically a column to know the source of each row - sql

I am trying to achieve the following in Google Spreadsheets.
First, I want to query several ranges (in different sheets from the same spreadsheet). I tried a formula like this =query(arrayformula({indirect(E2:E10)}),"select * where Col1 <>''") with no success
In E2:E10 I have a list of ranges. Column F contains a name that describes the source of the value in Column E.
My second problem is that I need to add a column to the output of that query that tells me the origin of each row.
If the sources are ranges of 3 columns by country I need to merge those tables and add that country to each row.

All credits to +Ben Liebrand who helped me out here: https://support.google.com/docs/profile/3464
"I just want to start of by saying that the indirect() function does not work in an arrayformula() function as expected. So you will need to take another approach. I can understand what you are trying to do so I added another TAB in your spreadsheet to demonstrate another approach. I know it was initially a specific design you were trying so I made some changes to what you had. Maybe you can take a look at what I have offered and maybe you can tweak your design.
I know what I am offering is just very rough but you will also notice that I removed the end row specifier from your ranges in the range table.
Don't assume my example to be the final result but I was just trying to show that the range you were trying to use with the indirect() function will not work.
So hopefully this will give you a new idea of how you can maybe handle this.
My formula also adds the country to each of the tables in the output. My formula looks like this
=query(ArrayFormula({
if(len(indirect(regexextract(F2,"\w+\!\w+")&":A")),G2,),indirect(F2);
if(len(indirect(regexextract(F3,"\w+\!\w+")&":A")),G3,),indirect(F3);
if(len(indirect(regexextract(F4,"\w+\!\w+")&":A")),G4,),indirect(F4);
if(len(indirect(regexextract(F5,"\w+\!\w+")&":A")),G5,),indirect(F5);
if(len(indirect(regexextract(F6,"\w+\!\w+")&":A")),G6,),indirect(F6);
if(len(indirect(regexextract(F7,"\w+\!\w+")&":A")),G7,),indirect(F7)
})," select * where Col1 <> '' ")
Hope this is of some help to you"
And I hope is useful to the community
Gerónimo

Related

VBA : re-sort/re-order rows based on column values through specific range

I'm fairly new to VBA, and have found this to be a great resource. I understand enough to generally find someone else's code and modify it to what I need it to do, but I've been stumped here. I've tried coding this several times in different ways, but can't seem to get it to work, and am not overly convinced I'm approaching it the right way to start with. I'm hoping someone is willing to help me.
Here's a theoretical example of what I'm trying to do.
I have a sheet that contains groups of orders of a certain product type with their quantities listed by the step of the process they're currently at:
Current orders in process
I need to "sort" them or re-order them so that:
1. The orders are copied into a tab based on process step, with the furthest process step being listed first working back to the closest, and...
2. Orders listed in the "complete" step need to be excluded completely, and...
3. I need to do this for multiple product types, which have different numbers of process steps, so the end column needs to be identified somehow (as a dynamic variable, or some trigger)
For the example, it would look like this:
Sorted data
I've thought through a half-dozen ways to logically go about this and tried to figure out how to then do that with code, but I'm stuck ... and I have the feeling that there is probably 1 or 2 "best" ways to go about this that someone in this community probably knows off the top of their head. Thank you in advance.
Edit: Here's my first attempt at coding the outline. I realize there are some components missing, but would love feedback on whether this seems to be a usable, or efficient approach.
Sub test1()
Dim skuStartRow As Long, skuEndRow As Long
Dim qty As Integer
Dim colCheck as long, lastCol as Long
skuStartRow = .Range("A:A").Find(what:=sku, after:=.Range("A1")).Row
skuEndRow = .Range("A:A").Find(what:=sku, after:=.Range("A1"), searchdirection:=xlPrevious).Row
' Code here to identify last column (lastCol) either by .UsedRange or by header?
For colCheck = 3 To lastCol
For rowCheck = skuStartRow To skuEndRow
qty = Cells(rowCheck, colCheck).Value
If qty <> "" Then
'copy current row and paste into destination worksheet
MsgBox qty 'test to see if correct qty is displayed
End If
Next rowCheck
Next colCheck
End Sub
Update:
Still having problems.
I've copied/pasted the whole range of orders based on product type to a new sheet where it will be sorted.
I've inserted a column and used the match/index formula provided by Variatus (autofill down) to create an index column.
Insert new column and use RANK function to create a rank ordered list, as there are duplicates in the Helper/Index column.
Used INDEX/MATCH functions to return values into rank ordered lists for both work order number and quantity value.
The code I currently have works - except that it's hard-coded using FormulaRICI = ... commands. I'm having major challenges figuring out how to convert the code to allow use of MATCH/INDEX functions with variables. I need to do this b/c depending on product type there are varying amounts of total orders, as well as varying amounts of number of steps; so I need to have a dynamic range for both rows/columns.
I've looked at a bunch of examples but can't seem to find something that works correctly. The most promising I've seen appears to be using the "replace" function in VBA, but my head's about to explode.
Any suggestions are much appreciated.
I wonder if you really need VBA for this. Please consider these steps.
Insert a column in your worksheet with this formula in row 2, copied down.
=IFERROR(MATCH(TRUE,INDEX($D2:$I2<>0,0),0),0). The range D2:I2 is defined by D2 = Step 1 and I2 = "Complete". The formula finds the first non-blank cell in the range. The column could be hidden.
Sort the sheet on the helper column, largest to smallest.
Apply a filter.
Filter on the product.
What you see at this point is the list you wish to create using VBA, however including the 'Complete' column and any other columns to the right of it.
There are a number of ways of automating this process. You might write code to either insert the formula in the helper column automatically or its result. This could be done when needed, without having a permanent extra column. You could write code to do the sorting and filtering automatically, for example, based on a currently selected product. And, finally, you might write code to copy the result to another sheet, remove the unwanted columns there, and restore the original sheet to its original condition (presumably sorted by order number).

Multi Column Vlookup and Place an 'X' where File Name and ID Intersect

I’m trying to search for a workbook name and find any ID numbers in one of 13 columns, and if there is a match, place an ‘X’ in a cell on another sheet. Since a picture is worth 1000 words, I’m posting a couple images to illustrate the point.
I have a small UDF that strips out numbers from alpha-numeric cells. This gives me an ID number. In a few cases, I do need an alpha-numeric ID, like LQ2. Anyway, I want to look for these IDs in any row in 13 columns and get the workbook name from the same row.
Then, I want to match this against the same workbook name in another sheet, and if an ID is found in any of those 13 columns, put an x in the cell where the workbook name and ID intersect.
I tried to pivot the data, but I couldn't get it working. I'm thinking that some kind of pivot solution should work. Either a code solution or a formula solution would be great. I'm trying to put together a VBA script that, hopefully, will get me close. I'll post back when I have made more progress on this.
My thanks to all.
Quick update: I tried a double match, like this . . .
=MATCH($B2&C$1,'Tasks & Remarks Split'!$Q$2:$AC$966&'Tasks & Remarks Split'!$A$2:$A$966,0)
This is CSE-entered. I thought it would pickup the matches, but it returns all #N/A results. Something is off here, because I know there are several matches. Can someone offer a tip?
I ended up doing a sumproduct against 2 criteria and if the count is >0 then put an 'X' in the cell. It seems to work just fine.
=IF(SUMPRODUCT(--('Tasks & Remarks Split'!$A$2:$A$966=C$1)*('Tasks & Remarks Split'!$Q$2:$AC$966=$B2))>0,"X","")

extract data in exel sheet using macro

you most probably going to think "what an idiot" but remember i never done any type of coding before so this is all new to me,
My problem are that i'm working on a HUGE excel sheet with loads of data that is not needed. i need to sort the data into a few columns, i only need column "A,K,AN,AQ" but in column "AS" i only need certain values (yes,no,blank) i only want the yes and blank values. like i said never done any coding before but i know that you can use an macro to do it so please help, how do i go about this?
before trying to get into macros, try to use functions with if else statements. They are quite easy to handle. Like: If (yes) then put it into X. Later, you could select all needed. Also, check the, how the dollar sign is used
use this links to see, if it is something for you.
One quick and dirty way of getting this job done would be to:
Delete the columns you don't need.
Select all cells in the range you're interested in, click the Insert menu, and choose "Table". If your columns have titles, select the box for "My Table has Headers."
-This turns your data into an array so that Excel recognizes that each row is an entry (instead of thinking that the cells are unrelated).
Now you can use the filter icon in the column headers to select and display only the rows containing the values in column X that you're interested in.
Note that there are some limitations to what the table feature is good for, so, as always, whether this is a good solution for you depends on what you want to do with the data.

Create list from data range

I was hoping to get a bit of help regarding formulas in Excel/VBA, I'm looking to do the following but I'm not sure how best to do it:
Okay, so I have a materials export from a piece of software and it comes out with the following sort of data:
In essence I would like to have a catalogue stored on a separate sheet which the function references for text strings that may be contained in a takeoff.
i.e. in E2 the function will check the column B for cells containing the text strings in A16:A18 (the consoles) and return any results along with the corresponding quantity to columns E and F. Similarly this will occur for the controller and cables.
I'm sure there's a solution somewhere but I've been unable to find one for lack of knowing what to search exactly.
Thanks for any help guys, it's appreciated.
What you may want to use is the COUNTIF function. Try this:
COUNTIF(A$2:A$13,"*" & A16 & "*")
If I am not mistaken you can check for the values in A16:A23 in the database dump column B1:B13, then you want the summer quantity for the matching values, where a match means the text you look for must be contained in the database dump column.
To get the quantity accordingly use the following formula in cell B16:
=SUMIFS(C$2:C$13;B$2:B$13;"*"&A16&"*")
then drag it into the other cells. The asterisks are text wildcards, so there may be text in front or behind the text part we look for.

Excel - How do I find all relevant rows by typing unique invoice# listed Col A

I have a Worksheet with 10 columns and data range from A1:J55. Col A has the invoice # and rest of the columns have other demographic data. Goal is to type the invoice number on a cell and display all the rows matching the invoice number from col A.
Besides auto filter function, the only thing comes to my mind is VBA. Please advice what is the best way to get the data. Thanks for your help in advance.
Alright, I'm pretty proud of this one. Again avoiding VBA, this one uses the volatile formula OFFSET to keep moving its VLOOKUP search down the table until it's found all matches. Just make sure you paste enough rows of the formula that if there are many matches, there's room for all of them to appear. If you put a border around your match area then it would be clear if you ever ran out of room and needed to copy down the formula some more.
Again, in the main section, it's just a single formula (using index):
=IFERROR(INDEX($A$1:$J$200,$M3,MATCH(N$2,$A$1:$J$1,0)),"")
This gets to be so simple because the hard work of the lookup is done by an initial column which looks up the next row that matches the invoice number. It has the formula:
=IFERROR(MATCH($L$2,OFFSET($A$1:$A$200,M2,0),0)+M2," ")
Here is the working example that goes with those formulas:
Let me know if you need any further description of how it works, but it mostly uses the same rules as above so that it's robust in copying and moving around.
I've uploaded the Excel file so you can play with it, but everything you need to reproduce this feature should be in this solution.
Google Docs - Click link and hit Ctrl+S to download and open in Excel.
A popular solution to this problem is a simple VLookup. Lookup the invoice the user types in on the table A1:J55, and then return an adjascent column's data.
Here's an example of it working:
The formula in the highlighted cell is:
=VLOOKUP($L3,$A:$J,MATCH(N$2,$1:$1,0),FALSE)
What's nice about this formula is you only need to type it once and then you can copy it across and it'll automatically pick out the correct column of the table (that's the match part). The rest is very simple:
The first part says lookup value $L3 (the invoice number typed in),
The second part says look it up in range $A:$J (which is where your table is located). I've shown how you can select the entire columns $A:$J so that you can add and remove data without worrying about adjustin the range in your lookups. (Excel takes care of optimizing the formula so that unused cells aren't checked)
The third part picks the column from which the resulting data will be drawn once a matching row is found.
The FALSE part is an indication that the invoice number must match exactly (no approximate matching allowed)
The $ signs ensure that fixed ranges like the location of your source table ($A:$J) and your lookup value ($L3) don't get automatically changed as you copy the formula across for multiple columns.
The formula is pretty easy to adapt if you want to move around your table and the area where you do your lookup. Here's an example:
Bonus
If you want to add a little spiff, you can add a dropdown to the Invoice # field so that the user gets auto-completion and the option to browse existing values like so: