I found the code below for Worksheet_Change. But my H29 is formula =SUM.
How do I change this to Worksheet_Calculate so that the below macro will run?
Basically what I want is if H29 is calculated as being equal to 20, show the button, else hide it.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target = Range("H29") Then
If Target.Value = "20" Then
Me.CommandButton1.Visible = True
Else
Me.CommandButton1.Visible = False
End If
Application.ScreenUpdating = True
End If
Depending on the button type, the code to instruct the button will be different. Try this.
Private Sub Worksheet_Calculate()
'ActiveX button
If Range("H29").Value = 20 Then
Sheets("Sheet1").CommandButton2.Visible = False
Else
Sheets("Sheet1").CommandButton2.Visible = True
End If
'Forms Button
If Range("H29").Value = 20 Then
Sheets("Sheet1").Shapes("CommandButton1").Visible = msoFalse
Else
Sheets("Sheet1").Shapes("CommandButton1").Visible = msoTrue
End If
End Sub
Sorry asked too soon. Resolved
Private Sub Worksheet_Calculate()
'ActiveX button
Application.ScreenUpdating = False
If Range("H29").Value = 20 Then
Me.CommandButton1.Visible = True
Else
Me.CommandButton1.Visible = False
End If
Application.ScreenUpdating = True
End Sub
Related
I have a spreadsheet where I have adapted two pieces of VBA code to perform two different double click event actions.
The 1st piece of code enters a "✓" in a specific range of cells when double clicked and removes it when double clicked again:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("H2:H600,M2:V600")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
End Sub
The 2nd piece of code enters a date/time stamp in a range of cells when double clicked:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Coded by SunnyKow - 16/09/2016
Application.EnableEvents = False
On Error GoTo ErrorRoutine
'You can change the range here
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
'Update only if cell is empty
If Target = "" Then
Target = Now
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
Because you cannot have two double click events in single worksheet (as separate VBA code), how do I merge these two pieces of VBA so that it is a single piece of code with two distinct actions based on the cell range selected. Would appreciate any help to resolve this.
It looks like an if statement will do the trick
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
On Error GoTo ErrorRoutine
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
'Update only if cell is empty
If Target = "" Then
Target = Now
End If
Cancel = True
ElseIf Not Intersect(Target, Range("H2:H600,M2:V600")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
On Error GoTo ErrorRoutine
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
If Target = "" Then
Target = Now
End If
Cancel = True
ElseIf Not Intersect(Target, Range("M2:V600")) Is Nothing Then
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
ElseIf Not Intersect(Target, Range("H2:H600")) Is Nothing Then
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
Target.Offset(0, 18) = Now
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
This code shows ListBox1 when cell A2 is clicked, hides it when A2 is clicked a second time or ListBox1 is no longer selected. The selections are output to A2.
The problem is that after A2 is clicked once to open and once to close, you must click another cell before clicking on A2 again to create the perceived toggle effect of the ListBox.
I have tried repeating Application.EnableEvents = False [A3].Select Application.EnableEvents = True just before End If however when trying to select any other cell on the sheet, only A3 is selected.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet.ListBox1
If Target(1).Address = "$A$2" And .Visible = False Then
.Visible = True
Application.EnableEvents = False
[A3].Select
Application.EnableEvents = True
Else
.Visible = False
For I = 0 To .ListCount - 1
If .Selected(I) Then txt = txt & ", " & .List(I)
Next
[A2] = Mid(txt, 2) 'remove first comma and output to A2 cell
End If
End With
End Sub
Question Updated to include the answer provided, while incorporating part of the code from above to output the ListBox selections to cell A2. The new problem is that the selections already made, continue to multiply in cell A2 everytime the ListBox is closed, rather than only adding new selections.
Option Explicit
Dim SelectCell As Boolean
Dim i As Long
Dim txt As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet.ListBox1
If Target(1).Address = "$A$2" And .Visible = False Then
.Visible = True
Application.EnableEvents = False
[A3].Select
Application.EnableEvents = True
SelectCell = True
Else
.Visible = False
For i = 0 To .ListCount - 1
If .Selected(i) Then txt = txt & ", " & .List(i)
Next
[A2] = Mid(txt, 2) 'remove first comma and output to A2 cell
If SelectCell = True Then
Application.EnableEvents = False
[A3].Select
Application.EnableEvents = True
SelectCell = False
End If
End If
End With
End Sub
Yes this works however when you click any other cell in the sheet it keeps selecting cell A3. I will clarify question. Thanks – aye cee 1 min ago
Is this what you are trying?
Option Explicit
Dim SelectCell As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With ActiveSheet.ListBox1
If Target(1).Address = "$A$2" And .Visible = False Then
.Visible = True
Application.EnableEvents = False
[A3].Select
Application.EnableEvents = True
SelectCell = True
Else
.Visible = False
If SelectCell = True Then
Application.EnableEvents = False
[A3].Select
Application.EnableEvents = True
SelectCell = False
End If
End If
End With
End Sub
The Listbox should open and close endless times by clicking A2 or close by clicking outside, while also allowing selection of other cells on the sheet. Yes I included all code. On your end can you repeatedly click A2 to open close without selecting any other cell? – aye cee 2 mins ago
It does exactly that. See this
Alternate solution
Use Worksheet_BeforeDoubleClick with Worksheet_SelectionChange. See this. Now the ListBox1 will show everytime you double click on A2 and hide when any other cell is selected.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target(1).Address = "$A$2" And ListBox1.Visible = False Then
ListBox1.Visible = True
Cancel = True
Else
ListBox1.Visible = False
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ListBox1.Visible = True Then ListBox1.Visible = False
End Sub
I see that you already have a solution. However, for what it's worth, here is another approach. I think it allows you more flexibility to control what the ListBox shows when it is displayed.
Option Explicit
Private Const Trigger As String = "A2"
Private Sub ListBox1_LostFocus()
' 238
Dim Txt As String
Dim i As Integer
With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
If Len(Txt) Then Txt = Txt & ","
Txt = Txt & .List(i)
End If
Next i
Range(Trigger).Value = Txt
End With
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' 238
With ListBox1
If Target.Address(0, 0) = Trigger Then
.Visible = True
.ListFillRange = "P2:P9"
Else
.Visible = False
End If
End With
End Sub
In the above setup the ListBox is displayed when the user clicks on A2. He can then make a selection and transfer it to A2 by clicking anywhere outside the ListBox. However, if he clicks on A2 he can see both his selection and its transscription to A2, and go back to the ListBox to change the selection.
what is wrong with this code?
Private Sub Worksheet_Change(ByVal Target As Range)
If IsEmpty(Sheet5.Range("A26").Value) = True Then
Sheet5.Rows("26:27").EntireRow.Hidden = True
ElseIf IsEmpty(Sheet5.Range("A26").Value) = False Then
Sheet5.Rows("26:27").EntireRow.Hidden = False
End If
End Sub
Can you please try it?
If IsEmpty(Worksheets("Sheet5").Range("A26").Value) = True Then
Worksheets("Sheet5").Rows("26:27").EntireRow.Hidden = True
ElseIf IsEmpty(Worksheets("Sheet5").Range("A26").Value) = False Then
Worksheets("Sheet5").Rows("26:27").EntireRow.Hidden = False
End If
Useful link:
https://learn.microsoft.com/en-us/office/vba/api/excel.range(object)
I found an answer!
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
If Sheet5.Range("A26").Value = "" Then
Sheet5.Rows("26:27").EntireRow.Hidden = True
Else
Sheet5.Rows("26:27").EntireRow.Hidden = False
End If
End Sub
Application.EnableEvents = True
I had to change
Private Sub Worksheet_Change(ByVal Target As Range)
to
Private Sub Worksheet_Calculate()
I needed to use calculate because the "" is created by formula therefore a calculation.
I'm currently working on a small project in VBA, therefore I have created an UserForm with two checkboxes and I wrote 2 subs proper to these checkboxes to make sure that only one of the checkboxes can be checked. Here is the code of the 2 checkboxes :
Private Sub CheckBox2_Click()
If CheckBox2.Value = True And CheckBox3.Value = True Then
CheckBox3.Value = False
TextBox8.Enabled = True
TextBox8.BackColor = RGB(255, 255, 255)
Else
CheckBox3.Value = False
CheckBox2.Value = True
End If
End Sub
Private Sub CheckBox3_Click()
If CheckBox3.Value = True And CheckBox2.Value = True Then
CheckBox2.Value = False
TextBox8.Enabled = False
TextBox8.BackColor = RGB(205, 205, 205)
Else
CheckBox2.Value = False
CheckBox3.Value = True
TextBox8.Enabled = False
End If
End Sub
As you have seen, the subs also enable/disables a TextBox field considering the checkbox checked.
This code perfectly works when I try to launch the Userform from the main menu with the Run button : il you check a checkbox, the other one will be unchecked.
I also create on the first sheet of my Excel file, a button that launches the userform, so the user won't have to activate the developer's menu to launch it. Here is the sub attached to this button :
Private Sub lance_interface()
UF.Show
End Sub
When I click on this button, the userform appears but once a checkbox is checked, there's no way to check the other checkbox in order to uncheck the first one : the first one keeps being checked.
I hope that my problem is clearly explained, thanks in advance for your help and sorry for my english mstakes.
You need to disable events in your userform. In your case that might look like that
Option Explicit
Dim eventOn As Boolean
Private Sub CheckBox2_Click()
If eventOn Then
eventOn = False
If CheckBox2.Value = True And CheckBox3.Value = True Then
CheckBox3.Value = False
TextBox8.Enabled = True
TextBox8.BackColor = RGB(255, 255, 255)
Else
CheckBox3.Value = False
CheckBox2.Value = True
End If
eventOn = True
End If
End Sub
Private Sub CheckBox3_Click()
If eventOn Then
eventOn = False
If CheckBox3.Value = True And CheckBox2.Value = True Then
CheckBox2.Value = False
TextBox8.Enabled = False
TextBox8.BackColor = RGB(205, 205, 205)
Else
CheckBox2.Value = False
CheckBox3.Value = True
TextBox8.Enabled = False
End If
eventOn = True
End If
End Sub
Private Sub UserForm_Initialize()
eventOn = True
End Sub
Fo further reading look at ChipPearson article on Surpress Change in Forms or disabling-events-in-userforms
I have problem. How to enable/disable a button depending on the cell value.
In excel sheet i have 2 buttons.
What i need to do is.
If a column "L" having data
The one button enable
else
"BQ" is having data
another button needs to be enabled.
Other button will be disabled.
How to achieve it.
Please help ....
Thanks in advance
I was waiting for your reply as to what should happen if both are filled up. I have added that option in the code. Amend it to suit your needs.
Try this
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
CommandButton1.Enabled = False: CommandButton2.Enabled = False
'~~> If both cols are filled up
If Application.WorksheetFunction.CountA(Columns(12)) > 0 And _
Application.WorksheetFunction.CountA(Columns(69)) > 0 Then
'~~> Change the message as applicable
MsgBox "Both Columns Cannot have data", vbInformation, "Error"
Else
'~~> If Col L is filled up
If Application.WorksheetFunction.CountA(Columns(12)) > 0 _
Then CommandButton1.Enabled = True
'~~> If Col BQ is filled up
If Application.WorksheetFunction.CountA(Columns(69)) > 0 _
Then CommandButton2.Enabled = True
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
Replace CommandButton1 and CommandButton2 with whatever the names of your buttons are.
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.WorksheetFunction.CountA(Range("L:L")) > 0 Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
If Application.WorksheetFunction.CountA(Range("BQ:BQ")) > 0 Then
CommandButton1.Enabled = False
CommandButton2.Enabled = True
Else
CommandButton1.Enabled = True
CommandButton2.Enabled = False
End If
End Sub