How to disable button depending on blank cell - excel

I have a problem. How do I enable/disable a button depending on whether a cell is empty or not.
In excel sheet named "Sheet1" I have 1 button called "CommandButton1" assigned to a macro named "If_empty".
What I need to do is if column "F2" is empty I want to disable the button but if column "F2" has any values in it for the button to be enabled.
How do I achieve this?
Help is greatly appreciated

As #Slai and Doug suggested, use the code below (add it to your Worksheet_Change event).
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
Dim i, j As Integer
Set WatchRange = Range("F2")
Set IntersectRange = Intersect(Target, WatchRange)
If Not IntersectRange Is Nothing Then
If Target.Value = "" Then
CommandButton1.Enabled = False
Else
CommandButton1.Enabled = True
End If
End If
End Sub

Related

Enabling the Command Button when 4 Cells are not Empty

I have 4 Cells (S11:T12) and a Command Button 1, what I want is, until all 4 cells are populated the command button should be disabled (Which I can do from the properties tab) and once all 4 cells are filled with number, the command button should be enabled and once the data from these cells are deleted, the command button should be disabled again.
Under which event should I write the code?
I tried this, but it does not work.
Private Sub Workbook_Open(Cancel As Boolean)
If Sheets("WorkArea").Range("S11:T12") = "" Then
Sheets("WorkArea").CommandButton1.Enabled = False
Else
Sheets("WorkArea").CommandButton1.Enabled = True
End If
End Sub
Use the WorkSheet_Change event handler to handle the change in cells, and you can use the CountBlank worksheet function to determine if a range is empty.
Private Sub Worksheet_Change(ByVal Target As Range)
If WorksheetFunction.CountBlank(Range("S11:T12")) = 4 Then
Sheets("WorkArea").CommandButton1.Enabled = False
Else
Sheets("WorkArea").CommandButton1.Enabled = True
End If
End Sub
Worksheet_Change
CountBlank
According to your question however, you actually want:
Private Sub Worksheet_Change(ByVal Target As Range)
If WorksheetFunction.CountBlank(Range("S11:T12")) = 0 Then
Sheets("WorkArea").CommandButton1.Enabled = True
Else
Sheets("WorkArea").CommandButton1.Enabled = False
End If
End Sub
A Worksheet Change
This solution will not work if the critical range contains formulas.
To count the number of cells that are not empty you can use the WorksheetFunction.CountA method.
Usually you don't want this code to run when there are changes outside of the range, so you will restrict the code to the range with the Application.Intersect method. You don't have to enable or disable the command button on each change since obviously the code will run on each change.
Since this is all happening in worksheet "WorkArea", there is no need to refer to it by its name i.e. you can safely use Range(rngAddress) and CommandButton1 instead of ThisWorkbook.Worksheets("WorkArea").Range(rngAddress) and ThisWorkbook.Worksheets("WorkArea").CommandButton1 respectively.
The Code
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const rngAddress As String = "S11:T12"
Dim rng As Range
Set rng = Range(rngAddress)
If Not Intersect(Target, rng) Is Nothing Then
If WorksheetFunction.CountA(rng) = rng.Cells.Count Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End If
End Sub

Is there a way to make a multi-step worksheet change macro work?

I'm building out a simple proof of concept inventory management system. To simplify the steps, I'm using excel vba's event change functionality with private subscripts on the sheets. The process flow should look like this: work order number is scanned into cell I2, triggering cell L4 to be selected. After something is entered into range L4:L13, it will then move to the next column over. Once cell O4 is changed, it will kick off another macro to update the database of entries. The problem lies in the fact that when cell I2 is changed, nothing happens.
I've tried creating multiple change event private subscripts, but that didn't work.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkOrder As Range
Dim MoveTo As Range
Dim ChangeStatus As Range
Dim ChangeComplete As Range
Set WorkOrder = Range("I2")
Set MoveTo = Range("L4:L13")
Set ChangeStatus = Range("M4:M13")
Set ChangeComplete = Range("O4")
If Not Application.Intersect(WorkOrder, Range(Target.Address)) _
Is Nothing Then
Range("L4").Select
ElseIf Not Application.Intersect(MoveTo, Range(Target.Address)) Is Nothing Then
Range("M4").Select
ElseIf Not Application.Intersect(ChangeComplete, Range(Target.Address)) Is Nothing Then
Call Module1.EditWorkOrderStatus
End If
Range("i2").Select
End Sub
I expect the macro in module1 to run and clear out all of the changed cells, but nothing is occurring. I'm receiving no errors on my code at this time.
UPDATED CODE:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkOrder As Range
Dim MoveTo As Range
Dim ChangeStatus As Range
Dim ChangeComplete As Range
Set WorkOrder = Range("I2")
Set MoveTo = Range("L4:L13")
Set ChangeStatus = Range("M4:M13")
Set ChangeComplete = Range("O4")
If Not Application.Intersect(WorkOrder, Target) _
Is Nothing Then
If WorkOrder = "" Then
WorkOrder.Select
Else
Range("L4").Select
End If
ElseIf Not Application.Intersect(MoveTo, Target) Is Nothing Then
Range("M4").Select
ElseIf Not Application.Intersect(ChangeComplete, Target) Is Nothing Then
If ChangeComplete = "" Then
Else
Call Module1.EditWorkOrderStatus
Range("o4").ClearContents
Range("i2").Select
End If
End If
End Sub
you can try:
If Not Application.Intersect(Target, Me.Range("I2")) Is Nothing Then

