Double if statement then highlight if value <> to % of - vba

What im trying to do is: If a cell on Column D says cars then check the last cell on the right (last cell on the row) and if the value is less than 20% then highlight. (which is the last part of the code):
This is what i have so far but i cant figure out how to solve it. I have to do this for work but i'm confused thanks!
Worksheets("Report").Activate
Call VBA
Dim lrow As Integer
Dim xrow As Integer
Dim FR As Range
Dim MR As Range
Dim cell As Range
lrow = wsr.Cells(Rows.Count, 3).End(xlUp).Row
xrow = wsr.Cells(Rows.Count, 3).End(xlToRight)
Set FR = Range("D12:D" & lrow)
For Each cell In FR
If cell.Value = "Cars" then for cell.xrow if cell.Value > 0.20 then Then cell.Interior.Color = RGB(224, 202, 224)
By Xrow i mean last cell on the row.
Thanks !

To avoid vba you can use Conditional Formatting.
The conditional formatting formula would then be:
=AND($D12="Cars",INDEX(12:12,MATCH(1E+99,12:12))>0.2)
The INDEX(12:12,MATCH(1E+99,12:12)) part is what finds the last column in that row that has a number. It then test whether it is greater than .2
You can apply it to all rows and columns desired to format starting with row 12, I only applied it to D12:D28:

Try this:
For Each cell In FR
If cell.Value = "Cars" Then
If Cells(cell.Row, Columns.Count).End(xlToLeft).Value < 0.2 Then
cell.Interior.Color = vbRed
End If
End If
Next
However Scott Craner's answer is a much more elegant solution.

Related

Counting the distance between similar values by rows using excel-vba/udf

I am having trouble in counting the distance between values that are similar because there’s no function in excel that could achieve this and I deal with 2000 row of values. I would prefer excel-vba for this, a button perhaps that generates distances like in the example. array formulas lags the excel when there's too many values. Counting them 1 by 1 would be a waste of time. Please I want to have this done. I would truly appreciate it if some genius out there could pull this off.
Example bellow shows how far a specific value from the other:
you could try this
Option Explicit
Sub main()
Dim cell As Range, f As Range
Dim rowOffset As Long
With Worksheets("gaps").Range("A2:F10") '<--| change this to your actual range of interest
For Each cell In .SpecialCells(xlCellTypeConstants, xlNumbers)
rowOffset = 1
Set f = .Find(what:=cell, after:=cell, LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlPrevious)
If Not f Is Nothing And f.Row <= cell.Row Then rowOffset = cell.Row - f.Row + 1
cell.offset(, .Columns.Count + 1) = rowOffset '<--| the "+1" offset results range one column away from values range: adjust it as per your needs
Next cell
End With
End Sub
tested on your "Values" it gives back the same "Value row gaps" except cell "K4": I hope it's a miscount on your part...
should you ever need to display output in the same "relative" position but on another worksheet (say: "sheet2") then just change
cell.offset(, .Columns.Count + 1) = rowOffset
to
Worksheets("sheet2").Range(cell.offset(, .Columns.Count + 1).Address) = rowOffset

VBA Autofill formula with a fixed cell reference

I am trying to autofill a formula to a range using vba.
What i am trying to do is to calculate a percentage contribution for each item.
Say i have 2 entries
A1= 4
A2= 6
A3= sum of A1 and A2 =10
What i want is
B1= A1/$A$3 = 40%
B2= A2/$A$3 = 60%
but i am not quite sure how i could locate the last row of A and code it into the
Range("B1").Formula= "=A1/?????"
any ideas would help!
Thanks
Please try this..
Range("B1").FormulaR1C1 = "=RC[-1]/INDEX(C[-1],MATCH(1E+99,C[-1]))"
You need to mention the range with circular references by specifying Row and column no from the active cell respectively like RC[-1]/R3C1
Find the last cell first, then sum it and add the formula to column B
Sub Button1_Click()
Dim LstRw As Long, rng As Range, Sm As Range
LstRw = Cells(Rows.Count, "A").End(xlUp).Row
Set rng = Range("A1:A" & LstRw)
Set Sm = Cells(LstRw + 1, 1)
Sm = Application.Sum(rng)
rng.Offset(, 1) = "=a1/" & Sm.Address
End Sub

How to write an "If(And" code with unknown number of cells in column?

