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
Related
I have written a small code that allow me to:
in a defined range (xrng) in column F, find all the cells that contain certain text and once found, select all the cells in the range A:G on the same row and delete them. I have a reverse loop, which work partially, as ignores some cells in the range, specifically the 2nd and the 3rd. Below a before and after pic:
Here my code:
Sub removeapp()
Dim g As Long, xrng As Range, lastrow As Long, i As Long
i = 4
lastrow = Cells(Rows.Count, "F").End(xlUp).Row
Set xrng = Range(Cells(lastrow, "F"), Cells(i, "F"))
For g = xrng.Count To i Step -1
If xrng.Cells(g).Value = "Adjustment" Or xrng.Cells(g).Value = "Approved" Then
Range(Cells(xrng.Cells(g).Row(), "A"), Cells(xrng.Cells(g).Row(), "G")).Delete
End If
Next
End Sub
Could you help me to figure out why?
Also, the code runs really slow... if you have any tip to make it slighlty faster would be great!
Try this, please:
Sub removeappOrig()
Dim xrng As Range, lastrow As Long, sh As Worksheet
Set sh = ActiveSheet 'good to put here your real sheet
lastrow = sh.Cells(sh.Rows.count, "F").End(xlUp).Row
Set xrng = sh.Range("A4:F" & lastrow)
xrng.AutoFilter field:=6, Criteria1:="=Adjustment", Operator:=xlOr, _
Criteria2:="=Approved", VisibleDropDown:=False
Application.DisplayAlerts = False
xrng.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True
sh.AutoFilterMode = False
End Sub
The next code is also fast enough since it iterates between array elements (in memory), not deletes row by row (it creates a ranges Union) and delete all at once:
Private Sub remoRangesAtOnce()
Dim i As Long, lastRow As Long, sh As Worksheet
Dim arrF As Variant, rng As Range, rngDel As Range
Set sh = ActiveSheet 'please name it according to your sheet name
lastRow = sh.Cells(sh.Rows.count, "F").End(xlUp).Row
Set rng = sh.Range("F4:F" & lastRow)
arrF = rng.Value
For i = LBound(arrF) To UBound(arrF)
If arrF(i, 1) = "Adjustment" Or arrF(i, 1) = "Approved" Then
If rngDel Is Nothing Then
Set rngDel = sh.Range(sh.Range("A" & i + 3), sh.Range("F" & i + 3))
Else
Set rngDel = Union(rngDel, sh.Range(sh.Range("A" & i + 3), sh.Range("F" & i + 3)))
End If
End If
Next i
If Not rngDel Is Nothing Then rngDel.Delete xlShiftUp
End Sub
I am working on the below code to insert same entire row below/beneath original one. I had a hard time fulfilling the requirement because I am just new to making macros.
I already tried searching but not able to code correctly. It is working to insert an empty row. But what I need is to insert the row that met the condition. Below is the screenshot/code for my macro.
Private Sub CommandButton1_Click()
Dim rFound As Range, c As Range
Dim myVals
Dim i As Long
myVals = Array("LB") '<- starts with 51, VE etc
Application.ScreenUpdating = False
With Range("F1", Range("F" & Rows.Count).End(xlUp))
For i = 0 To UBound(myVals)
.AutoFilter field:=1, Criteria1:=myVals(i)
On Error Resume Next
Set rFound = .Offset(2).Resize(.Rows.Count - 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilter
If Not rFound Is Nothing Then
For Each c In rFound
Rows(c.Row + 1).Insert
c.Offset(1, -1).Value = ActiveCell.Value
Next c
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
Sub Test()
Dim rng As Range
Dim rngData As Range
Dim rngArea As Range
Dim rngFiltered As Range
Dim cell As Range
Set rng = Range("A1").CurrentRegion
'Exclude header
With rng
Set rngData = .Offset(1).Resize(.Rows.Count - 1)
End With
rng.AutoFilter Field:=6, Criteria1:="LB"
Set rngFiltered = rngData.Columns("F:F").SpecialCells(xlCellTypeVisible)
rng.AutoFilter Field:=6
For Each rngArea In rngFiltered.Areas
For Each cell In rngArea
'// When inserting a row,
'// iteration variable "cell" is adjusted accordingly.
Rows(cell.Row + 1).Insert
Rows(cell.Row).Copy Rows(cell.Row + 1)
Next
Next
End Sub
Below is the code I just used . Thank you!
Private Sub CommandButton2_Click()
Dim x As Long
For x = ActiveSheet.UsedRange.Rows.CountLarge To 1 Step -1
If Cells(x, "F") = "LB" Then
Cells(x, "F") = "ComP"
Cells(x + 1, "F").EntireRow.Insert
Cells(x, "F").EntireRow.Copy Cells(x + 1, "F").EntireRow
End if
Next x
End Sub
I have a lengthy spreadsheet that is always being updated. When the task is complete the row of data will be filled with the standard color green. I want to be able to code a macro that can take all rows filled with the color green from the current sheet and paste them on a new sheet? Any ideas?
If this helps, the row numbers are not constant, they are always changing. The number of rows filled with green are not always the same.
Maybe you can modify the code below as per your requirement.
Sub CopyGreenColoredRows()
Dim wsSource As Worksheet, wsDest As Worksheet
Dim i As Long, lr As Long, lc As Long, dlr As Long
Application.ScreenUpdating = False
Set wsSource = Sheets("Sheet1") 'Source sheet with colored rows/Sheet to copy data from
Set wsDest = Sheets("Sheet2") 'Destination Sheet/copy the data to
'Clearing the destination sheet excluding headers before pasting new data
'Remove this line if not required
wsDest.UsedRange.Offset(1).Clear
lr = wsSource.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lc = wsSource.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
With wsSource
'Assuming Row1 is the header row
For i = 2 To lr
'The code assumes that the color applied is through the conditional formatting
If .Range("A" & i).DisplayFormat.Interior.Color = 5287936 Then
dlr = wsDest.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
.Range("A" & i, .Cells(i, lc)).Copy wsDest.Range("A" & dlr)
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
The code below does what you've described. Notice in the animated .gif that Sheet2 starts off blank and then when run, just the green rows are copied over. Of course, you'll need to modify for your exact situation.
Option Explicit
Sub transferGreen()
Dim sourceSh As Worksheet, destSh As Worksheet
Dim cell As Range, sourceR As Range, destR As Range
Set sourceSh = Worksheets("Sheet1")
Set sourceR = sourceSh.Range("A1")
Set sourceR = sourceSh.Range(sourceR, sourceR.End(xlDown))
Set destSh = Worksheets("Sheet2")
Set destR = destSh.Range("A1")
If destR.Offset(1, 0) <> "" Then Set destR = destR.End(xlDown).Offset(1, 0)
sourceR.Select
destSh.Activate
For Each cell In sourceR
If cell.Interior.Color = 5287936 Then
sourceSh.Rows(cell.row).Copy
destSh.Rows(destR.row).Select
destSh.Paste
Set destR = destR.Offset(1, 0)
End If
Next
End Sub
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
This is what I have already, and it works great in removing #N/As from the range. I am now looking to modify it to do the same thing for cells that contain 0.
Sub DeleteErrorRows()
Dim r As Range
Set r = Range("B:B").SpecialCells(xlCellTypeConstants, 16).EntireRow
r.Copy Sheets("Sheet2").Range("A1")
r.Delete
End Sub
Thanks :)
Try this. It autofilters your column and keeps rows that have the findMe value in your source worksheet. You can set it to 0 as I have in the example or to whatever else you want. It copies those rows (except for the header row) to the target sheet and then deletes them from the source sheet.
Note that this also finds the first empty row on the target sheet so that you can run it multiple times without overwriting what you've already moved to the target sheet.
Sub CopyThenDeleteRowsWithMatch()
Dim wb As Workbook
Dim ws As Worksheet
Dim tgt As Worksheet
Dim rng As Range
Dim lastRow As Long
Dim firstPasteRow As Long
Dim findMe As String
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
Set tgt = wb.Sheets("Sheet2")
lastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
firstPasteRow = tgt.Range("B" & tgt.Rows.Count).End(xlUp).Row + 1
findMe = "0"
Set rng = ws.Range("B1:B" & lastRow)
' filter and delete all but header row
With rng
.AutoFilter Field:=1, Criteria1:="=" & findMe
With .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
.Copy tgt.Range("A" & firstPasteRow)
.Delete
End With
End With
' turn off the filters
ActiveSheet.AutoFilterMode = False
End Sub
Consider:
Sub DeleteZeroRows()
Dim r As Range, rTemp As Range, rB As Range
Set rB = Intersect(Range("B:B"), ActiveSheet.UsedRange)
Set r = Nothing
For Each rTemp In rB
If Not IsEmpty(rTemp) And rTemp.Value = 0 Then
If r Is Nothing Then
Set r = rTemp
Else
Set r = Union(r, rTemp)
End If
End If
Next rTemp
Set r = r.EntireRow
r.Copy Sheets("Sheet2").Range("A1")
r.Delete
End Sub