I want to use an If statement (VBA code) to check the cell range in a column for a given numeric parameter. For the cell that matches the given value, the cells at the right (in the same row) should change the background color.
Pseudocode Example:
A1=5,7
If cell in Range(F1:F10) has value=A1 Then
(random matched cell: F7=5,7)
Range (G7:M7) = Background Blue
The part to change the background I know how to do it, but what is the best way to check the given range?
I think you want something like
for i = 1 to 10 'rows in column f to loop through
if cells(i,6) = cells(1,1) then 'column a is 1, column f is 6, etc.
range(cells(i,7), cells(i,13)).interior.colorindex = 'number for that color
end if
next i
I'm guessing you may have multiple rows in F1:F10 that have a match on A1. I would iterate through the cells in the range with:
For each rngCell in Range("F1:F10")
If rngCell.value = Range("A1").value
Range("G" & rngCell.row, "M" & rngCell.row).Interior.ColorIndex = 5
End If
Next
Related
I am trying to figure out how to autofill a specific range of cells based on already defined data in the same row. My task is more complex, but a simplification of the step can be seen in the code below. What I want to achieve is:
Define a range where I want to output my values
Multiply two values in the same row of the selected range cell (to its left), and output this number in the currently selected range cell. To do this, one of the numbers to be multiplied will be dependant on a string also in the row (which describes its type).
Loop through the defined range and repeat the calculation on each row.
My current code is as follows and outputs a "application defined or object defined error".
Any help would be much appreciated.
For a = Range("P12") To Range("P33") 'Range of cells I want to fill.
If Cells(a, -10).Value = "B" 'If the cell 10 to the left of our "a" value is = "B".
Then c = Cells(a, -10).Value * Worksheets("LCI").Range("D4").Value 'Then new variable "c" = (cell 9 to left of a) * (number from another sheet containing a database)
Cells(a).Value = c 'Update value of selected range cell to contain c which we calculated.
Next 'Repeat for all rows in range.
You are close.
You need to think of a as a cell or Range object. That variable is the cell itself which has properties like a.row and a.column that describe its location on the sheet in which it lives.
When you say Cells(a, -10) you are saying "On the Activesheet I want a Cell where the row is a, and where the column has the number -10". There is no row a (as a is a range/cell object and because you didn't specify which property of a you are wanting here it will default to a.value which is probably nothing) and there is no column -10 on the Activesheet.
Your loop is defined incorrectly. You can use a For Each loop to loop through cells in a range.
Instead:
For Each a In Range("P12:P33")
'clear `c` each loop
c=0
If a.Offset(,-10).Value = "B" Then 'start at cell `a` and move back 10 columns and grab the value
c = a.Offset(, -10).Value * Worksheets("LCI").Range("D4").Value
End If
a.Value = c
Next a
I'm trying to have a program that can read a range of cells which consist of 12 cells (let's say: P79, R79, T79, V79, X79, Z79, AB79, AD79, AF79, AH79, AJ79, AL79) and under those cells there are 6 cells (let's say: V81, X81, Z81, AB81, AD81, AF81), the program is looking for whether or not there are values typed in the cells within the described range.
The program should be able to read the cells from left to right on the top row and loop down to the bottom row and read that from right to left.
If all the cells in the top row have values in them, then the program breaks and doesn't read the values in the bottom row.
As the program reads the values from each cell it should create a table consisting of three columns (let's say: M88, N88, O88), the leftmost column should have the cell number (in order of cell as read by the program (whichever cell has a value first in the loop is given the number 1 and then the next cell to have a value is given number 2 etc.). The middle column should have whatever value is written in it's corresponding cell read from the range. The right column should have the value of whatever is to the right of each cell containing a value.
The first value to be read with a value should give the value "Left End" and the last value to read (whether or not it is the 12th cell to have a value in the top row or the leftmost cell to have a value in the bottom row) should give the value "Right end".
An example of what a row from the table could look like:
Cell # Cell Value Position/Left/Right
1 First Left End
This is the code I have so far:
Sub Code()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
Dim i As Integer, j As Integer, k As Integer
' First loop to compare a car to the rest after it
For i = 1 To 12
For j = i + 1 To 12
If Not IsEmpty(ws.Range("Cell_" & i)) And Not IsEmpty(ws.Range("Cell_" & j)) Then
ws.Range("B82").Offset(i).Value = j
Exit For
End If
Next j
Next i
' Loop backwards to find "Right End"
For k = 12 To 2 Step -1 '24 To 2
If Not IsEmpty(ws.Range("Cell_12")) Then
ws.Range("B82").Offset(12).Value = "Right End"
Exit For
' Has the "Right End" Follow when cars are left blank for lower row
ElseIf IsEmpty(ws.Range("Cell_" & k)) And Not IsEmpty(ws.Range("Cell_" & k - 1)) Then
ws.Range("B82").Offset(k - 1).Value = "Right End"
Exit For
End If
Next k
What I have here merely inserts a count into a cell range, what I'm trying to do is have my code actually read the cells in the range in the order I described and one at a time look at which cells have values written in them and look at which cells (with values in them) are to the right of any cell with a value and produce the table described above.
After reading your explanation, which was quite challenging I tried to recreate what you are asking.
I used cells A1:L1 with numbers 1 to 12. in the row below that A2:L2, some numbers have been added. with an if value <> "" you can see which cells contain a value.
In the second worksheet the table is made:
Sub test()
Dim a As Integer
Dim i As Integer
Dim name As String
ActiveWorkbook.Sheets(1).Activate
a = 1
For i = 1 To endcel
If Sheets(1).Range("a1").Offset(a, i - 1).Value <> "" Then
name = Sheets(1).Range("A1").Offset(a, i - 1).Value
Sheets(2).Activate
Sheets(2).Range("b2").Offset(i).Value = name
End If
Next i
End Sub
Does this help? You can adapt it a bit to your problem.
Good luck!
I have some VBA code that will delete the entire row if a cell in a column has red text
Dim Cell As Range
For Each Cell In Intersect(Columns("L"), ActiveSheet.UsedRange)
If Cell.DisplayFormat.Font.ColorIndex = 3 Then Cell.Value = "#N/A"
Next
On Error GoTo NoRedText
Columns("L").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoRedText:
I would like to extend this code to also include another column that includes a "Y" value in the cell.
Column L includes the red text string
Column P includes the "Y" text string
Therefore if the text is red in column L AND the text is equal to "Y" in column P it should delete the entire row
What do I need to add to the code to achieve this?
Thank you
Your Cell variable references a range and all the properties that go with it.
The Offset property
Returns a Range object that represents a range that's offset from the
specified range.
(https://msdn.microsoft.com/en-us/library/office/ff840060.aspx)
Using this knowledge you can tell your code to look at the range that is offset by four columns:
If Cell.DisplayFormat.Font.ColorIndex = 3 AND Cell.Offset(,4)="Y" Then Cell.Value = "#N/A"
So, I have a script that is plotting data points for me, and I'm running into an issue where there are several blank spots in the data. Of of my columns is calculated, and I have a formula that sets it equal to "" if more than 0 cells that are used in the calculation are blank. The plots that use the blank cells work fine to show gaps in the data, but Excel doesn't evaluate a cell that has a formula that results in "" as blank.
Therefore, I need to set up some code that can search the entire column of data and clear the formula out of the cells whose value equal "", thereby making them blank and displaying as gaps in the plot.
I know I can use the Find and What commands to find the first cell that is evaluated as "", but how can I use it to find all the cells in the column?
The row range for the data is always constant, between 4 and 243, and the column number I am searching (within a loop) evaluates as 3*(iCounter - 1) + 2, if that helps anyone.
(The range I am searching is equal to Range(Cells(4, 3*(iCounter - 1) + 2), Cells(243, 3*(iCounter - 1) + 2))
Click on any cell in the column you wish to cleanup and run this:
Sub ClearThem()
Dim BigR As Range, r As Range
Set BigR = Intersect(ActiveCell.EntireColumn, ActiveSheet.UsedRange)
For Each r In BigR
If r.Value = "" Then r.Clear
Next r
End Sub
Hi I'm fairly new to formulas and excel but this is one of the problems I have encountered.
I have been using 2 conditional formatting formulas as follows
=INDIRECT("I"&ROW())="Del"
=INDIRECT("I"&ROW())="Sum"
Where the first formula simply highlights the row grey if del is in the I column of that row, is there also a way of making it change say the K column to 0 if column H in that row is 0?
And for the second formula which also highlights the row another color based on sum input in the given column of that row, is it also possible to change K column of that row to match the value of H column of the given row.
I know they would be similar but I needed to make it so formula one would only zero the K column in the given row if I column had "del" and H column of the row had Zero.
And for the second formula the values would only change in column k of the given row if "sum" was in the I column. Anything else needs to stay unformatted unless these changes are implemented.
I am unable to add a formula the the cells in question as these are overwritten with an button clicked event which inputs data into this field.
any information is appreciated, formula or VBA.
a) use =$I2="Del" instead of INDIRECT (where 2 is the first row of the range your conditional format applies to, e.g. =$A$2:$Z$9999, or the row of the firstly selected cell of the range when you are inserting the conditional format)
b) if you can use a new column that won't be overwritten, the formula in this new column can be:
=if(and(I2="Del";H2=0);0;if(I2="Sum";H2;K2))
P.S.: use , instead of ; if your Windows > Control Panel > Region and Language > Additional settings... > List separator is set to a comma
Just adjust the Offset accordingly to change the column(s) you want.
Dim firstCell As Range
Dim FoundCell As Range
Dim lastrow As Long
With ActiveSheet
lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
With Range("H2:H" & lastrow)
Set FoundCell = .Find(What:="3")
Set firstCell = FoundCell
Do Until FoundCell Is Nothing
'.offset(0,-1) would be the same row in Column "G"
FoundCell.Offset(0, -1).Value = 0
'if you wanted to assign the same value then do this:
' FoundCell.Offset(0, -1).Value = FoundCell.Value
Set FoundCell = .FindNext(FoundCell)
If FoundCell.Address = firstCell.Address Then
Exit Do
End If
Loop
End With
End With