Turning coloured cells into no-fill cells throughout the whole excel spreadsheet - vba

This is my code which does not seem to work. I basically get an "object required" error in the 3rd line "for each cell in UsedRange.Cells"
Sub AgreeAll()
Dim myRange As Range
For Each cell In UsedRange.Cells
If cell.Interior.Color = RGB(255, 192, 0) Then
If myRange Is Nothing Then
Set myRange = cell
Else
Set myRange = Union(myRange, cell)
End If
End If
Next
If Not myRange Is Nothing Then
myRange.Interior.Color = RGB(255, 255, 255)
Range("P7").ClearContents
Columns("E:F").EntireColumn.Delete
End If
End Sub

The anatomy of a basic If block:
If [condition] = [true | false] Then
'// Do something
[Else]
['// Do something Else]
End If
So in your case, you would want something like:
(Revised after comment feedback)
Dim myRange As Range
For Each cell In UsedRange.Cells
If cell.Interior.ColorIndex = 44 Then
If myRange Is Nothing Then
Set myRange = cell
Else
Set myRange = Union(myRange, cell)
End If
End If
Next
If Not myRange Is Nothing Then
myRange.Interior.ColorIndex = xlNone
Range("P7").ClearContents
Columns("E:F").EntireColumn.Delete
End If

It has never been determined whether the cells are orange filled by manually setting a background fill or whether they are filled by a conditional formatting rule. If the latter, then you can set the Interior.Pattern property to white or xlNone all you like; the CF rule will override anything you set. You can clear the content (which I assume drives the CF rule) or delete the CF rule from that cell.
The AutoFilter method can filter for the Range.DisplayFormat property. The .DisplayFormat includes both regular formatting and conditional formatting; i.e. if you see orange, then so does .DisplayFormat.
Sub AgreeAll()
Dim rng As Range
Dim c As Long
'Application.ScreenUpdating = False
With Worksheets("Sheet2") '<~~set this worksheet reference properly!
'why wait until the end to do this? It only means you have to process rtwo columns you plan to delete
.Columns("E:F").EntireColumn.Delete
If .AutoFilterMode Then .AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
For c = 1 To .Columns.Count
With .Columns(c)
.AutoFilter Field:=1, Criteria1:=RGB(255, 192, 0), _
Operator:=xlFilterCellColor
If .Cells.SpecialCells(xlCellTypeVisible).Count > 1 Then
.Offset(1, 0).Interior.Pattern = xlNone
For Each rng In .SpecialCells(xlCellTypeVisible)
If rng.DisplayFormat.Interior.Color = RGB(255, 192, 0) Then _
rng.FormatConditions.Delete
Next rng
'your code cleared P7; did you want to clear all of the orange cells?
'.Offset(1, 0).ClearContents
End If
.AutoFilter
End With
Next c
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
Neither your code nor narrative adequately explains what Range("P7").ClearContents was supposed to do. If you wanted to clear the orange cells then I've left some commented code in the appropriate place to do that.
If you run into problems, feel free to leave a comment but remember to provide sufficient information that I can help you with it.

Try this :
Sub AgreeAll()
Dim wS As Worksheet, _
myRange As Range, _
aCell As Range
Set wS = ActiveSheet
'set ws =sheets("Your_Sheet's_Name")
With wS
For Each aCell In .UsedRange.Cells
If aCell.Interior.Color = RGB(255, 192, 0) Then
If myRange Is Nothing Then
Set myRange = aCell
Else
Set myRange = Union(myRange, aCell)
End If
End If
Next
If Not myRange Is Nothing Then
myRange.Interior.Pattern = xlNone
.Range("P7").ClearContents
.Columns("E:F").EntireColumn.Delete
End If
End With
End Sub

Related

delete all cells of a certain color

This seems relatively simple and as I understand, it is possible. But I can't seem to figure it out or find exactly what I am looking for on the internet.
I have some excel data in column A and some of the data is blue (0,0,255), some is red (255,255,255), some is green (0, 140, 0). I want to delete all blue data.
I was told that:
Sub test2()
Range("A2").DisplayFormat.Font.Color
End Sub
Would give me the colors... but when I run that it says invalid use of the property and highlights .color
Instead I clicked on the:
Font color drop down
then more colors
then custom colors
then I can see that the data in blue is at (0,0,255)
So then I tried:
Sub test()
Dim wbk As Workbook
Dim ws As Worksheet
Dim i As Integer
Set wbk = ThisWorkbook
Set ws = wbk.Sheets(1)
Dim cell As Range
With ws
For Each cell In ws.Range("A:A").Cells
'cell.Value = "'" & cell.Value
For i = 1 To Len(cell)
If cell.Characters(i, 1).Font.Color = RGB(0, 0, 255) Then
If Len(cell) > 0 Then
cell.Characters(i, 1).Delete
End If
If Len(cell) > 0 Then
i = i - 1
End If
End If
Next i
Next cell
End With
End Sub
I found this on the web as a solution in several places but when I run it, nothing seems to happen.
This is basic, if your cells with blue font are not deleted then the font is a different color. Change the range to meet your needs.
For Each cel In ActiveSheet.Range("A1:A30")
If cel.Font.Color = RGB(0, 0, 255) Then cel.Delete
Next cel
Updated to allow user to select the first cell in the column with the font color, obtain the font color, and clear all the cells that match the font color.
Dim rng As Range
Set rng = Application.InputBox("Select a Cell:", "Obtain Range Object", Type:=8)
With ActiveSheet
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
Dim x As Long
x = rng.Row
For i = lr To x Step -1
If .Cells(i, 1).Font.Color = rng.Font.Color Then .Cells(i, 1).Clear
Next i
End With
you coudl use Range object Autofilter() method with xlFilterFontColor operator;
Sub test()
With ThisWorkbook.Sheets(1)
With .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
.AutoFilter Field:=1, Criteria1:=RGB(0, 0, 255), Operator:=xlFilterFontColor
If Application.WorksheetFunction.Subtotal(103, .Cells) > 0 Then .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).ClearContents
End With
.AutoFilterMode = False
If .Range("A1").Font.Color = RGB(0, 0, 255) Then .Range("A1").ClearContents ' check first row, too (which is excluded by AutoFilter)
End With
End Sub
Something like following where all qualifying cells are gathered together, using Union, and deleted in one go. If deleting entire rows individually, you always need to loop backwards. Deleting/clearing in one go is more efficient.
Sub test()
Dim wbk As Workbook, ws As Worksheet
Dim i As Long, currentCell As Range, unionRng As Range
Set wbk = ThisWorkbook
Set ws = wbk.Worksheets("Sheet1")
With ws
For Each currentCell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row) '<==assuming actual data present
If currentCell.Font.Color = RGB(0, 0, 255) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(currentCell, unionRng)
Else
Set unionRng = currentCell
End If
End If
Next
End With
If Not unionRng Is Nothing Then unionRng.Delete
End Sub
Option Explicit
Sub test2()
Dim cel As Range
Dim LR As Long
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For Each cel In ActiveSheet.Range("A1:A" & LR)
If cel.Font.Color = RGB(0, 0, 255) Then cel.ClearContents
Next cel
End Sub

