VBA Code for Multiple Text Search in one Column from List - multiple-columns

Need to search multiple text in Description column from List column (which will be in different Sheet later) if Found the exact Item from List in Description column it will be return in Result column with comma after each item.
Please note that from this code there is a problem - see in Description column below picture, its mentioned "I am testing for SMDB" but in Result Column its showing DB MDB and SMDB. The Result should me SMDB only. Please advice
Function aconcat(a As Variant, Optional sep As String = "") As String
Dim y As Variant
If TypeOf a Is Range Then
For Each y In a.Cells
aconcat = aconcat & y.Value & sep
Next y
ElseIf IsArray(a) Then
For Each y In a
aconcat = aconcat & y & sep
Next y
Else aconcat = aconcat & a & sep
End If
aconcat = Left(aconcat, Len(aconcat) - Len(sep))
End Function

As you did not include the matching function code, this answer is a guess.
You have DB, MDB, SMDB, ... as searched values.
The text I am testing for SMDB contains the text SMDB but it also contains MDB (as SMDB contains the text MDB) and DB (as SMDB contains the text DB).
You could split your Description using different separators (whitespace, comma, semi-comma ...) to get an array of words and then try to match these words with your searched values.

Related

VLookup not working on full numbered Value

I created a Google Spreadsheet (File Name Product Test) and I have an ID field on column A which contains the word ID plus some letters and numbers (Example "ID-KNYT-12345"). The KNYT part is different per ID, some KNYT some DMXF etc.
So in column B I added a custom formula (Sample below) which processes the ID. If KNYT only numbers are kept. If DMXF the DMXF part is included plus the numbers.
I then have a vlookup/importrange formula on column C which is supposed to use the converted value in column B to lookup the value from another sheet and retrieve a certain information.
The problem is if the converted value contains all numbers like 12345 the vlookup fails, "Did not find value in lookup evaluation". If the converted value contains letters and numbers like DMXF-25452 the lookup works. If I manually type 12345 on column A the lookup works. Why would the lookup say it didn't find a result when the value is there? More details below
I checked, all cells involved are in format Number>"AUTOMATIC".
I checked, the value 12345 is definitely found on the other sheet (Imported Range)
I checked these values online, I found no hidden characters or spaces
The data is from an email with attached Excel file. I don't download the file, I just click to preview it and copy-paste the entire table over to my Product Test spreadsheet
The custom Formula:
function Convert(Thevalue)
{
Thevalue = Thevalue.toString().replace("ID-KNYT-", "");
Thevalue = Thevalue.toString().replace("ID-DMXF-", "DMXF-");
if (Thevalue == "DMXF-2245"){Thevalue = "Evaluated";}
if (Thevalue == "DMXF-3268"){Thevalue = "Pending";}
return Thevalue;
}
The Vlookup (Not actual sheet url just a sample)
VLOOKUP($B1,IMPORTRANGE("https://docs.google.com/spreadsheets/d/feiugsdfjhsdkjfhiesdfjh-p-dsflkjgsdf/edit#gid=000222333","sheet1!$A:$C"),3,FALSE)
UPDATE: This seem to fix it for me. Looks like if the return value is all numbers and no letters it is a NaN issue
if (!isNaN(Thevalue))
{
return Number(Thevalue);
}
else
{
return Thevalue;
}
Your custom function returns text strings. The vlookup() function does not consider the number 123 and the text string "123" equal. To make it work, convert the lookup keys to text with to_text(), like this:
=vlookup( to_text($B1), Sheet1!$A:$C, columns(Sheet1!$A:$C), false )
As RemcoE33 said, the custom function seems superfluous, because the same thing can be done with a plain vanilla spreadsheet formula that employs regexreplace(), like this:
=arrayformula( regexreplace( to_text(A2:A), "ID(?:-KNYT)?-", "" ) )

How to combine many query in spreadsheet if one or more query is empty

Problem
I have some data from spreadsheet and want to match with date with query, and combine many query.
Question
My query will work if all col i choose is'nt empty. But my data (col) will not all be filled, some will be left blank. If I try with my query one data blank don't want to appear.
Example
=IFERROR({IFERROR(QUERY(dashboar_data;"SELECT C,I WHERE J = date'" & TEXT(DATEVALUE(B5);"yyyy-mm-dd") & "'";0);dashboar_data/0);
IFERROR(QUERY(dashboar_data;"SELECT C,M WHERE N = date'" & TEXT(DATEVALUE(B5);"yyyy-mm-dd") & "'";0);dashboar_data/0);
IFERROR(QUERY(dashboar_data;"SELECT C,Q WHERE R = date'" & TEXT(DATEVALUE(B5);"yyyy-mm-dd") & "'";0);dashboar_data/0)})
Its will work if 3 col is filled, i want if one col empty other query still work
You may try to fake data with iferror. Produce the same number of columns with empty values:
iferror (... , {""\""})

