Delete a row based on conditional value fill - vba

I am currently working on a database analysis and I have a Conditional format rule which fills a cell red. I need to delete the whole row for that cell which is red, I am able to do so if the cell is filled with that color manually but not through the conditional value rule, Here is what I have right now:
Sub delteRed()
Dim xRg As Range, rgDel As Range
Sheets("Hoja1").Select
For Each xRg In ThisWorkbook.ActiveSheet.Range("A2:A21")
If xRg.Interior.ColorIndex = 3 Then
If rgDel Is Nothing Then
Set rgDel = xRg
Else
Set rgDel = Union(rgDel, xRg)
End If
End If
Next xRg
If Not rgDel Is Nothing Then rgDel.EntireRow.Delete
End Sub

In a VBA sub procedure you can use the DisplayFormat to determine the color of a cell that has been adjusted by a CFR.
If xRg.DisplayFormat.Interior.ColorIndex = 3 Then
DisplayFormat cannot be used in a UDF.

Related

VBA for performing action until cell meets criteria

I've had no luck on searching this, I hope you all can help.
I am trying to perform the following in Excel:
If a cell matches a value from table1, highlight that cell. Then highlight the cell below it and continue to highlight the cells below it until it reaches a cell that matches a value from table2.
I cant figure out how to work in the loop function for this
Thanks in advance
I've written a simple for loop, which iterates through the data range. My data table consists of integers 1 - 15, named 'Data'. The starting point is named 'Point_1' and the ending point is named 'Point_2'.
The code is below, annotated for clarity. In essence, it is simply toggling highlight ON when the cell matches Point_1, and highlighting until it is toggled back OFF, when the cell matches Point_2.
Sub HighlightRange()
Dim cel As Range
Dim dataRange As Range
Dim highlighting As Boolean
highlighting = False
With Application.ActiveWorkbook.Sheets("Sheet1")
Set dataRange = .Range("Data") 'This is your data range. I named mine 'Data'
For Each cel In dataRange
'Check for beginning or end values
If cel = .Range("Point_1").Value Then 'This is your starting value. I named mine 'Point_1'
highlighting = True
ElseIf cel = .Range("Point_2").Value Then 'This is your ending value. I named mine 'Point_2'
highlighting = False
End If
'While highlighting is activated, highlight the current cell
If highlighting = True Then
cel.Interior.ColorIndex = 5
End If
Next cel 'Check all cells in dataRange
End With
End Sub
Notably, the end point is not highlighted. However, you can move the ElseIf statement that turns highlighting back off to the end of the for loop, after the highlight command. This will turn highlighting off AFTER highlighting Point_2 instead of just before. Code for this case is below.
Sub HighlightRange()
Dim cel As Range
Dim dataRange As Range
Dim highlighting As Boolean
highlighting = False
With Application.ActiveWorkbook.Sheets("Sheet1")
'This code also highlights Point_2.
Set dataRange = .Range("Data") 'This is your data range. I named mine 'Data'
For Each cel In dataRange
'Check for beginning or end values
If cel = .Range("Point_1").Value Then 'This is your starting value. I named mine 'Point_1'
highlighting = True
End If
'While highlighting is activated, highlight the current cell
If highlighting = True Then
cel.Interior.ColorIndex = 5
End If
If cel = .Range("Point_2").Value Then 'This is your ending value. I named mine 'Point_2'
highlighting = False
End If
Next cel 'Check all cells in dataRange
End With
End Sub
I hope this helped! You can change the highlight color from blue (colorIndex = 5) to whatever color you like. If you click Record Macro and format a cell just how you want, you can copy the generated code into this macro in place of the following line:
cel.Interior.ColorIndex = 5
Cheers and Good Luck!

Excel VBA - Change button visibility based on update to adjacent cell

I have data in column "AK" and a button in Column "AL"; there are several hundred rows and there is only one macro for all buttons as it uses relative references based on the row it is in.
I want the button to only be visible when there is data in the adjacent cell. The following pseudo-code explains what I am trying to achieve:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 37 Then
If Target.Value = 0 Then
Shapes(Target.offset(0, 1)).Visible = False
Else
Shapes(Target.offset(0, 1)).Visible = True
End If
End If
End Sub
The reason for doing this is that the value in AK is calculated based on other values and only displays once all mandatory fields have been completed. The button should only be available for an automation task once all details are complete. What real code would make this work without having to call each button out individually?
I'm not sure if you can directly reference a shape by its location on the sheet.
This code will look at each shape until it finds the one to the right of the cell you've just changed, it will then change the visibility based on the contents of the cell.
(Target.Value <> "") returns TRUE/FALSE.
This will only work if your buttons are placed in the correct cell (slightly too high and it will return the cell above).
Private Sub Worksheet_Change(ByVal Target As Range)
Dim shp As Shape
For Each shp In ThisWorkbook.Worksheets("Sheet1").Shapes
If shp.TopLeftCell.Address = Target.Offset(, 1).Address Then
shp.Visible = (Target.Value <> "")
Exit For 'Exit the loop - the correct button has been found.
End If
Next shp
End Sub
Edit:
I've updated the code so it checks that only a single cell has been changed and then looks at each dependent cell of the cell that was changed.
This will probably muck up if the dependent cell is on another sheet though.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rUpdated As Range
Dim shp As Shape
Dim rCell As Range
If Target.Cells.Count = 1 Then
'Hopefully someone will have better code than On Error....
On Error Resume Next
Set rUpdated = Range(Target.Dependents.Address)
On Error GoTo 0
If Not rUpdated Is Nothing Then
'Look at each dependent cell in rUpdated.
For Each rCell In rUpdated
'Look at each shape in the sheet and cross-reference with rCell.
For Each shp In Target.Parent.Shapes
If shp.TopLeftCell.Address = rCell.Offset(, 1).Address Then
shp.Visible = (Target.Value = 0)
Exit For 'Exit the loop - the correct button has been found.
End If
Next shp
Next rCell
End If
End If
End Sub
NB: I got the idea for checking the dependent cell from here: How can I run a VBA code each time a cell get is value changed by a formula?

