I fairly new in excel so don't know much about it or VBA.
I have this dataset shown below, In which first column has some values in it.
I wanted a conditional formatting or VBA formatting(don't know if this exists also!)
to color the data from the 3rd to 5th column based on the selected cell in the first column.
For eg. if I select dbo.project in the 1st column, all the cells having the dbo.project string
in it should be colored/highlighted.
Given Below is the image of my dataset.
The below code will activate whenever a single cell in column 1 is selected, and will color all cells containing the selection's text in columns C:E to vbGreen color.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Target.Column = 1 Or Not Target.Cells.Count = 1 Then Exit Sub
With ThisWorksheet
Dim ColorColumns As Range, Cell As Range, strMatch As String
Set ColorColumns = Intersect(.Range("C:E"), .UsedRange)
strMatch = "*" & Target.Text & "*"
For Each Cell in ColorColumns
If Cell.Text Like strMatch Then Cell.Interior.Color = vbGreen
Next
End With
End Sub
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 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").
I want to run VBA code when all the cells in a filtered table column contain a value. I want a shape to be visible only when each cell in the column has a value, otherwise I want it to remain hidden.
I use the following code in Excel VBA:
Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("Table1[Verify]").SpecialCells(xlCellTypeVisible)
Dim i As Range
For Each i In rng.Cells
If i.Value <> "" Then
ActiveSheet.Shapes("Oval 6").Visible = True
ElseIf i.Value = "" Then
ActiveSheet.Shapes("Oval 6").Visible = False
End If
Next i
End Sub
The shape is hidden when the last cell in the column is blank, and visible when the last cell has a value. However, if all other cells are blank and the last cell is not, the shape remains visible.
In other words:
I want the shape visible when ALL of the xlCellTypeVisible cells are non-blank, and I want it hidden if ANY of them are blanks.
The reason that the visibility only depends on the last cell is because you don't have an exit in your loop. If it finds a blank cell, it will still look at the next cell and hides or shows the shape based on the next value.
Basically you want to stop looking at the row the moment you hit a blank cell.
This means you need to use the Exit For statement:
ElseIf i.Value = "" Then
ActiveSheet.Shapes("Oval 6").Visible = False
Exit For
End If
The conditional formattiong is applied to the range(B1:B54) which contains numbers, text & blank. Once this is done, I am required to re-colour cells in a column back to default one which are coloured either green or red from conditional formatting.
Can anybody give me small script to either delete the CF for texts & blanks in range(B1:B54).
You could try:
Option Explicit
Sub Delete_CF()
Dim rng As Range, cell As Range
With ThisWorkbook.Worksheets("Sheet1") 'Change if needed
'Set the range to loop
Set rng = .Range("B1:B54")
'Loop the range
For Each cell In rng
With cell
'Check if cell is empty or not numeric
If .Value = "" Or Not IsNumeric(.Value) Then
.FormatConditions.Delete
End If
End With
Next cell
End With
End Sub
I have values on Sheet 1 and I gave the background color using conditional formatting.
I want to copy only the color and paste it to the corresponding cell of sheet 2 without pasting the value.
Example if sheet 1 cell A1 has red color for specific value, transfer the color to sheet 2 A1.
I use two colors, red and white. Red is for higher value and white is for lower value.
Sub copycolor()
Dim intRow As Integer
Dim rngCopy As Range
Dim rngPaste As Range
For intRow = 1 To 20
Set rngCopy = Sheet1.Range("A" & intRow + 0)
Set rngPaste = Sheet2.Range("b" & intRow)
'Test to see if rows 500+ have a value
If rngCopy.Value <> "" Then
'Since it has a value, copy the value and color
rngPaste.Value = rngCopy.Value
rngPaste.Interior.Color = rngCopy.Interior.Color
End If
Next intRow
End Sub
rngPaste.Interior.Color = rngCopy.DisplayFormat.Interior.Color
Seems to work for me. Keep in mind that DisplayFormat is read-only and is not allowed to return value outside of the function it's used in. Also it is only available in Excel 2010 +
I was editing my answer to include the other stuff you mentioned and realized it was getting confusing to explain it all in separate chunks. Here's a recommended approach to achieve what you're saying.
Public Sub CopyColor()
Dim SourceSht As Worksheet
Dim TargetSht As Worksheet
Dim rngCopy As Range
Dim rngPaste As Range
Dim LastCopyRow As Long
Dim LastCopyColumn As Long
'Define what our source sheet and target sheet are
Set SourceSht = ThisWorkbook.Worksheets("Sheet1")
Set TargetSht = ThisWorkbook.Worksheets("Sheet2")
'Find our used space on the source sheet
LastCopyRow = SourceSht.Cells(Rows.Count, "A").End(xlUp).Row
LastCopyColumn = SourceSht.Cells(1, Columns.Count).End(xlToLeft).Column
'Setup our ranges so we can be sure we don't loop through unused space
Set rngCopy = SourceSht.Range("A1:" & SourceSht.Cells(LastCopyRow, LastCopyColumn).Address)
Set rngPaste = TargetSht.Range("A1:" & TargetSht.Cells(LastCopyRow, LastCopyColumn).Address)
'Loop through each row of each column.
' This will go through each cell in column 1, then move on to column 2
For Col = 1 To LastCopyColumn
For cel = 1 To LastCopyRow
' If the string value of our current cell is not empty.
If rngCopy.Cells(cel, Col).Value <> "" Then
'Copy the source cell displayed color and paste it in the target cell
rngPaste.Cells(cel, Col).Interior.Color = rngCopy.Cells(cel, Col).DisplayFormat.Interior.Color
End If
Next cel
Next Col
End Sub
Simplest would be to apply the same conditional formatting to Sheet2, but use the values from Sheet1 as your criteria. So if Sheet1 Cell A1 has the value that makes it red, add formatting to Sheet2 that turns Sheet2 Cell A1 red as well.
There's a good explanation of how to achieve this here.
.Interior.Color gets the actual colour of the cell rather than the conditionally formatted colour (the one you see). So you can't copy/paste this red colour in your example in this way.
I believe that the only way to get the conditionally formatted colour you see would be to recompute whatever formula you've used in your conditionally formatting criteria.
Excel 2007 conditional formatting - how to get cell color?
Edit
While #JeffK627 was giving an elegant solution, I was knocking up some rough vba code to recompute what I gather your conditional formatting does. I've done this over range A1:A20 on sheet 2. At the moment it colours the cell that contains the value itself, but only requires a little tweak to colour the equivalent cell on another sheet.
Sub ColouringIn()
Dim intColIndex As Integer
Dim dblMax As Double
Dim dblMin As Double
Dim rngCell As Range
'RGB(255, 255, 255) = white
'RGB(255, 0, 0) = red
'so need to extrapolate between
dblMax = Application.WorksheetFunction.Max(Sheet2.Range("A1:A20"))
dblMin = Application.WorksheetFunction.Min(Sheet2.Range("A1:A20"))
For Each rngCell In Sheet2.Range("A1:A20")
If IsNumeric(rngCell.Value) And rngCell.Value <> "" Then
intColIndex = (rngCell.Value - dblMin) / (dblMax - dblMin) * 255
rngCell.Interior.Color = RGB(255, intColIndex, intColIndex)
End If
Next rngCell
End Sub
Adding following example as alternative solution, as I needed something dynamic/active where color IS a required condition of data & not reliant on any other trigger.
Option1:
Dim rngPrev2Update As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cellbox As Range
Dim rngDest As Range
If Not rngPrev2Update Is Nothing Then
For Each cellbox In rngPrev2Update.Cells
Worksheets("Sheet2").Range(cellbox.Address).Interior.ColorIndex = cellbox.Interior.ColorIndex
Next cellbox
End If
Set rngPrev2Update = Target
End Sub
This will update destination cells when cursor is next moved to another cell.
Option2:
Private Sub Worksheet_Activate()
Dim cellbox As Range
Dim rngCells As Range
Set rngCells = Range("B1:B10")
For Each cellbox In rngCells.Cells
Range(cellbox.Address).Interior.ColorIndex = Worksheets("Sheet2").Range(cellbox.Address).Interior.ColorIndex
Next cellbox
End Sub
Will update relevant cells on sheet load.
Note: If you have very large data set you may want to put this into a macro button &/or filter this further for only the cells you need, otherwise this may slow your spreadsheet down.
Appreciating this was some time ago. I would like to do a similar thing however would like to append the Interior Color Reference ie. 255 to the cells value.
so if cell A1 has Hello in the cell and is Colored Red I'd want in the other worksheet cell A1: Hello | 255
Just used | as a delimiter but anything sensible...