Test to see if first letter is capitalized - vba

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).

Related

Checking if a Word table's cell is empty - Empty cell containing a "marker"

This Word file has a giant table with 2 columns. I have to make a statement like:
If theCellIsEmpty, then
Fill it
End if
The thing is that when I get the content of the cell through:
FirstCellText = ActiveDocument.Tables(Tbl).Cell(Rw, 1).Range.Text
It doesn't get the "" I'm expecting, but it contains a sort of dot, so the text read is never empty.
Here is the dot I'd like to test for:
Moreover, I have no idea what this dot can be or where it came from. I tried to find it in the characters table without success.
Could someone explain me how to test the presence of this "end of line character" or the emptiness of the row? I don't want to check if the cell only has 1 character as if this character is something put there with a purpose, I don't want to delete it.
Also, every single line has this dot at the end...
What is it? How can I use/avoid it?
All you should need is something like:
Dim Cll As Cell
For Each Cll In ActiveDocument.Tables(1)
With Cll.Range
'if all it has is the end-of-cell marker, it's empty, so fill it
If Len(.Text) = 2 Then .Text = "Filler Text"
End With
Next
Thanks for all your help, I might have a simple solution thanks to Freeman, mostly :-)
I took the ascii number of the character to check what it is (through asc(xxxx) function) and it happens to be a "carriage return" having the ascii number "13"
Therefore, so I just need check if the content of the cell gives the ascii number 13 and it's done. (the "asc(xxxxxx)" gives the ascii number of only the first character of the text chain :) )
Of course, if the cell starts with a carriage return and there is text after, it won't be nice, so checking also if the text contained in the cell is only 2 char long will be a nice thing (this "carriage return" is apparently 2 char long... strange...) :)
If Asc(FirstCellText) = 13 and len(FirstCellText) = 2 Then

Comparing specific data from string within cells by locating via unique parameters

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)

VBA script in excel to find and highlight text

I am looking for a VBA script that will help me find certain keywords in a cell and if found highlight the entire row. Below are my requirements:
I have a database of words eg hell, get out, shut up, don't you dare etc. I need a macro to search the data in column "E" of excel and in case any of the cell in column "E" contains any word listed in the database (irrespective of the case of the word upper or lower)the entire row is highlighted. The word can be in the beginning, middle or end of the cell and the macro should be able to find that word and highlight the column.
Seeking help from all VBA masters for this.
You can do this with conditional formatting, instead of VBA.
Conditional formatting works by applying a 'second formula' to a given cell. If the 'second formula' results in TRUE, then special formatting conditions can be applied.
EXAMPLE CONDITIONAL FORMATTING
For example, if you have a single column of Data, A:A, and you want to check if that column has the exact string "hello world", you could add a conditional format [Home ribbon, Styles section, Conditional Formatting] that turns a cell yellow with this formula:
=$A1="hello world"
This will only result in TRUE if the cell in column A at that row equals exactly "hello world" [note that Column A has an absolute-reference $, and row 1 does not, so row 1 is relative to the position of the cell in the condiitonal format rule].
To check to see if any row in column A includes hellow world, we need to add a SEARCH function, which checks to see if a small search string is inside of a larger string:
=SEARCH("hello world",$A1)>0
Because SEARCH by default returns the first character in a larger string that matches the search term (and if it finds nothing, it returns #N/A), we check to see if our search for "hello world" in column A returns a number.
SEARCHING MULTIPLE COLUMNS
Now, to see if ANY column, say from A-D, includes "hello world", we concatenate each value of each column so that it gives us a single string, which we can search through for "hello world", like so:
=SEARCH("hello world",$A1&$B1&$C1&$D1)>0
This will first create a single string, equal to A1 & B1 & C1 & D1 all in a row. Then it will search that newly created string to see if "hello world" is inside it, and return a number value if it is.
ARRAY FORMULA BASICS
Finally, we need to do the tricky part - searching for multiple terms instead of just "hello world". This is called an Array Formula. An array formula works by performing a single operation on multiple cells, and then returning multiple results in an Array. In an Excel sheet, an array formula must be confirmed with CTRL + SHIFT + ENTER (instead of just ENTER), but in conditional formatting, you actually don't need to do anything special - it will recognize an array formula without a special command.
As an example of conditional formatting, see this example, which checks whether any value from A1:A5 = 10, and if it does, it gives us the value in B1:B5:
=IF(A1:A5=10,B1:B5,"")
Remember in Excel on a worksheet, this would be confirmed by pressing CTRL + SHIFT + ENTER. If you do test this, it will give you the following result, assuming A2 = 10 and A5 = 10:
={"";B2;"";"";B5}
This result would actually be hidden, because Excel can't "collapse" an array function on its own. So assume column B had values, and we actually want to sum them together. We would then wrap the Array formula in a SUM function:
=SUM(IF(A1:A5=10,B1:B5,""))
As you can see if you test this, we have actually created our own SUMIF function, using Array formulas instead of the built-in SUMIF.
SEARCHING FOR MULTIPLE TERMS WITH ARRAY FORMULAS
So now we apply these principles to the conditional formatting, to create an array formula which will check our concatenated 'NEW STRING' for any number of provided terms, as follow [Assumes the search terms are typed into cells E1:E10]:
=SUM(SEARCH($E$1:$E$10,$A1&$B1&$C1&$D1)>0)
This formula can be placed as a conditional formatting rule which reaches all of A:D. Set the rule to highlight / change format in whatever way you like.

Excel conditional formating exact match on Characters

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

Removing invisible question mark from text - #​E using vba

I have to read the text from the cells of a column in excel and search for it in another sheet.
say for example, the text in sheet1 column A is "Evoked Potential Amplitude N2 - P2." This has to be searched in sheet2 column C. This fails because a question mark appears before the "E" which is not present in the value in the sheet2.
Both are representation of same character in different application. Maybe someone might recognize it.
In the excel sheet I don't see any junk characters, but while handling it in the vb code I see a question mark before the word - Evoke.
This data was extracted from a share point application and this character (?) is not visible to the plain eye. Search and replace functions are not working in this case.
Unicode 8203 is a zero-width space. I'm not sure where it's coming from. It is probably a flaw in the way the data is imported into Excel which you haven't noticed before, but it might be worth fixing.
In the meantime, you can simply use the Mid() function in Excel VBA to remove the unwanted character. For example instead of
x = cells(1,1).value
use
x = Mid(cells(1,1).value,2)
which deletes the first character.