Show Shapes Based on Cell Value - excel

Show Shapes Based on Cell Value
I want to show and hide shapes on a sheet based on the cell value >1. If cell A1 = 1 the shape should be visible and for value is 0, the shape should be hidden. i need the code for compelete my job,

The following Private Sub should work whether the cell value changes manually or as a result of a formula. It assumes cell A1 contains the test value, and simply add the name of your shape to make it work:
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveSheet.Range("A1") = 1 Then
Me.Shapes("Shape Name").Visible = True
Else
If ActiveSheet.Range("A1") = 0 Then
Me.Shapes("Shape Name").Visible = False
End If
End If
End Sub

Related

Put first cell's value depend on which row is selected, into another cell

i need to write a cell value in other cell by clicking a range of cells as below:
my range is between b1:x30 and i want every time i click on these selection range then the value of b1 to b30 (depending which row selected) will write in cell z1
i tried to write a code as below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:X30")) Is Nothing Then
If Intersect(Target, Range("B1:X30")) Then
Worksheets("Order Sheet").Range("Z1").Value = .Range("B1:B30").Value
End If
End If
End If
End Sub
i know this code is not complete yet and i need to help to complete.

select cell in range inputs value into cell in another sheet

In the code below that i got working, when i select a cell in a range, an input box pops up and my input is sent to the same cell i clicked but in sheet 2. I want to take this one step further. i want to bypass the input box completely and just send the value F, and i only want to do this after i click cell b2. so cell b2 would have to work as some kind of toggle (maybe put an invisible shape over it to act as a button?)
Example 1: sheet 1, select cell B2 turns on macro, select cell in range example: D10 inputs the letter F into cell D10 on sheet 2, select cell B2 turns off macro so if i select cell D10 or any cell in that range nothing will happen anymore. it would also need to remove the value F from D10 if the cell is clicked again while the macro is on.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
xRtn = Application.InputBox("Insert your value please")
Sheets("2020").Range(Target.Address).Value = xRtn
End If
End If
End Sub
Untested. I'm not sure if I understood all of your objectives.
I think if you add a checkbox to your worksheet (resize and store it wherever you want; maybe in cell B2) called "Check Box 1" then the below code should work.
One way of adding a check box might be: Excel > Developer > Insert > Check Box (Form Control) (depending on your Excel version). If the Developer tab is not visible, you may need to get it to show first.
Option Explicit
Private Function GetCheckBox() As Shape
Set GetCheckBox = Me.Shapes("Check Box 1")
End Function
Private Function IsCheckBoxTicked() As Boolean
IsCheckBoxTicked = (GetCheckBox.OLEFormat.Object.Value = 1)
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge <> 1 Then Exit Sub
If Intersect(Target, Me.Range("D9:AS20")) Is Nothing Then Exit Sub
If IsCheckBoxTicked() Then
With ThisWorkbook.Worksheets("2020").Range(Target.Address)
.Value = IIf(.Value = "F", Empty, "F")
End With
End If
End Sub

How do I run VBA code when none of the cells in a filtered table column are blank?

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

Using a Worksheet_Change target value that is a formula output not a manual input

I am writing code to clear the contents of a cell if another cell equals 2 different values. The first Target Range value is a formula pulling in the number from another sheet. The second target value is a manual input.
The second part of this code works, but not the first part. If I change the value in D9 to a manual input then the first part of the code works.
What do I need to change so it recognizes the value brought in from another sheet?
If Not Intersect(Target, Range("D9")) Is Nothing Then
If Target = 1 Then
Range("D23").ClearContents
Range("E23").ClearContents
Range("F23").ClearContents
ElseIf Target = 2 Then
Range("D23").ClearContents
Range("E23").ClearContents
Range("F23").ClearContents
End If
ElseIf Not Intersect(Target, Range("D11")) Is Nothing Then
If Target = 1 Then
Range("E13").ClearContents
Range("F13").ClearContents
Range("E23").ClearContents
Range("F23").ClearContents
ElseIf Target = 2 Then
Range("F13").ClearContents
Range("F23").ClearContents
End If
End If
Intersect only gives you a value if the interactions is done in the range you insert:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Columns(1)) Is Nothing Then
ThisWorkbook.Sheets(1).Range("D1").Value = "The active cell does NOT Intersect"
ThisWorkbook.Sheets(1).Range("D2").Value = Target
Else
ThisWorkbook.Sheets(1).Range("D1").Value = "The active cell does Intersect"
ThisWorkbook.Sheets(1).Range("D2").Value = Target
End If
End Sub
if you want to take the range from another sheet you need to write: sheets("YOUR_SHEETNAME").Range("D11").
you are right now looking if the cell selected is cell D9 or D11 in one sheet

(VBA-EXCEL) Automatically change and increment the value of an activex textbox based from a cell value

I have an activex textbox in worksheet1 that needs to automatically change based on the cell value which is in worksheet2 (where my table is).
This value should be incremented, like the concept of ID Number which increments if you add a customer,the incremented value must be displayed at the textbox ready to be saved.
Does anyone have any constructive suggestion concerning the above?
Kindly paste the below code in worksheets(2) of your workbook. Also txtVal is the name of textbox on Worksheets(1)
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'Assuming A1 is the cell in worksheet 2 and it has numeric value as it should be incremented.
If Target.Count = 1 And Target.Address = "$A$1" Then
If IsNumeric(Target.Value) Then Sheet1.txtVal.Text = CInt(Target.Value) + 1
End If
Application.EnableEvents = True
End Sub

Resources