Deleting condition formatting for texts & blanks which may occur at any cell in range(b1:b54) - excel

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

Related

Apply loop to check if value is equal between ranges

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

How to test if a Range is blank?

I'm making a college exercise and I need to check if some specific cells are blank. If they are blank, I need to write something in them. I've tried to make one program, but it did not worked, showing me the error: 13.
Here is my code:
Option Explicit
Sub Test()
If Range("a1:e1").Value = "" Then
Range("a1:e1") = "x"
End If
End Sub
Thank you for the help!
If a range contains 1 cell then its .Value property returns a single scalar value.
However, If a range contains multiple cells then its .Value property returns an Array.
You could iterate over all the cells in the range to see if they are all empty. Alternatively, you could use WorksheetFunction.CountBlank to see if the number of blanks in the range matches the number of cells in the range.
With Range("a1:e1")
If WorksheetFunction.CountBlank(.Cells) = .Cells.Count Then
Range("a1:e1") = "x"
End If
End With
It's not clear what you want to do in case one cell in this range is not empty.
Since the case where you want to fill the cells if they are all blank has been already covered, I will cover the case where you want to check if there are any blank cells in a range and if yes then fill them.
Dim cell As Range
For Each cell In Range("A1:E1").Cells
If cell.Value = "" Then
cell.Value = "x"
End If
Next cell
You could also use WorksheetFunction.CountA. It counts non empty cells. Like this:
If WorksheetFunction.CountA(Range("a1:e1")) = 0 Then
Range("a1:e1") = "x"
End If
It does the same as TinMan's code. But the check is differend.
You could try this code:
Sub Test()
Dim rng As Range
Set rng = Range("A1:E1")
' if we come upon non-empty cell we exit sub and do nothing
For Each cell In rng
If Not IsEmpty(cell) Then Exit Sub
Next
' if we reached this, all cells are empty
rng.Value = "x"
End Sub

Find row indices of empty cells in a given range

Is it possible to write a vba macro that determines if there are any empty cells in a given range and returns the row number of that cell?
I'm new to vba and all that I managed to write after searching the internet was something that takes a range and colors every emty cell in it red:
Sub EmptyRed()
If TypeName(Selection) <> "Range" Then Exit Sub
For Each cell In Selection
If IsEmpty(cell.Value) Then cell.Interior.Color = RGB(255, 0, 0)
Next cell
End Sub
The macro does basically what I want, but instead of coloring the empty cell red I would like to know the row index of the empty cell.
A little background info: I have a very large file (about 80 000 rows) that contains many merged cells. I want to import it into R with readxl. Readxl splits merged cells, puts the value in the first split cell and NA into all others. But a completely empty cell would also be assigned NA, so I thought the best thing would be to find out which cells are empty with Excel, so that I know which NA indicate a merged cell or an empty cell. Any suggestions on how to solve this problem are very welcome, thanks!
Edit: To clarify: Ideally, I want to unmerge all cells in my document and fill each split cell with the content of the previously merged cell. But I found macros on the web that are supposed to do exactly that, but they didn't work on my file, so I thought I could just determine blank cells and then work on them in R. I usually don't work with Excel so I know very little about it, so sorry if my thought process is far too complicated.
To do exactly what you state in your title:
If IsEmpty(cell.Value) Then Debug.Print cell.Row
But there are also Excel methods to determine merged cells and act on them. So And I'm not sure exactly what you want to do with the information.
EDIT
Adding on what you say you want to do with the results, perhaps this VBA code might help:
Option Explicit
Sub EmptyRed()
Dim myMergedRange As Range, myCell As Range, myMergedCell As Range
Dim rngProcess As Range
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set rngProcess = Range("A1:B10")
For Each myCell In rngProcess
If myCell.MergeCells = True Then
Set myMergedRange = myCell.MergeArea
With myMergedRange
.MergeCells = False
.Value = myCell(1, 1)
End With
End If
Next myCell
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
End sub
Note that I explicitly declare all variables, and I hard coded the range to check. There are various ways of declaring the range to be checked; using 'Selection' is usually rarely preferred.
Before anything else: From the opposite end of the spectrum, you can use Range.MergeCells or Range.MergeArea to determine if a Cell is part of a Merged Area. But, I digress...
You can use Cell.Row to get the row number. How you return or display that is up to you - could be a Message Box, a delimited string, or an array, or even a multi-area range.
A Sub cannot return anything once called, so you may want a Function instead, e.g. Public Function EmptyRed() As String
(Also, I would recommend you get in the habit of explicitly declaring all of your variables, and perhaps using Option Explicit too, before you run into a typo-based error. Just add Dim cell As Range at the top of the sub for now)
Sub FF()
Dim r, wksOutput As Worksheet
Dim cell As Range, rng As Range, rngArea As Range
With Selection
.UnMerge
'// Get only blank cells
Set rng = .SpecialCells(xlCellTypeBlanks)
'// Make blank cells red
rng.Interior.Color = vbRed
End With
'// Create output worksheet
Set wksOutput = Sheets.Add()
With wksOutput
For Each rngArea In rng.Areas
For Each cell In rngArea
r = r + 1
'// Write down the row of blank cell
.Cells(r, 1) = cell.Row
Next
Next
'// Remove duplicates
.Range("A:A").RemoveDuplicates Array(1), xlNo
End With
End Sub
There are a couple ways:
Sub EmptyRed()
Dim rgn,targetrgn as range
Dim ads as string ‘ return rgn address
Set targetrgn= ‘ your selection
For Each rgn In Targetrgn
If IsEmpty(rgn.Value) Then
‘1. Use address function, and from there you can stripe out the column and row
Ads=application.worksheetfunction.addres(cell,1)’ the second input control the address format, w/o $
‘2. Range.row & range.column
Ads=“row:” & rgn.row & “, col: “ & rgn.column
End if
Next rgn
End Sub
Ps: I edited the code on my phone and will debug further when I have a computer. And I am just more used to use “range” rather than “cell”.
To clarify: Ideally, I want to unmerge all cells in my document and fill each split cell with the content of the previously merged cell.
Cycle through all cells in the worksheet's UsedRange
If merged, unmerge and fill the unmerged area with the value from the formerly merged area.
If not merged but blank, collect for address output.
Sub fillMerged()
Dim r As Range, br As Range, mr As Range
For Each r In ActiveSheet.UsedRange
If r.Address <> r.MergeArea.Address Then
'merged cells - unmerge and set value to all
Set mr = r.MergeArea
r.UnMerge
mr.Value = mr.Cells(1).Value
ElseIf IsEmpty(r) Then
'unmerged blank cell
If br Is Nothing Then
Set br = r
Else
Set br = Union(br, r)
End If
End If
Next r
Debug.Print "blank cells: " & br.Address(0, 0)
End Sub