Excel leave existing date in cell (calendar VBA)

I am using VBA calendar to input dates in my workbook. I have added code to my worksheet to call calendar on double click of cell. So basically when I double click cell in range I get calendar form, where I pick up the date and then it is inserted in double clicked cell. The only problem is that after closing my calendar form my cell gets selected and I see formula inside my cell. How I can make it close my calendar form without selecting cell after it?
I have used this calendar:
https://trevoreyre.com/portfolio/excel-datepicker/
Code to call calendar form by double clicking cell in range:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim xRg As Object
If Not Intersect(Target, Range("E7:F187")) Is Nothing Then dateVariable = CalendarForm.GetDate
if datevariable=0 then
exit sub
else
For Each xRg In Selection.Cells
xRg.Value = dateVariable
Next xRg
End if
'datevariable=0
End Sub
try, this. It seems that you can select another cell in the code.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rng As Range
Set rng = Target.Offset(1, 0)
If Not Intersect(Target, Range("E7:F187")) Is Nothing Then datevariable = CalendarForm.GetDate
If datevariable = 0 Then
rng.Select
Exit Sub
Else
Target.value = datevariable
rng.Select
End If
End Sub

EXCEL VBA Dynamic Sheet Name according to a cell value - Not working when formula in the cell

Hej,
I've created a small VBA code to dynamically rename a worksheet.
It's working perfectly when the cell is just manually typed.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C9")) Is Nothing Then
ActiveSheet.Name = ActiveSheet.Range("C9")
End If
End Sub
But then as soon as I will put a formula concatenating 2 cells values within C9 cell it will not update it automatically.
To make it work I need to enter the cell and type ENTER again and it works.
I have to do same manipulation each time I change a value in on of the 2 cell concatenated.
THANKS for your help guys
You need to capture a different event:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
ActiveSheet.Name = ActiveSheet.Range("C9")
Application.EnableEvents = True
End Sub
NOTE:
We disable events during the name change in case the worksheet contains a formula referencing the tab-name.
this should work:
replace
ActiveSheet.Name = ActiveSheet.Range("C9")
by
ActiveSheet.Name = ActiveSheet.Range("C9").Value
This is an alternate answer if someone still wants to execute this on worksheet change event
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim formulacell As Range
Set formulacell = Range("C9")
Set formulacell = Application.Union(formulacell, formulacell.Precedents)
If Not Intersect(Target, formulacell) Is Nothing Then
ActiveSheet.Name = ActiveSheet.Range("C9").Value
End If
Application.EnableEvents = True
End Sub

Excel VBA: Worksheet Change cell value to different sheet

I've been working on this for some time now and have hit a real stumbling block.
I have a set of values that are available via a validated dropdown menu in Sheet 3, Column D. Once selected this value currently displays in a different sheet (Sheet 7) using excel function ='Sheet 3'!D4 and so on, and I have some code that reads this and performs an IF statement to produce a value in another cell.
My problem is the code is dependant on reading the value and not the formula.
I currently have a worksheet change command for a separate function I want to run, is there a way for this to run a second function and call any changes from sheet 3 column D into sheet 8 column D and then run my other change function?
Sheet 7 Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Intersect(Target, Range("D2:D102")) Is Nothing Then Exit Sub
Application.EnableEvents = False
On Error GoTo Finalize
For Each c In Target.Cells
Select Case c.Column
Case 4
Call Print_Quality(c)
End Select
Next c
Finalize:
Application.EnableEvents = True
End Sub
Sheet 7 Module:
Sub Print_Quality(c As Range)
Dim PrintQuality As String
Dim PrintSpeed As String
PrintQuality = c.Value
If PrintQuality = "A Quality 1" Then PrintSpeed = "100"
c.Offset(0, 5).Value = PrintSpeed
End Sub
I've been trying this route but to no avail:
Worksheet 3 code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("D4:D104")) Is Nothing Then Exit Sub
Application.EnableEvents = False
On Error GoTo Finalize
UpdateVal
Finalize:
Application.EnableEvent = True
End Sub
Module:
Sub UpdateVal()
Worksheets("Sheet 7").Range("D2").Value = Worksheets("Sheet 3").Range("D4").Value
End Sub
Many thanks
Sods law, I've managed to fix this an hour after my desperation post.
I completed another worksheet change from the sheet it was calling from (Sheet 3)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("D4:D104")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Call UpdateVal
End If
End Sub
then added this function into the module
Sub UpdateVal()
Sheet8.Cells(2, 4).Value = Sheet3.Cells(4, 4)
End Sub
this now references the value of the dropdowns in sheet 8 and allows other functionality to continue using the cell value
Have you tried stepping through your code to see where it is having an issue? It not, I would suggest putting a break at the beginning of each module, and then use F8 to step through. This will confirm it is running as it should.
You should also be fully-qualifying your references to worksheets. While presumably the sheet references should carry through given that they are in worksheet modules, there is the chance of failure. You can simply assign a variable to hold the worksheet like so:
Dim wb as Workbook
Dim ws as Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("YourSheetName")
Additionally, your worksheet 3 code:
EnableEvent = True
Should be:
EnableEvents = True

Resources