I have a list of similiar links of website with equal base in Excel. At the and of the link there is numbers, e.g. *_100, *_1013, *_14 and so on.
I need to sort the list descending like
*_1013
*_100
*_14
(first 4-digit links, then 3-digit, then 2-digit)
Is there any possibility in Excel to sort this array in right way?
From your question the "*_XXX" is at the end of the string. So to get the values you need to
Indentify where the *_ occurs and extract that
Remove the *- from the extracted string (which I used SUBSTITUTE to do), then convert it to a value
=IFERROR(VALUE(SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND("*_",A1)-1),"*_","")),"no match")
If you don't mind an extra column you can extract the number, make it a real number with =value() and then simply sort largest to smallest. (note: prepending 0s, if any exist, would be lost during call to value function)
Related
I'm trying to sort a column in my excel spreadsheet that contains numbers ,strings and "empty". My goal is to sort the numbers descending, followed by empty, followed by strings descending as well.
Sample data with 4 rows is in the table below.
Columns("A:A").Sort key1:=Range("A2"), order1:=xlDescending
With a normal column sort, I can get the numbers and the "empty" in the right order (Failed Code). However, the String keeps on appearing before the numbers when sorting descending.
Initial Failed_Code Desired_Output
1 6566 String 6566
2 6566 700
3 700 700
4 String String
Normally you can specify the sorting order in a list and use the OrderCustom parameter of VBA sort, an example is in the accepted answer of Code an Excel VBA sort with a custom order and a value containing commas. However as you are sorting integers and strings the usually adviced helper/auxiliary column ( Custom sort in Excel by first character and user-defined non-alphabetical order., ) is an easier solution here too, especially as in custom lists no wildcards are possible (https://www.mrexcel.com/board/threads/custom-sort.471612/).
As ! is the first character following the control characters in ASCII table and Windows sorting order (What is the first character in the sort order used by Windows Explorer?) write a macro that creates the auxiliary column according to the first character in the cells you want to sort (How to check first character in a cell value), if this first character is a letter prepend an !, then sort by the auxiliary column in descending order.
An alternative an possibly easiest solution, would be in your case to first sort the entire column ascending, then determine the index where is the 'border' between integers and strings by checking the first character (How to check first character in a cell value) and then sort the integers and strings separately descending, because you can always specify the sorting range you do not have to necessarily sort tte entire column.
I have a string of letters and numbers, where if the second two characters of the string equal a certain value, then a location value should be shown in the corresponding column.
I have used the MID function to essentially extract the characters of the string that I want to use MID(A2,2,2) but now I can't figure out how to compare what is returned to a list of options that those two characters could be without typing in each option in an extremely long formula.
Here are possible strings that are situated in a column:
3PH356969
MSFFACEBUS
MBH0007398
MBH0007402
I am extracting the second two characters of these, to compare to a list similar to this
PH
SF
BH
PG
HR
These values then correspond to location (below), which would optimally be returned:
Philadelphia
Bay Area
Birmingham
Western PA
Hartford
I can write =IF(MID(A2,2,2)="PH","Philadelphia",else...) but then the else-ifs will go on for 76 more 2-character strings to compare against. I'm hoping there is a more optimal way for this.
Expected results should be the location corresponding to the string, or just "error" displayed.
Basically we need to use a lookup/reference table, but instead of a much more common VLOOKUP function we can use a much faster INDEX + MATCH combo.
Formula in B1:
=INDEX($E$1:$E$6,MATCH(MID(A1,2,2),$D$1:$D$6;0))
I would use a VLOOKUP, personally. Although it would require a separate lookup table, just feed your MID result as the VLOOKUP key. Then you could easily add/remove locations, and there will be an #N/A error if the key's not there.
If you don't want a separate lookup table, you may try it this way:
=IFERROR(INDEX({"Philadelphia","Bay Area","Birmingham","Western PA","Hartford"},MATCH(MID(A2,2,2),{"PH","SF","BH","PG","HR"},0)),"Not found")
What is the best way to find the right column for the travelled miles using visual basic coding or some excel function and return the price from that column? HLOOKUP can't be used here because the lookup value isn't exact and the ranges in the table are also not with specific intervals (If they were, I could use e.g. FLOOR(travelled miles/100)*100 and find the price with HLOOKUP). Obviously, it's easy to find the price manually with a small table but with a big table computer will be faster.
Note that, if x is between a and b, then MEDIAN(x,a,b)=x. Combine this with some nested IFs:
=IF(MEDIAN(B5,B1,C1-1)=B5,B2,IF(MEDIAN(B5,C1,D1-1)=B5,C2,IF(MEDIAN(B5,D1,E1-1)=B5,D2)))
I'm on my phone, so just done the first three cases, but hopefully you can see how it continues.
(should note you need to remove the dashes for this to work)
Edit:
I also want to answer your question in the comments above. You can use the following to keep the dash, but get a number to work with.
Assume cell A1 has got the value 10-. We can use the FIND function to work out where the - occurs and then use the LEFT function to only return the characters from before the dash:
=LEFT(A1,FIND("-",A1)-1)
This will return the value 10, but it will return it as a string, not a number - basically Excel will think it is text. To force Excel to consider it as a number, we can simply multiply the value by one. Our formula above therefore becomes:
=(LEFT(A1,FIND("-",A1)-1))*1
You may also see people use a double minus sign, like this:
=--LEFT(A1,FIND("-",A1)-1)
I don't recommend this because it's a bit complex, but combining with the formula above would give:
=IF(MEDIAN(B5,--LEFT(B1,FIND("-",B1)-1),--LEFT(C1,FIND("-",C1)-1)-1)=B5,B2,IF(MEDIAN(B5,--LEFT(C1,FIND("-",C1)-1,--LEFT(D1,FIND("-",D1)-1-1)=B5,C2,IF(MEDIAN(B5,--LEFT(D1,FIND("-",D1)-1,--LEFT(E1,FIND("-",E1)-1-1)=B5,D2)))
I have cells in excel containing data of the form v-1-2-1, v-1-2-10, v-1-2-100. I want to convert it to v-1-2-001, v-1-2-010,v-1-2-100. I have nearly 2000 entries
If all of the data follows the format shown then you could use FIND to return the position of '-'. There will be three instances of this character and you need to find the third one so use the position given by the first instance as the start position parameter of the second FIND and again for the third (essentially nesting FIND). Once you have the position of the third '-' you know where the final set of numbers are (from the returned third position+1 to the LEN of the string) and could use SUBSTITUTE or a combination of other excel string functions to configure the final portion as you need it.
I'm assuming that excel has your data formatted as text.
If you need further assistance I'm happy to knock up the formula in excel but I'm off to work now and won't be able to do so for around 9 hours.
Please try:
=LEFT(A1,6)&TEXT(MID(A1,7,10),"000")
Suppose I have a list of two-word pairs in a column in Excel. These words are delimited by a space so that a typical pair might look like "extreme happiness". The goal is to search for these 'bigrams' in a larger string located in another column. The issue is that the bigram will only be found if the two words are together and separated by a space. What would be preferable is if Excel could look for both words anywhere in a given larger string. It is crucial that the bigrams occupy one cell each since a score is assigned to each bigram and in fact the function used VLOOKUPs this value based on the bigram cell value. Would it make sense to change the space between any two words to a - or some other character? Is there a way to have Excel look up each value one at a time (perhaps by recognizing this character and passing through the larger string twice, that is, once for each word)?
Example: "The weather last night was extremely cold, but the warm fire gave me some happiness."
Here we would like to find both the word 'extreme' within the word extremely and the word happiness. Currently Excel would not be successful in doing this since it would just look for "extreme happiness" and determine that no such string exists.
If the bigram in the row below "extreme happiness" reads "weather gave" (for some reason) Excel will go check whether that bigram exists in the larger string and return a second score. This is done so that at the end every score can be added together.
This is pretty easy with a couple of formulas. See screenshot below:
The logic is simple. Assuming your bigram is in B1, we can input the following in C1. This will replace the spaces with *, which is Excel's wildcard character.
=SUBSTITUTE(B2," ","*")
Then we concatenate it to give us a wildcarded beginning and end.
=CONCATENATE("*",SUBSTITUTE(B2," ","*"),"*")
We then use a simple COUNTIF against the statement (here in A1) to return to us a count of occurence.
=COUNTIF(A2,CONCATENATE("*",SUBSTITUTE(B2," ","*"),"*"))
A simple IF check enclosing the above, with condition >0, can be used to give us either Yes or No.
=IF(COUNTIF(A2,CONCATENATE("*",SUBSTITUTE(B2," ","*"),"*"))>0,"Yes","No")
Let us know if this helps.