I have a string that often looks like this:
X-POTATO-2D-AB3F-N
It will always be in that format, and I want to compare "AB3F"to another string of four characters. Here's the catch, certain values in this string may change in length, so I cannot compare simply "the first four of the last 6 characters"
What will always stay the same is the number of dashes. Always at least 4 in total (on rare occasions 5), and the string that I want to compare will always be between the last two. Is it possible to use these to isolate the 4 or 5 characters that I want to compare?
I know Stack overflow doesn't like vague questions. Sorry! :D
Here you go:
http://excel.tips.net/T003324_Finding_the_Nth_Occurrence_of_a_Character.html
Essentially, this formula:
=FIND("¬",SUBSTITUTE(A1,"-","¬",4))
Where A1 is your string. This will return the position of the 4th "-". (Because substitute replaces the 4th "-" with a "¬", which is unlikely to appear in the code, and find then finds the "¬")
Edit, just realised that the 4th group may also have more than 4 digits, so you could retrieve it with the overcomplicated:
=MID(A1,FIND("¬",SUBSTITUTE(A1,"-","¬",3))+1,FIND("¬",SUBSTITUTE(A1,"-","¬",4))-FIND("¬",SUBSTITUTE(A1,"-","¬",3))-1)
Hope this helps!
Since you mentioned there could be four or five hyphens, and that your desired value will always be between the last two, I suggest the following:
=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",99)),198),99))
Replace each hyphen with 99 spaces (much longer than the longest substring)
The Right most 198 characters will surely have the last two substrings
Left (...,99) will surely return the first of those two
Trim to get rid of the extra spaces
This approach will work with four or five hyphens.
If you are going to use this in a Macro, the following code will return the "Next To Last" hyphen-delineated substring within a string:
Dim V As Variant
V = Split(S, "-")
NextToLast = V(UBound(V) - 1)
Related
I have a numeric text file whose data need to be tabulated. Unfortunately, depending on the input data, this text file shows numbers which are not separed, one to each other, from a fixed number of spaces and unfortunately this number of space could not be the same from a row to another.
I tried this:
For Each line In RichTextBox2.Lines
If Not line.Trim.ToString = "" Then
Dim item() As String = line.Trim.Split(" "c)
DataGridView1.Rows.Add(item)
End If
Next
and then I used to erase the blank columns. But I realised that when the number of space is different on the rows, I have data, that should fill the same column, that are on two or three different columns (with spaces). Let me provide an example:
Here there are the numbers coming from the text file and here are the tabulated numbers.
What I would do, is to erase all the spaces except one, in order to have these numbers equally spaced one from another. Hoping to have been clear, I thank you so much for the support
I am trying to get the last word in a cell however, it does not seem to be appearing getting the last word of the cell. It seems to get the partial match and not working correctly. I am using =IFERROR(RIGHT(AP2,SEARCH(" ",AP2)-1),AP2). This works great for getting the first word in a cell even where the first word is the only word in a cell. Any ideas on why this is not getting the last word correctly?
You may try this UDF which can be used in another sub routine or on the worksheet as well.
Function GetLastWord(ByVal Str As String) As String
Dim arrStr
arrStr = Split(Str, " ")
GetLastWord = arrStr(UBound(arrStr))
End Function
If you need to use it on the worksheet, assuming your string is in A1 then try this...
=GetLastWord(A1)
Your formula is based on the first occurrence of a space in the string.
For example with "Man City V Stoke" in the formula SEARCH(" ",AP11) is looking for the first occurrence of a space which occurs at position 4. You are then subtracting 1 from this and using this as the number of characters to return from the right of the original string. So you get Right("Man City V Stoke",3) which is "oke".
A better way is a formula such as
=TRIM(RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11)))
find text after last space
The part written: =SUBSTITUTE(AP1," ",REPT(" ",LEN(AP1)))
This inserts spaces the length of the string (16) in place of every occurrence of a space i.e.
Man City V Stoke
When you then do RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11))
you are guaranteed to only get whitespace followed by the last word. You then use TRIM to get rid of this white space.
As far as I can tell the below worked for me.
=TRIM(RIGHT(SUBSTITUTE(AP40," ",REPT(" ",100)),100))
I can only assume my other method is unreliable at getting the last word.
Information
I have a column of about 300 part numbers that is constantly expanding. These part numbers can be only numbers, only letters, or a combination of both. I'm using dynamic named ranges with the part numbers to fill combobox lists. When the user types in the part number in the combobox, if we've done that part before it will autofill as they keep typing. I've recently ran into a problem with this however with a recent group of parts that was added.The part number is just a six digit number, and are the only part part numbers that solely consist of numbers, every other one has letters, or if its just numbers there are hyphens in the part number. The combobox I'm filling with this named range does show these numerical part numbers in its list, but does not autofill them when the user types and does not autofill the other information associated with the part number in other textboxes when the part number is done being typed in.
Question
Can anyone help me understand why it isn't autofilling the numerical value but it fills every other value?
Code
Here is the code I'm using to fill the combobox list and the dynamic range formula. Let me know if you need anything else to help you give an answer.
=OFFSET('Part List'!$A$1,0,0,COUNTA('Part List'!$A:$A),1)
comboxPartNumber.List = Range("Part_Number").Value
List of Sample Parts
X-600-ASSY-SM
LO-5093-020-023
LO-5093-020-025
AB-1541754
764761
766415
I cannot add a comment, sice I am 2 points short :(, so I am forced to post this as a solution:
Try to make all the numbers strings.
Dim R As Range
For Each R In Range("Part_Number").Cells
If IsNumeric(R.FormulaLocal) Then R.FormulaLocal = "'" & R.FormulaLocal
Next
Let me know if it worked.
I'm building a spread sheet and have been trying to get conditional formatting working how I would like. The cells will contain either one or two Characters and I have been trying to get it to fill the Cell with RED if it's say an uppercase 'W' or ORANGE if its a lower case 'w'. There will only be about 10 characters allowed (upper and lower W H F S O) but I also need say a GREEN for the two characters 'wh'.
So far the formatting gives me RED irrespective of it being a 'W' or 'w'. I've tried different combinations of tests and a lot of searching the internet, any assistance would be welcome.
Thanks
String comparison is case agnostic in Excel, i.e. ="W"="w" evaluates to TRUE
You should use the =code(text) function to compare the ASCII codes of the cells in your conditional formatting formula
For instance =code("W") evaluates to ascii code 87 and =code("w") evaluates to ascii code 119
=code(text) only evaluates on the first character so you'll have to do something like =and(code(left(cell, 1))=119, code(mid(cell, 2, 1))=104) to capture the two character example "wh"
You may wind up with a lot of messy conditionals in the conditional formatting, so you could be better off doing all of that logic elsewhere in the spreadsheet
I was wondering if there is a way to test if the first letter in a table cell is capitalized without ripping the letter and comparing it to an array full of CHR codes or looping 26 instr() functions for every cell.
Basically, we have clients that send us tables in which the stub cell (far left) has part of the sentences on one line and then the rest on the line below, indented.
The issue is that I can't use the indents to test for these scenarios because other cells are indented for other reasons. I need to apply row shading depending on these scenarios and I'm having a hard time finding an efficient way to test for this.
This code returns 1
MsgBox (StrComp("This sentence continues", UCase("This sentence continues"), vbBinaryCompare))
This code returns 1 too
MsgBox (StrComp("this sentence continues", UCase("This sentence continues"), vbBinaryCompare))
Assuming you already have the character stored in a string strFirst:
StrComp(strFirst, UCase(strFirst), vbBinaryCompare)
would return 0 if the letter is uppercase.
If you do not already have the first character from the text then you'll need to extract it using Left(string, 1).