SUM values of all entries' cost column matching a list of items

Here's my sheet setup:
I have a sheet "Inventory" that holds rows of all my inventory items in stock. Every item has a value/column for "Cost", and a column for "Sale Price".
I have another sheet "Photography", that will have for every row/entry a cell which specifies which are the list of items used in the photo, referenced by some inventory item ref. And from that I want to vlookup or something, to fetch the "Cost" of all those items in the list, and SUM them up.
That way I can know for a specific photo, what are all the items used in the photo, and how much is the cost of all those items (summed up).
Then I will do the same for the "Sale Price", that way for every photo I take, I'll be able to tell the cost of that product item "as is" (with all the different inventory items), and the sale price.
Example:
I was able to do that for only 1 item, but I want the formula to work on the "list" of items in B9.
I even tried with this query formula, I just didn't know how to say "matching any item from the list", which will be a string in another cell:
=SUM(QUERY(A:B,"select B where A matches 'ID-100'"))
If your "items used" column consistently uses comma-space separated format for item lists, the following will work.
=query(Cost!A:B, "select sum(B) where A = '" & join("' or A = '", split(B9, ", ")) & "' label sum(B) ''")
Here, split splits the list of items used into individual item Ids. These are then joined by putting ' or A = ' in between, for example:
ID-101' or A = 'ID-101
There are bits prepended and appended to form the query string, e.g.,
select sum(B) where A = 'ID-101' or A = 'ID-101' label sum(B) ''
which does the job. The label part is necessary to get one-cell output, without a "Sum(Cost)" header getting in the way.
A detail concerning split: as written above, split(B9, ", ") splits by either comma or space (ignoring empty string in the output). So it will work even with ID-1,ID-2, ID-3 ID-4. On the other hand, this may be a problem if your IDs contain spaces. The stricter splitting mode is split(B9, ", ", False) which requires the whole string ", " to be used as a separator.

Find result then range and copy

I'm having some trouble trying to make a function that acts like vlookup but returns a range rather than a cell
The data to search through looks like this
Sometimes there is a space separating sometimes not
What I would like to do is to look up 16 from my main page and return all the values in that range.
the code I currently am using will only return the first line in a messagebox
Public Function findrulepos(target) As Range
Dim ruleStart, ruleEnd, ruleEnd2 As String
Dim RuleRange As Range
'Dim ruleEnd As Range
MaxRule = 100000
MaxRow = 100000
Set target = Sheets("main").Range("E2")
Sheets("ResRules").Select
For i = 3 To MaxRow
If CStr(ThisWorkbook.Sheets("ResRules").Range("A" & i).Value) = _
CStr(target.Value) Then
ruleStart = _
ThisWorkbook.Sheets("ResRules").Range("A" & i).Offset(0, 1).Text
Exit For
Else
End If
Next i
End Function
If we can assume that the numeric group labels in col A are sequential, then I think this will achieve what you need:
Enter the numeric label you are wanting to extract (16 in your example) into cell E1
Note that =MATCH(E1,A:A,0) gives us the row number where group 16
starts, which is the first row we want to copy. Similarly,
=MATCH(E1+1,A:A,0) gives us the row number where group 17 starts,
which is one row below the last row we want to copy. (Unfortunately
that's not true for the very last group of code, but to rectify that
you just need to add a dummy number at the very bottom of the data
in col A.)
Enter the formula =IF(ROW()+MATCH(E$1,A:A,0)-1<MATCH(E$1+1,A:A,0),INDIRECT("B"&MATCH(E$1,A:A,0)+ROW()-1),"") in F1. That should copy the first value of the selected code block -- [DO] ADD TO QUEUE P in your example.
Copy F1 down as many rows as the largest code block is likely to be.
The one problem with that is that it will put 0 whenever it copies a blank row. So you have to explicitly check for that case, e.g. by changing the formula in F1 to =IF(ROW()+MATCH(E$1,A:A,0)-1<MATCH(E$1+1,A:A,0),IF(ISBLANK(INDIRECT("B"&MATCH(E$1,A:A,0)+ROW()-1)),"",INDIRECT("B"&MATCH(E$1,A:A,0)+ROW()-1)),"")

How to refer to cell without A1 notation?

In my Access VBA I have hyperlink, which is using the following way to link to cell:
oSheet.Cells(1, i).Formula = "=HYPERLINK(""#Sheet2!E6"", """ & !TestCase & """)"
However, instead of E6, I want to use row,col notation, since all my internal application logic is using Cells/rows/cols.
Thanks.
Instead of Formulause FormulaR1C1(Row / Column format)
Here are two examples:
Set the formula of your cells to =$B$1 :
oSheet.Cells(1, i).FormulaR1C1 = "=R1C2"
Set the formula of A1 to =C2, A2 to =C3 etc.:
Range("A1:A10").FormulaR1C1 = "=R[1]C[2]"