M Function to trim specific range - excel

I need to trim in Power Query a column that has the following structure:
"ABC (XI 011)"
"ABC (XI1 02)"
I need to trim/ get every value between "(" and " ".
And I need to trim/ get every value between " " and ")".
For the 2 examples above the result should be for the first column:
XI
XI1
And for the second column:
011
02
Is there any chance to get this result with Power Query functions?

Use such code:
let
Source = #table({"col"},{{"ABC (XI 011)"}, {"ABC (XI1 02)"}}),
split = Table.SplitColumn(Source, "col", (x)=>Text.Split(Text.BetweenDelimiters(x, "(", ")")," "))
in
split

Related

IF and RIGHT formula

I am attempting to extract two digits from the right side of a string.
The input string can end with digits or text. For example:Hitch Rack - 4-Bike and Mountain-100 Silver, 38.
My end goal is to pull the last two digits if they exist into another cell. If the initial string doesn't end with the digits, I want to output a " ".
Currently, my brain is telling me this, where [#Product] is the input cell string value:
=IF(ISNUMBER(RIGHT([#Product],2)),RIGHT([#Product],2)," ")
Using the two input examples above I should get: " " (space) and 38 as the outputs respectively.
Try typing *1 after the right() formula:
=IF(NOT(ISERROR(RIGHT([#Product],2)*1)),RIGHT([#Product],2)*1," ")
If you use left/right/mid in excel, it returns the output as a string (e.g. "38") which is text, not a number. Multiplying by 1 makes it a number, if the return values are letters you get an error.
One more method pasted below -
=IFERROR(IF(NUMBERVALUE(RIGHT(H8,2)),RIGHT(H8,2)," ")," ")

Extract location of string in Excel sheet using readtable

I am writing a program that will use readtable to manipulate data that is read from an Excel spreadsheet such as the one below:
A B C D E F...
1 " " " " " "
2 " S 3 K3 " "
3 " " " " " "
4 " " 10 3C " "
5 " G 7 U " "
6 " " " " " "
.
.
.
I must extract the original location of some of the data, that is I need to store the location of 'S' as 'B2' and 'K3' as 'D2' etc. after they are read from the spreadsheet. To do this, I use readtableand then search for the location in the spreadsheet that contains the desired string; however, this requires that the entire spreadsheet be loaded into MATLAB.
I don't know in advance the number of empty rows or columns before 'S', nor do I know if some data will be filled in any of the columns in the rows above it. Additionally, I don't want the variable names to be generated automatically.
readtable('FilePath', 'ReadVariableNames', false, 'Range', 'myrange')
allows for not reading the variable names but doesn't allow for a dynamic range, i.e. the default option will skip the blank space and a specified range can't be dynamic.
opts=detectImportOptions('FilePath');
opts.DataRange='A1'
readtable('FilePath', opts, 'ReadVariableNames', false)
allows for specifying a start of the range but opts=detectImportOptions overwrites the 'ReadVariableNames' option, and the program reads the variable names anyway. The only workaround that I found was to use readtable without opts and to specify a range larger than the largest anticipated data set and then to trim empty rows and columns, but this is slow and clumsy. Is there a workaround?

Multiple returns from a cell lookup

I have text string in cells that I want to interrogate and then have all matching text values displayed in one cell.
I'm currently using:
=IF(ISNUMBER(SEARCH("horse",G360)),
"horse",
IF(ISNUMBER(SEARCH("cat",G360)),
"cat",
IF(ISNUMBER(SEARCH("monkey",G360)),
"monkey",
IF(ISNUMBER(SEARCH("donkey",G346)),
"donkey"))))
However of course this only shows the first matching value not ALL matching values. How would I do this?
If a cell contained text "blah cat blah blah monkey blah blah horses" the formula result would be "Horse, Monkey" not just "Horse".
In the sample even CAT will also come in I suppose.
For limited number of items case you could try:
=SUBSTITUTE(TRIM(CONCATENATE(
IF(ISNUMBER(SEARCH("horse",G360)),"horse "," "),
IF(ISNUMBER(SEARCH("cat",G360)),"cat "," "),
IF(ISNUMBER(SEARCH("monkey",G360)),"monkey "," "),
IF(ISNUMBER(SEARCH("donkey",G346)),"donkey "," ")
))," ",", ")

How to add space in a string in excel vba?

how can i add space in this example string?
Input value in cell A1 is ABCDEFGHIJK and will be paste in another cell B1 with a format of ABCDE FG HIJK.
One formula to insert a space:
=LEFT(A1,5)&" "&RIGHT(A1,LEN(A1)-5) to insert a space after the 5th position. (ABCDE FGHIJK)
One to insert 2 spaces as per example:
=LEFT(A1,5)&" "&MID(A1,6,2)&" "&RIGHT(A1,LEN(A1)-7)
Input ABCDEFGHIJK, result ABCDE FG HIJK
In short: Use =LEFT(), =RIGHT() and =MID() to get parts of your string and concatenate the parts and your spaces.
Edit:
In VBA:
Public Function StringWithSpaces(inpStr As String) As String
StringWithSpaces = Left(inpStr, 5) & " " & Mid(inpStr, 6, 2) & " " & Right(inpStr, Len(inpStr) - 7)
End Function

Sort out dimensions listed incorrectly/only keep data in a certain format

I have a list of around 1500 items with dimensions, but the dimensions do not all have the same format. The dimensions I want to keep are listed as L x W x H. How can I sort the dimensions listed like this from the stuff I don't want (some are listed as only L x H, Diameter, or just gibberish, etc.) Thank you.
If by gibberish you mean text values that could include <space>x<space> then you have some real problems. However, it it can be reasonable assumed that the L x W x H format is what you want and the only values that contain 2 occurrences of <space>x<space> are valid ones then a helper column would identify the valid entries.
In an unused column to the right put this formula into the second row.
=ISNUMBER(FIND(" x ", $A2, FIND(" x ", $A2) + 3))
Fill down as necessary. The results should resemble the image below.
        
Use Data ► Sort & Filter ► Filter to filter your Helper column for FALSE. These entries can be deleted and when you turn the filter off you will be ;left with valid entries.
Elaborating on #jeeped's answer, if you are dealing with data from an external source, you might want to relax your rules to allow other valid input formats:
There must be exactly three numbers, all non-negative integers.
A decimal point is allowed, but no digits after the decimal point.
They can be separated by "x" or "X" or "*".
They can have extra spaces before, after or between the numbers, but not between the digits.
That would mean these values would all be OK:
17x12x13
100 * 50 * 2
100. X 200. X 300
Problems of this sort are ideally suited to regular expressions. The RegExp feature can be added in Code editor with Tools > References, then check "Microsoft VBScript Regular Expressions". Then try this VBA function:
Public Function IsNxNxN(s As String) As Boolean
With New RegExp
.Pattern = "^\s*(\d+)\.?\s*[xX*]\s*(\d+)\.?\s*[xX*]\s*(\d+)\.?\s*$"
With .Execute(s)
IsNxNxN = (.Count = 1)
End With
End With
End Function
In jeeped's sample worksheet, you would replace the B2 formula with:
=IsNxNxN(A2)
If you are trying to clean up the data as well as filter it, you could use this:
Public Function CleanupNxNxN(s As String) As String
With New RegExp
.Pattern = "^\s*(\d+)\.?\s*[xX*]\s*(\d+)\.?\s*[xX*]\s*(\d+)\.?\s*$"
With .Execute(s)
If .Count = 1 Then
With .Item(0)
CleanupNxNxN = .SubMatches(0) & " x " & _
.SubMatches(1) & " x " & _
.SubMatches(2)
End With
End If
End With
End With
End Function
and set the formula for C2 to:
=CleanupNxNxN(A2)
Any dimension values that are invalid will report False in column B and blank in Column C. Valid dimensions such as " 10. x 20X30 " would be reformatted as "10 x 20 x 30".
If you would like to allow extra "gibberish" before or after the dimensions, you could remove the "^" and "&" anchor characters from .Pattern, and get:
"approx. Size: 10*20*30 feet" would yield: True, "10 x 20 x 30"

Resources