VBA - conditional formatting with text - vba

I was hoping that there is a way to conditionally format the column G like columns B & C. Possibly as binary, where the text = 1 and blank = 0? Thanks in advance.

Not exactly know the logic but here is a way to accomplish this with tweaking Custom Number Format. Here is the example and how I do it:
Determine your logic. See what I have in my example. I simply used 0 and 1 because Conditional Formatting works better with numbers.
Right click on the cell on column G, make a rule like [=0]"FAIL";[=1]"OKAY". Values inside [ ] is the condition, and the output string is inside the double quotes " ".
As for the Conditional Formatting, I believe you know what to do. Please let me know if this helps and good luck.

Related

Swapping the placement of a character in a cell

I've been looking for a way to change the placement of a character within a cell with a macro, and have not been able to find exactly what I'm looking for. I've found ways to switch words (such as first name last name), but I need to switch a single character that is attached to the end of a number.
The report that's being filtered in registers negative numbers as:
2.00-
and I'm looking for an easy way to switch it to:
-2.00
as I am dealing with a massive number of records. Is there an easy macro that I can use to do this, or do I need to manually change these? Thanks in advance!
Select column then Text-to-Columns, Fixed width, Next, Advanced, Trailing minus for negative numbers, Ok, Finish.
With data in A1, in B1 enter:
=IF(RIGHT(A1,1)="-",-MID(A1,1,LEN(A1)-1),A1)
Create a new column and insert this formula. Replace A3 with the first cell in question, and then drag it down:
=IF(RIGHT(A3,1) = "-", LEFT(A3, LEN(A3)-1) * -1, A3)

How to Translaste this =SUM(COUNTIFS(F4:AN4,{"0","1"})) into VBA?

I need help translating a common Excel function, into VBA code.
Please see attached screenshot for the code I already have started.
I am using the calculations seen in the screenshot to build a scorecard/grading worksheet. I will need to adjust the rows in each of these, but never the columns.
Once I figure this out, I will then loop these to repeat for each new row as they are added.
image of my code, so far
When a literal string needs to contain double-quote characters, you need to use two double-quotes in a row for each double-quote you need in the string.
So your string
Range("AP4").Formula = "SUM(COUNTIFS(J3:AR3,{"0","1"}))"
needs to look like this:
Range("AP4").Formula = "SUM(COUNTIFS(J3:AR3,{"“0"”,""1""}))"
You can also do this without putting the formula into the content of the cell like this:
Range("AP4") = WorksheetFunction.SUM(WorksheetFunction.COUNTIFS(J3:AR3,{""0"",""1""}))

Custom number formatting to display 0000.00

I have a database with all the U.S. tariff codes that I need to format.
The numbers appear as:
010129
Whereas I need them to appear as:
0101.29
I have tried to use custom formatting to match the decimals with ####.?? but it does not work.
The database contains 19,423 rows of data; therefore, if there is a better way to format the numbers using VBA, I will gladly take that response as well. Thank you!
Because you have Text rather than Numbers, reformatting is not enough. With your data in A1, in B1 enter:
=LEFT(A1,5) & "." & MID(A1,6,3)
and copy down. For example:
EDIT#1
If the double-quotes do not appear in the data then use:
=LEFT(A1,4) & "." & MID(A1,5,2)
See:

How do I add a subscript to a string?

I need to add a subscript (a little number next to a character, like 2 or 3 - note: not a power of...) to a string [variable]. Is this possible? I don't want the code to be lengthy as I will need to process a lot of formulas, one at a time. Thanks.
You will have to use the character set subscript numbers. Strings do not contain formatting.
http://www.fileformat.info/info/unicode/char/2082/index.htm
Is this possible? No - string variables do not contain formatting.
In order to achieve this you will need to add some sort of formatting to your string and display it in something that can show different formats - for example a RichTextBox control
Try adding a RichTextBox control and running the following line:
RichTextBox1.Rtf = "{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Segoe UI;}}\viewkind4\uc1\pard\ltrpar\sa200\sl276\slmult1\f0\fs22 sometext\fs12 subscript\fs22\par\pard\ltrpar\f1\fs17\par}"
I don't claim to know what all the formatting is in here so I will leave you to figure that out yourself - hope that helps...

How to fit VERY long condition in conditional formatting

I have a very long condition, about 3,000 characters. Access has only space for about one tenth of that.
Is there some other way to set conditional formatting on a textbox besides through the dialogue, or can I do it in VBA and, if so, HOW?
I have conditional formatting on a bunch of textboxes that trigger when the report is opened.
Would be curious to know if the VBA approach worked?
Another idea: create a hidden field on the form and set it's value based on your 3000-character condition. If you are just trying to have 1 conditional expression, you could give this hidden field a false/true or 0/1 value; if you want mulitple conditions, you could give it a value of 0, 1, 2, or 3 corresponding to the conditions you want applied. In either case, your conditional expression test(s) is(are) now trivial: [HiddenFieldName]=ConditionValue
According to Access' Help topic, the FormatConditions Collection has methods (Add, Delete, Modify) which should allow you to adjust your FormatConditions with VBA code. I've never tried, so offer no opinion as to whether this would be a practical approach for you.
I also tried to find out if there is a capacity limit for the number of characters FormatConditions can accept. I didn't find anything there.
Yes, you can manipulate Format Conditions in VBA. There's a full detailed article knowledgebase article at http://support.microsoft.com/kb/304104.
Code snippet here showing a basic example. Refer to the above link to get the VBA for AddFormats:
Public Function HighLightForeignKeys(argFieldName As String, argFieldValue As Integer)
Dim FormatCondition As String
Dim CodeReception As Integer
FormatCondition = "[" & argFieldName & "] = " & ArgFieldValue
With Me.ID
.FormatConditions.Delete
.FormatConditions.Add acExpression, , FormatCondition
.FormatConditions(0).BackColor = 16510422
AddFormats Me.ID, Me
End With
End Function
I dont know if this is what you were after, since you mentioned there was only a tiny box to enter all of your conditional text. Others have shown you VBA solutions - since you asked if that was one way to achieve it.
I have always resorted to using the "zoom" feature which is accessible via the SHIFT-F2 keystroke. Is that what you were after? I think it goes back several Access versions too.
A good set of Access shortcut keystrokes is here:
http://www.joyedaniels.com/keys_access.htm