I have two columns, one contains addresses of other cells ($F$2 for example), the other contains colour index numbers (15, for example).
I want to colour the referred-to cell (which is on another sheet).
Sub Colour_World()
Dim Cell As Range
For Each Cell In Range("p2:p3907") 'column with cell values
Worksheets("World").Range(Range(Cell).Value).Interior.ColorIndex = Cell.Offset(0, 5).Value
End Sub
Because variable Cell in For Each will return string in range("p2:p3907") and when we refer to the range just use only Range("string refer to cell"). So, remove one range from your code and it will be as you need. See code below.
Sub Colour_World()
Dim Cell As Range
For Each Cell In Range("p2:p3907") 'column with cell values
Worksheets("World").Range(Cell).Interior.ColorIndex = Cell.Offset(0, 5).Value
Next
End Sub
Sub Colour_World()
Dim Cell As Range
For Each Cell In Range("p2:p10") 'column with cell values
Worksheets("World").Range(Cell.Value).Interior.ColorIndex = Cell.Offset(0, 5).Value
Next Cell
End Sub
Sub mySub()
Dim colorCodeRange As Range 'Cell is a keyword so you don't want to try to use it as a variable name.
Dim outputCell As String
Dim colorCode As Byte
Set colorCodeRange = Sheets(1).Range("P2:P10")
For Each Cell In colorCodeRange.Cells 'either define the range and use it here or don't define the range object and type in a hard-coded range here.
'you were defining the range then not using it, which is pointless.
outputCell = Cell.Value2 'get the output cell from the color code sheet
colorCode = Cell.Offset(0, 5).Value 'get the color code
Sheets("World").Range(outputCell).Interior.ColorIndex = colorCode 'output the color to the "World" sheet
Next Cell 'be sure to end the loop with "Next Cell" or at least "Next"
End Sub
This Sub will get output cells (F2, F3, etc.) from the range P2:P10. These values are in sheet 1. The color codes are in the same row, 5 columns over to the right (column U). The fill colors are output to the "World" sheet in the cell specified in the colorCodeRange variable ("P2:P10").
Related
I have a long list of equipment like this;
I would like to be able to run a VBA script that allows excel to change the format of the first of a series so that they are more visible. Is this a possibility?
This is housed in an excel table, not sure if that has an impact.
You could iterate through the range update format if below cell<>previous cell.
Sub UpdateCatHead(ByRef rng As Range, Optional col_index As Integer = 1)
Dim rng_search As Range
'lets ,make sure to have range of one column
Set rng_search = rng.Columns(col_index)
Dim cell As Range, prev_cell As Range
Dim prev_cat As String
For Each cell In rng_search.Cells
'check if empty is empty and exit for?
If cell.Row = 1 Then
'update cell to bold here
Debug.Print (cell.Row)
Else
Set prev_cell = cell.Worksheet.Cells(cell.Row - 1, cell.Column) 'cell above
If CStr(cell.Value) <> CStr(prev_cell.Value) Then
'update cell to bold here
Debug.Print (cell.Row)
End If
End If
Next cell
End Sub
call Sub like this:
UpdateCatHead ThisWorkbook.Sheets("data").Range("A1:A100")
I am trying to compare values from a vlookup to a static value in the column to the left, and then paste the value if it matches.
Column J contain static values
Column K contains a vlookup formula referencing another sheet
I need to loop through column K, and if the value from the vlookup matches the value in the same row in column J, then paste the value into column K.
So if cell J2 = 240.89, and the value of the vlookup in cell K2 = 240.89, then paste the value 240.89 into K2.
So far, the code I have loops through the range but stops at the last pasted value and ignores the vlookup formulas.
Here is the code I have:
Option Explicit
Sub CheckValue()
Dim myRange As Range
Dim cell As Range
Set myRange = Range("K2:K3194")
For Each cell In myRange
If cell.Value = cell.Offset(0, -1) Then
cell.Copy
cell.PasteSpecial xlPasteValues
End If
Next cell
End Sub
Any help is appreciated!
Just for fun another option:
Sub Test()
Dim ws as Worksheet: Set ws = Thisworkbook.Worksheets("Sheet1")
ws.Range("K2:K3194").Value = ws.Evaluate("IF(K2:K3194=J2:J3194,J2:J3194,FORMULATEXT(K2:K3194))")
End Sub
Note: This requires FORMULATEXT, a function available since Excel2013.
Try
For Each cell In myRange
If cell.Value2 = cell.Offset(0, -1).Value2 Then
cell.value = cell.value
End If
Next cell
or maybe use a WorksheetFunction.Round to match the values to a specific precision. I feel like this is more the issue than anything.
I am trying to highlight a cell if it doesn't equal the value defined in a worksheet range.
I am checking each cell in the range "ADS_Export[ADS_208_SZ]" against Worksheets(ADS_Validator").Range("E3:E500") but it doesn't like the range E3:E500.
Seems to work if I just put E3 but all after the first cell check are incorrect as its checking against the third row, not the next row in the worksheet range (E4 etc.).
It needs to check the first value in the ADS_Export range against the first value in the worksheet range which starts at E3 and then does the next one. Also, need to redefine to highlight if not equal instead of equal.
Only just getting into VBA so my knowledge is very limited.
Sub IF_Loop()
Dim cell As Range
For Each cell In Range("ADS_Export[ADS_208_SZ]")
If cell.Value = Worksheets("ADS_Validator").Range("E:E").Value Then
cell.Interior.Color = 65535
End If
Next cell
End Sub
Try this code, please:
Sub IF_Loop()
Dim cell As Range, celVal As Range, lastRow As Long
lastRow = Worksheets("ADS_Validator").Range("E" & Rows.Count).End(XlUp).row
For Each cell In Range("ADS_Export[ADS_208_SZ]")
For Each cellval In Worksheets("ADS_Validator").Range("E3:E" & lastRow)
If cel.value = celVal.value Then
cell.Interior.Color = 65535
End If
Next
Next cell
End Sub
In column A there are 24 titles. In column B there is similar data.
I want a "Private Sub CommandButton3_Click()" to clear contents from cell B1 in which text does not equal cell A1. Same for all lines down the table.
You can use for each loop to do the job.
Private Sub CommandButton3_Click()
Dim range As range
Set range = Worksheets("Sheet1").range("B1:B" & Cells(Rows.Count, 1).End(xlUp).Row)
For Each cell In range
If cell.Value = cell.Offset(0, -1) Then
cell.ClearContents
End If
Next cell
End Sub
I have various empty cells in a table that I want to fill with the last known value in that column.
Sub autofiller()
Dim DataRange As Range
Set DataRange = Range("A1:D4")
Dim i As Integer
FillA = ""
For Each cell In DataRange
If cell(1, 1).Value <> "" Then
FillA = cell(1, 1).Value
Else
cell(1, 1).Value = FillA
End If
i = i + 1
Range("C1").Value = i
Next
End Sub
There will be no empty values in the first row. My logic is that it will look through every cell in that range, and if it is not an empty cell, pick up the value until it hits an empty cell where then I will place that value. The i counter is for me to keep track. It seems to me that the code is checking cell by cell horizontally but I want it to check cell by cell vertically. How can I make it check vertically?
I am new to VBA so any additional comments/guides will help.
Try this:
Sub autofiller()
Dim cell as range
Dim DataRange As Range
Set DataRange = Range("A1:D4")
For Each cell In DataRange
If cell.Value = "" Then
cell.Value = cell.Offset(-1).Value
End If
Next
End Sub
Because the for each will go left to right, top to bottom it will fill it with the cell above as it encounters empty cells.
You are confusing your variable cell which is a range variable with Cells() which is a range object. cell(1,1) is equal to cell as it is getting the first cell in a range of one.
Another way to write cell.Offset(-1) would be cell(-1,1) this gets the cell directly above the cell.