Copy cell background color and paste it to corresponding cell of another sheet

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...

How to dynamically select specific cells based on other cells in the same row

Similar to how if you drag a cell down in a spreadsheet it will continue to reference cells in the same row of that formula. I need to find a way that I can search the spreadsheet for letter Y in the I column and if it finds Y in the I column it will then Select the cells in that same row for column B Through AR. then hide just those cells not the entire row. This is what I have so far:
Sub Macro1()
'Sub HideRows()
Dim cell As Range
For Each cell In Range("I1:I5000")
If UCase(cell.Value) = "Y" Then
Select (??? this is where I need to find help selecting the proper range.)
Selection.NumberFormat = ";;;"
End If
Next
Calculate
End Sub
Thanks,
You should be able to use your cell object? No need to Select anything.
For Each cell In Range("I1:I5000")
If UCase(cell.Value) = "Y" Then
cell.NumberFormat = ";;;"
End If
Next
Regarding hiding cells, I don't think you can hide individual cells without hiding the entire row.
cell.EntireRow.Hidden = True
This will set the cell format of column B - H to ;;; on the rows that contain Y in column I.
Sub test()
Dim sht As Worksheet, cell As Range
Dim rangeString As String
Set sht = ActiveSheet
For Each cell In sht.Range("I1:I5000")
If UCase(cell.Value) = "Y" Then
'Columns B --> H
sht.Cells(cell.Row, 2).Resize(1, 7).NumberFormat = ";;;"
End If
Next cell
End Sub
Autofilter would work, but obviously, you're not hiding the cells, since you can only hide full columns or rows. You're just obfuscating the display, but the contents can still be seen in the formula bar.
Sub Macro1()
'Sub HideRows()
Dim cell As Range
with Range("I1:I5000")
.autofilter
.autofilter field:=1,criteria1:="Y"
.offset(1).resize(.rows.count-1).specialcells(xlCellTypeVisible).offset(0,-7).resize(, 43).NumberFormat = ";;;"
.autofilter
end with
End Sub

Resources