Is there a way to check if all cells in a column are less than 1? If there were only a few cells, with the number of cells known up front, I would use the code below.
However, from case to case the number of cells in column A will vary. I need to know if any of the cells in column A is less than 1.
If there is one (or more) cell containing a value less than 1, I need a cell (A1 for example) to show NOT OK. If only ALL the cells' values are greater than 1, I need the cell (A1 for example) to show OK.
If all cells in column A have values greater than 1, I want to continue and check column B for the same thing. Otherwise I want to save and close the workbook and continue with next open workbook...also with vba code.
Any suggestions on how to write this in VBA? Maybe there is way other than If(AND...)?
Sub IfAnd()
IF(AND(A5>1,A4>1,A3>1,A2>1),"OK", "NOT OK")
End Sub
This code will solve all your columns and insert the data in THE FIRST ROW OF EACH COLUMN
Sub Problems()
Dim CurCol, LastRow, LastCol as Long
LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
For CurCol = 1 to LastCol
LastRow = Cells(Rows.Count, CurCol).End(xlUp).Row
If WorksheetFunction.Min(Range(Cells(2, CurCol), Cells(LastRow, CurCol))) < 1 Then
Cells(1, CurCol).Value = "NOT OK"
Else
Cells(1, CurCol).Value = "OK"
End If
Next CurCol
End Sub
Here is a way of doing it without any worksheet functions.
Sub test()
Dim ws As Worksheet
Dim ce As Range
Dim sr, lr, lc As Integer
'worksheet you are working with
Set ws = ThisWorkbook.Sheets(1)
'column you are searching
Set ce = ws.Cells(ws.Rows.Count, 1)
'start row set to 2 so row 1 will contain output
Let sr = 2
'search only the last row
Let lr = ce.End(xlUp).Row
Let lc = ws.Cells(sr, ws.Columns.Count).End(xlToLeft).Column
For c = 1 To lc
For r = sr To lr
If ws.Cells(r, c).Value < 1 Then
ws.Cells(1, c).Value = "NOT OK"
GoTo NotOK
End If
Next r
ws.Cells(1, c).Value = "OK"
NotOK:
Set ce = ws.Cells(ws.Rows.Count, c+1)
Let lr = ce.End(xlUp).Row
Next c
End Sub
This should be faster and more efficient for large data sets. Especially if it is sorted smallest to largest.
Here you are:
=IF(MAX(A:A)<1)
If VBA is not required, here is a worksheet formula that should do the job, and will also ignore blanks and non-numeric entries:
This formula must be array-entered:
=IF(ISNUMBER(MATCH(TRUE,IF(ISNUMBER($A:$A),$A:$A)<1,0)),"NOT OK","OK")
If this formula must be located in A1, change the range references from $A:$A to $A$2:$A$1000 where 1000 represents the highest conceivable row number for the data.
To array-enter a formula, after entering
the formula into the cell or formula bar, hold down
< ctrl-shift > while hitting < enter >. If you did this
correctly, Excel will place braces {...} around the formula.

Change a cell's format to boldface if the value is over 500

I am using Excel 2010 and trying to add a bunch of rows placing the sum of columns A and B in column C. If the sum is over 500 I would then like to boldface the number in column C. My code below works works mathematically but will not do the bold formatting. Can someone tell me what I am doing wrong? Thank you.
Public Sub addMyRows()
Dim row As Integer 'creates a variable called 'row'
row = 2 'sets row to 2 b/c first row is a title
Do
Cells(row, 3).Formula = "=A" & row & "+B" & row 'the 3 stands for column C.
If ActiveCell.Value > 500 Then Selection.Font.Bold = True
row = row + 1
'loops until it encounters an empty row
Loop Until Len(Cells(row, 1)) = 0
End Sub
Pure VBA approach:
Public Sub AddMyRows()
Dim LRow As Long
Dim Rng As Range, Cell As Range
LRow = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("C2:C" & LRow)
Rng.Formula = "=A2+B2"
For Each Cell In Rng
Cell.Font.Bold = (Cell.Value > 500)
Next Cell
End Sub
Screenshot:
An alternative is conditional formatting.
Hope this helps.
Note: The formula in the block has been edited to reflect #simoco's comment regarding a re-run of the code. This makes the code safer for the times when you need to re-run it. :)

vba searching through rows and their associated columns and highlight if conditions meet

The code below would search through a row and its associated columns.
For Row 7, if it is a "N" or "TR" and if all entries are blank below line 12,the code would hide the entire column.
However, I still need help with some further help!
If there is a "N" or "TR" in row 7. If there is something writen in any cell, (rather than leaving it alone), can I highlight its associated cell in row 7 in yellow?
If ther eis a "Y" in row 7, If there is any empty cells, can I highlight its associated cell in row 7 in yellow?
Thank you so much! special thanks to KazJaw for my previous post about simular issue
Sub checkandhide()
Dim r As Range
Dim Cell As Range
Set r = Range("A7", Cells(7, Columns.Count).End(xlToLeft))
For Each Cell In r
If Cell.Value = "N" Or Cell.Value = "TR" Then
If Cells(Rows.Count, Cell.Column).End(xlUp).Row < 13 Then
Cell.EntireColumn.Hidden = True
End If
End If
Next
End Sub
attached example of spreadsheet
Here you have an improved version of your code (although I might need further clarifications... read below).
Sub checkandhide()
Dim r as Range, Cell As Range, curRange As Range
Set r = Range("A7", Cells(7, Columns.Count).End(xlToLeft))
For Each Cell In r
Set curRange = Range(Cells(13, Cell.Column), Cells(Rows.Count, Cell.Column)) 'Range from row 13 until last row in the given column
If Cell.Value = "N" Or Cell.Value = "TR" Then
If Application.CountBlank(curRange) = curRange.Cells.Count Then
Cell.EntireColumn.Hidden = True
Else
Cell.Interior.ColorIndex = 6 'http://dmcritchie.mvps.org/excel/colors.htm
End If
ElseIf Cell.Value = "Y" Then
If Application.CountBlank(curRange) > 0 Then
Cell.Interior.ColorIndex = 6 'http://dmcritchie.mvps.org/excel/colors.htm
End If
End If
Next
End Sub
I am not sure if I have understood your instructions properly and thus I will describe here what this code does exactly; please, comment any issue which is not exactly as you want and such that I can update the code accordingly:
It looks for all the cells in range r.
If the given cell (which might be in row 7 or in any other row below it) meets one of the conditions, the corresponding actions would be performed.
Part of the conditions depends on curRange, which is defined as all the rows between row number 13 until the end of the spreadsheet.
Specific conditions:
a) If the value of the current cell is N or TR. If all the cells in curRange are blank, the current column is hidden. If there is, at least, a non-blank cell, the background color of the given cell would be set to yellow.
b) If the value of the current cell is Y and there is, at least, one cell in curRange which is not blank, the background color of the background cell would be set to yellow.