Deselect Subrange

I want to lock the range A4:B9 in my sheet. Now as it looks like the only way to accomplish that is to set the Lock=false status to all cells I do not want to be locked and then protect the whole file.
My problem is, that I have just a little range that has to be protected, so I need to find a way to create a range with all cells but e.g. A4:B9. I know intersect and union but cannot come up with an idea to apply them to get my goal accomplished.
I wrote this that works for me:
Sub deselect_subranges()
Dim cell As Range
Dim rngAll As Range
Dim rngMy As Range
Dim rngNew As Range
Set rngAll = ThisWorkbook.Worksheets(1).Range("A1:AZ400")
Set rngMy = ThisWorkbook.Worksheets(1).Range("C3:E8")
Set rngNew = Nothing
For Each cell In rngAll
If Intersect(cell, rngMy) Is Nothing Then
If rngNew Is Nothing Then
Set rngNew = cell
Else
Set rngNew = Union(rngNew, cell)
End If
End If
Next cell
rngNew.Select
End Sub

How to highlight empty/blank cells using VBA macro

I realized I messed up asking my very first question, so I will try one last time. I am targeting the same 4 columns from 2 separate sheets that have cells that either contain text or do not. Sheet 1 will be updated automatically, so I will be running this code daily to manually update sheet 2. I am trying to find a way to basically find out which cells are missing the text using a macro. I tried using a code that I found on this website that puts borders on cells containing text and clears borders for empty cells.
Sub BorderForNonEmpty()
Dim myRange As Range
Set myRange = Sheet1.Range("C2:C252")
' Clear Existing Borders
myRange.Borders.Linestyle = xlLineStyleNone
' Test Each Cell and Put a Border Around it if it has content
For Each myCell in myRange
If myCell.Text <> "" Then
myCell.BorderAround (xlContinuous)
End If
Next
End Sub
This code works, but I want to try to highlight the empty cells with a color opposed to clearing its border. This is also my first time posting on StackOverflow, so I apologize beforehand. Thank you.
Instead of looping through all cells, Excel has a built in function to select blank Cells. This should be faster, and more reliable.
Sub BorderForNonEmpty()
Dim myRange As Range
Set myRange = Sheet1.Range("C2:C252")
'clear all color
myRange.Interior.ColorIndex = xlNone
'color only blank cells
myRange.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 6
End Sub
Another option could be to just use conditional formatting (another built-in feature), but that can be hard to control for changing ranges.
Replace
myCell.BorderAround (xlContinuous)
with
myCell.Interior.Color = RGB(100, 100, 100)
Give this a try:
Sub BorderForNonEmpty()
Dim myRange As Range
Set myRange = Sheet1.Range("C2:C252")
For Each myCell In myRange
If myCell.Text = "" Then
myCell.Interior.ColorIndex = 6
End If
Next
End Sub
EDIT#1:
Sub BorderForNonEmpty()
Dim myRange As Range
Set myRange = Sheet1.Range("C2:C252")
For Each myCell In myRange
If myCell.Text = "" Then
myCell.Interior.ColorIndex = 6
Else
myCell.Interior.ColorIndex = xlNone
End If
Next
End Sub
EDIT#2:
To make the macro "clickable":
Put any AutoShape on the worksheet
Format the AutoShape
Right-click the AutoShape and assign the macro to it.

Get the column of a user selected cell using vba excel

Refer to this question: Let the user click on the cells as their input for an Excel InputBox using VBA.
I use this line of code Set rng = Application.InputBox(Prompt:="Select the cell you want to edit.", Title:="CELL TO EDIT", Type:=8) to ask the user to select a cell. If the cell that is selected is in column G then I will need to call a subroutine. So, I need to figure out what column the selected cell is in.
Thanks for the help!
Use rng.Column property to return column number:
Sub test()
Dim rng As Range
On Error Resume Next
Set rng = Application.InputBox(Prompt:="Select the cell you want to edit.", title:="CELL TO EDIT", Type:=8)
On Error GoTo 0
If rng Is Nothing Then Exit Sub
'column G=7
If rng.Column = 7 Then
'do something
End If
End Sub
If user can select more than one cell, change
If rng.Column = 7 Then
to
If Not Application.Intersect(rng, Sheets("Sheet1").Range("G:G")) Is Nothing Then