Conditional Format Row based on IFERROR VLOOKUP result

I am trying to conditinally format every row that fails my IFERROR VLOOKUP formula in column A. I have gotten to where I can get the IFERROR cell to highlight, but not the entire now. Please help. This is what I have so far:
Range("A2:AH2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""Not VA Student"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
It is unclear to me what you meant by your IFERROR VLOOKUP as i can't see this in your code.
I think you are wanting to only highlight rows where "Not VA Student" is found in the stipulated range. Your current code does this for individual cells.
I think the range selected is likely excessive in that you select to the bottom of the sheet within the specified column limits.
Try the following. I have made minor amendments to existing code, credits to user3598756
Private Sub HighlightRows()
Dim wb As Workbook
Dim ws As Worksheet
Dim FoundCell As Range
Dim rng As Range
Dim FirstFound As String
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1") ' change as appropriate
Const fnd As String = "Not VA Student"
With ws.Range("A2:AH" & ws.Range("AH2").End(xlDown).Row) '<--| reference the range to search into ''#qharr: this seems excessive to me
Set FoundCell = .Find(what:=fnd, after:=.Cells(.Cells.Count)) '<--| find the first cell
If Not FoundCell Is Nothing Then 'Test to see if anything was found
FirstFound = FoundCell.Address ' <--| store the first found cell address
Set rng = FoundCell '<--| initialize the range collecting found cells. this to prevent first 'Union()' statement from failing due to 'rng' being 'Nothing'
Do
Set rng = Union(rng, FoundCell) 'Add found cell to rng range variable
'Find next cell with fnd value
Set FoundCell = .FindNext(after:=FoundCell)
Loop While FoundCell.Address <> FirstFound 'Loop until cycled through all finds
End If
End With
If Not rng Is Nothing Then rng.EntireRow.Interior.Color = 65535
End Sub

VBA Unlock Cells in a Range if colorindex = 0

I am trying to unlock cells in a given range if they do not have a background color.
Sub macunlock()
Dim rng1 As Range
Set rng1 = Range("A1:C1")
For Each cell In rng1
If cell.Interior.ColorIndex = 0 Then cell.Locked = False
Next cell
End Sub
However, the specified cells do not unlock.
Because, blank formats are -4142 and not 0
Change to this:
If cell.Interior.ColorIndex = -4142 Then cell.Locked = False
Robust way:
Sub macunlock()
Dim rng1 As Range
Set rng1 = Range("A1:C1")
For Each cell In rng1
If cell.Interior.ColorIndex = XlColorIndex.xlColorIndexNone Then
cell.Locked = False
End If
Next cell
End Sub

How to copy a range and omit blank cells?

In column A on sheet 1 there are 3000 cells that I need copied at 350 cells each. My current Macro is copying everything just fine until I get to the end and it copies blanks. Is there a way to include a "cell is blank do nothing" code into my macros?
Sorry if this sounds uneducated, I'm just starting on learning macro.
Here is a copy of the current macro, the rest of the macro is the same as this just with increasing numbers by 350.
Sub Copy_Bins_1_350()
If Range("D12").Value <> "!" Then
Exit Sub
ElseIf Range("D12").Value = "!" Then
Sheets("sheet1").Select
Range("B2:B351").Select
Selection.Copy
Range("B2").Select
Sheets("sheet2").Select
Range("E12").Select
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
End If
End Sub
You can use Union to form your own range of non empty cells and then copy them.
Also INTERESTING READ
Try this (TRIED AND TESTED)
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Dim aCell As Range, rngCopyFrom As Range, rng As Range
Dim lRow As Long
Set wsI = ThisWorkbook.Sheets("BIN LIST PASTE")
Set wsO = ThisWorkbook.Sheets("BIN LIST COPY")
Set rng = wsI.Range("B2:B351")
For Each aCell In rng
If Len(Trim(aCell.Value)) <> 0 Then
If rngCopyFrom Is Nothing Then
Set rngCopyFrom = aCell
Else
Set rngCopyFrom = Union(rngCopyFrom, aCell)
End If
End If
Next
If Not rngCopyFrom Is Nothing Then _
rngCopyFrom.Copy wsO.Range("E12")
With wsO
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
Set rng = .Range("E12:E" & lRow)
With rng.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
End With
End Sub

Remove cells in Excel which have zero values

I have been trying to remove/hide cells which values are equal to zero (0).
Sub HideRows()
Dim cell As Range, rng As Range
Cells.Rows.Hidden = False
On Error Resume Next
Set rng = Columns(5).SpecialCells(xlConstants, xlNumbers)
On Error GoTo 0
For Each cell In rng
If cell.Value = 0 Then
cell.EntireRow.Hidden = True
End If
Next
End Sub
The code removes the entire row. I want to remove the description of the value and the value.
This code will quickly clear (erase) values and comments from cells in column E that have a value of 0
Sub Testme()
Dim rng1 As Range
Set rng1 = Columns(5)
With rng1
.AutoFilter 1, "0"
With rng1.Offset
.ClearContents
.ClearComments
End With
With rng1.Offset(0, -1)
.ClearContents
.ClearComments
End With
End With
End Sub