Vba Numberformat muliple condtions to format cell value - vba

Dear all i want to format my cell based on the Cell value. There are 3 possible Conditions:
Cell is larger than 0.05 -> the Value should stay the same but be fromated to 0.0
Cell is smaller than 0.05 -> the Value should be replaced with a String "a.C."
Cell is Zero -> the Value should be replaced with a Dash"
I found thisSolution for the dashes and could combine it with the number formating
.NumberFormat = "0.0;[=0]---"
This works. but if i add an additonal argument the vba code breaks.
.NumberFormat = "0.0;[=0]---;[<0.05]a.C."
Does not work. Would you mind telling me what i am doing wrong?
Thank you in advance

Edit:
FOr what ever reason this Order seems to work:
.NumberFormat = "[=0]---;[<0.05] ""a.C."";0.0 "

Related

Vba turns cell in to date on mistake

Hi how can I get around vba is converting a cell automatic to a data format when I add a value like 10 - 12 I just want the value in the cell to be 10 - 12 not a date
the code I use is "cFli is the value 10 - 12":
tsheet.Range("E2").Value = cFli
You can prepend a single-quotation mark to the value of a cell, which indicates to Excel to treat the rest of the string as text:
tsheet.Range("E2").Value = "'" & cFli
Note: The ' does not become part of the cell contents, although it will appear when editing the cell in future.
Alternatively, you can achieve the same thing by formatting the cell using the Text format:
tsheet.Range("E2").NumberFormat = "#"
tsheet.Range("E2").Value = cFli
Note: The formatting must be done before putting the value into the cell.

Set up a an IF formula through VBA but if cell is blank return an empty cell

Hello Stackoverflow Community,
Thanks in advance for any help you can give.
I am currently trying to set an IF formula through VBA that refers to the cell above. If the cell is equal to 0 then it would return a blank value other wise it would return the same value as above.
I am using Belgium setting hence if formula has a comma and not a semicolon
Below is the code. note that the first line help me refer to the cell above.
v = i - 1
Cells(i, "F").Formula = "=if(F" & i - 1"=0,"""",F" & i - 1")"
Thanks for your help!

how to remove parentheses for negative number in excel

I am trying to remove parentheses for negative numbers in excel, but I have so far been unable to remove them successfully
I tried below code:
sheet1.UsedRange.Select
sheet1.Range("A:XFD").NumberFormat = "0.00"
objExcel.ActiveWorkbook.PrecisionAsDisplayed = True
but it only works after doing TextToColumns manually. I need a more general solution, as I don't always know which rows have numbers.
That has to do with the format of the cell. Right-Click cell > Format > (Select Number or Currency) change how you want the negative number to look. ... or if you want to do it in VBA, this will change the negative values to red:
Range("A1").NumberFormat = "$#,##0.00;[Red]$#,##0.00"
This will change it back to ():
Range("A1").Style = "Currency"
or this:
Range("A1").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"

Highlight cells + adjacent 2 cells if certain text if located

If any cell contains the text "example", this cell + the two cells on the same row to the right of, to be highlighted.
So for instance
B5 contains "example", b5,c5,d5 need to be highlighted orange
b9 contains "example", b9,c9,d9 need to be highlighted orange.
And so forth across the whole sheet. Multiple rows and multiple columns could contain the specific text.
Any assistance, examples appreciated.
Private Sub CommandButton1_Click()
row_number = 4
Do
DoEvents
row_number = row_number + 1
swing_data = Sheet1.Range("B" & row_number)
If InStr(swing_data, "Test") >= 1 Then
With Range("B" & row_number).Offset(, 2).Interior
.Pattern = x1solid
.PatternColorIndex = x1automatic
.Color = 65535
.PatternTintAndShade = 0
End With
End If
Loop Until swing_data = ""
End Sub
This is not highlighting the 2 cells to the right, and if there is a blank cell it's stopping. Also it's only working on one column. Needs to work on columns B, E, N, Q, Z, AC,
Changed this line
<<code>With Range("B" & row_number).Offset(, 2).Interiorcode>
To read <code>With Range("B" & row_number).resize(, 3).Interiorcode>
And it works.
Would the be an easier way to include multiple columns in this...?
Conditional formatting:
As multiple columns may contain your criteria you need a bit more complex formula (conditional formatting - use a formula...): =or(iferror(rc[-1]="example",false),iferror(rc[-2]="example",false),rc="example") - notes: I switch to R1C1 reference style before entering conditional formatting as I find it mite clear there; iferror is necessary to make formula working also in first and second columns.
VBA:
you can use VBA find object to cycle through all instances and change formatting there (I think built-in help and maybe recording some short macros give you enough information to create it)
You can do this with conditional formatting if you want to avoid VBA. Select the cells you want you want to apply the formatting to (say B1:D10), Click conditional formatting -> New Rule... -> Use a formula to determine which cells to format. Use this formula
=EXACT($B1,"example")
The $ in front of the column makes sure only that column is looked at, the row will be independent. You then need to change the formatting to whatever you want. In your case change the fill to orange.

VBA Count cells in column containing specified value

I need to write a macro that searches a specified column and counts all the cells that contain a specified string, such as "19/12/11" or "Green" then associate this number with a variable,
Does anyone have any ideas?
Do you mean you want to use a formula in VBA? Something like:
Dim iVal As Integer
iVal = Application.WorksheetFunction.COUNTIF(Range("A1:A10"),"Green")
should work.
This isn't exactly what you are looking for but here is how I've approached this problem in the past;
You can enter a formula like;
=COUNTIF(A1:A10,"Green")
...into a cell. This will count the Number of cells between A1 and A10 that contain the text "Green". You can then select this cell value in a VBA Macro and assign it to a variable as normal.
one way;
var = count("find me", Range("A1:A100"))
function count(find as string, lookin as range) As Long
dim cell As Range
for each cell in lookin
if (cell.Value = find) then count = count + 1 '//case sens
next
end function
If you're looking to match non-blank values or empty cells and having difficulty with wildcard character, I found the solution below from here.
Dim n as Integer
n = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
Not what you asked but may be useful nevertheless.
Of course you can do the same thing with matrix formulas.
Just read the result of the cell that contains:
Cell A1="Text to search"
Cells A2:C20=Range to search for
=COUNT(SEARCH(A1;A2:C20;1))
Remember that entering matrix formulas needs CTRL+SHIFT+ENTER, not just ENTER.
After, it should look like :
{=COUNT(SEARCH(A1;A2:C20;1))}