VBA syntax to find an if conditional statement field code - vba

I am searching for the correct syntax for my macro to find an IF Conditional statement field code.
I need to search for {IF{DOCVARIABLE"CODE"} = "YES" "A" "B"} to remove this and replace with "B" (the false statement).
When searching for a word field like {DOCVARIABLE "CODE"} alone, I write as: Text1 = "CODE" but when there is an IF & Text & Yes & TRUE, FALSE like above, how do I write?

Consolidated comment (OP crossposted elsewhere):
For that specific sequence, you can search for
^19IF^19DOCVARIABLE"CODE"
Word will select the entire IF field (so you will then need to parse the entire IF field anyway), but it should not select e.g.
{IF{DOCVARIABLE"BOX"}
If there is some variation (e.g. additional whitespace in the string) you may be able to use something like
^19^wIF^w^19^wDOCVARIABLE^w"CODE"

Related

Regular expression not working in MS Word

I have markups enclosing text in a word document.
If the markups enclose valid text or any valid dynamic field, the regular expression works :
But the second line, which contains an invalid cross-reference is not found by the search engine... whereas I need to find and delete it actually.
Edit on the 25/01/2018 :
Thanks for your answer.
I run the search from a macro, that is right. I have no error from the macro, the sequence [SP]invalid reference[\SP] is just not found
I actually want to select anything between the two markups (table, image, text, reference, fields ...) including the markups
It is impossible to find fields via wildcard search. I also cannot find an error field by searching for ^d without wildcard.
Therefore: use two loops in your code. Before searching for [SP], iterate over all fields, identify those with error and delete / Replace with a space:
Dim fld As Field, ran As Range
For Each fld In ActiveDocument.Fields
If InStr(1, fld.Result.text, "Erreur !") >= 0 Then
Set ran = fld.Result
fld.Delete
ran.InsertAfter " "
End If
Next fld
After that, run your normal wildcard search/replace for [SP]...[/SP] and you should be fine.

In Excel VBA, how do I refer to a named column that contains a single quote

I am investigating a bug in an Excel spreadsheet where the following formula is inserted into every cell in a column.
=AGGREGATE( 3, 5, InputData[#[Foo]:[Bar]]) > 0
The VBA is as follows:
Let AddColumn.DataBodyRange.formula = "=AGGREGATE( 3, 5, [#[Foo]:[Bar]]) > 0"
this will evaluate to FALSE if all of the cells on the current row between columns Foo and Bar are empty, otherwise it evaluates to TRUE
The problem I'm seeing is that the names Foo and Bar are variable and not under my control and the formula fails with Run-time error 1004 if a name contains a single quote:
Let AddColumn.DataBodyRange.formula = "=AGGREGATE( 3, 5, [#[Foo's name]:[Bar]]) > 0"
Is there a way I can escape the name in such a way that single quotes won't create the run-time error? Adding double quotes around the name gives me the same error.
Are there likely to be further problems if the names contain other characters that have special meaning in Excel?
I could also refer to the columns by address instead of name. Would that work with the current row '#' notation?
Excel version:14.0.7188.5002
I hear you when you say the naming convention is "not under my control". This really puts you in a bind when anything can be pumped into your code.
Sadly, the only solution is to scrub the input when they finally hand it over to you. This involves you having to make your own vba function that takes in a string and returns a string that has special characters removed (or replaced with something else).
In your case, you are going to have to scrub the data in possibly two places.
First, you will need to change all the column names so they don't have special characters in them. You'll need to access each name and send it through the 'scrub' function and then replace the name with the scrubbed name.
Second, when someone inputs a column name for your AGGREGATE, you'll need to capture that input into a string variable and then pass that through the 'scrub' function. Then you'll need to validate that the input they gave you matches up with a valid column name. If it's not valid, send them an error message asking them to enter a valid name or to cancel out.
After you have valid values for foo and bar, you can add them to your AGGREGATE function and let it execute.
If you can't scrub/change your column names, then you'll have to make a list of scrubbed column names and associate them with the column address. Then when you get your input, scrub it, and then match to the list to grab the correct address. Then you can use hard addresses instead of variable naming schemes.
It's a lot of work. But it's necessary when you have naming conventions that are not under your control.
The other answers and comments put me on the right track:
Function escapedColumnName(columnName As String) As String
columnName = Replace(columnName, "'", "''")
columnName = Replace(columnName, "#", "'#")
columnName = Replace(columnName, "[", "'[")
columnName = Replace(columnName, "]", "']")
escapedColumnName = columnName
End Function

Excel: Highlighting duplicates with exact matches

I've got two columns of data, and I would like to use conditional highlighting to find the EXACT matches in both columns. However, using a formula like this one turns up a lot of incorrect matches:
=MATCH(A1,B:B,0)>0
There are a lot of partial matches, so cells are highlighted that I don't want. What I want is the equivalent of the "Match Entire Cell Contents" checkbox in the Find & Replace dialog.
To make this clear, it seems Excel will do partial matches - here is the Find & Replace dialog without "Match Entire Cell Contents" checked, with a sample search for all matches:
Search for all matches, any match
Vs. the dialog with the checkbox ticked and the results:
Search for all matches, match entire cell
But this only applies to find & replace. How can I update the formula above to work the same way, so that only exact matches to what's in a cell are highlighted?
=NOT(ISERROR(MATCH(A1,B:B,0)))
Regular use of MATCH() - without wildcards - doesn't do partial matches, and MATCH returns an error if there's no hit, not zero
This isnt quite what is being asked, but this was helpful to me:
To find duplicate records, use Excel's easy-to-use Filter feature as follows:
Select any cell inside the recordset.
From the Data menu, choose Filter and then select Advanced Filter to open the Advanced Filter dialog box.
Select Copy To Another Location in the Action section.
Enter a copy range in the Copy To control.
Check Unique Records Only and click OK.
There's more about conditional formatting on this site:
https://www.techrepublic.com/blog/windows-and-office/how-to-find-duplicates-in-excel-245163/

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.

SSRS expression and free text in the same textbox

Is it possible?
I would like to place the following inside a textbox:
There are =Count("MyDataSet") records found.
But if I place that in the expression on the textbox it just displays as above instead of getting the value.
Now I know if I were to split the above in two different textbox works ok but for spacing reasons it would be better if I could just use one?
Is that possible?
You need an expression in the textbox that specifies the text strings and concatenates these to your count value, something like:
="There are " & CountRows("MyDataSet") & " records found"