Show/Hide a textbox based on the value of a cell

Matteo85

New Member
Joined
Feb 26, 2008
Messages
7
Office Version
  1. 2021
Platform
  1. Windows
I have a table in my worksheet that populates with a spill of the main table if any project in the main table is unfinished. I need to show a textbox if there are no unfinished projects to overlay the area of the spill array and hide it if there is any value in the spill array.

How could this be done? I've tried the following:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row = 14 And Target.Column = 2 Then ' The spill array's upper left cell is B14
        If Target.Value = "" Then
            ActiveSheet.Shapes("txtbx_NoOpenProjects").Visible = True
        Else
            ActiveSheet.Shapes("txtbx_NoOpenProjects").Visible = False
        End If
    End If
End Sub

but I can't get the textbx_NoOpenProjects to appear/disappear. The spill array is working properly and is based on the main table in another worksheet.

Any help?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I am guessing that B14 has a formula. If that is the case then your If condition will never be True because Target will never be B14. The Worksheet_Change condition is not triggered by a change to the result of formula, only when the actual content of a cell changes.

Try this instead:
VBA Code:
Private Sub Worksheet_Calculate()

        If Range("B14").Value = "" Then
            ActiveSheet.Shapes("txtbx_NoOpenProjects").Visible = True
        Else
            ActiveSheet.Shapes("txtbx_NoOpenProjects").Visible = False
        End If

End Sub
 
Upvote 1
Solution
VBA Code:
Private Sub Worksheet_Calculate()
    Dim spillRange As Range
    Set spillRange = Range("B14").CurrentRegion ' Assuming the spill array starts from B14
    
    If WorksheetFunction.CountA(spillRange) = 0 Then
        ActiveSheet.Shapes("txtbx_NoOpenProjects").Visible = True
    Else
        ActiveSheet.Shapes("txtbx_NoOpenProjects").Visible = False
    End If
End Sub
 
Upvote 1
Neither of the solutions above seems to be working. Is it possibly due to the action taking place on another worksheet?

I tried moving the logic into another cell that checks to see if the spill array has a value and returns "0" or "1" depending. That doesn't seem to work either.

Code in A14:
VBA Code:
'=IF(B14="","0","1")
Any ideas?
 
Upvote 0
I got the following to work. Thanks for pointing me in the right direction.

VBA Code:
Private Sub Worksheet_Calculate()
    Dim wk As Worksheet
    Set wk = Worksheets("Sheet1")
    If wk.Range("B14").Value = "" Then
            wk.Shapes("txtbx_NoOpenProjects").Visible = True
        Else
            wk.Shapes("txtbx_NoOpenProjects").Visible = False
    End If
End Sub
 
Upvote 0
I got the following to work.
If the formula in B14 is on Sheet1, and the shape is on Sheet1 and the Worksheet_Calculate code is in the Sheet1 module, then I don't see how the post 2 code works in any way differently to the code in post 5. Can you explain the difference?

BTW these 5 lines of code ..
VBA Code:
If wk.Range("B14").Value = "" Then
        wk.Shapes("txtbx_NoOpenProjects").Visible = True
    Else
        wk.Shapes("txtbx_NoOpenProjects").Visible = False
End If

.. could be replaced with this single line
VBA Code:
wk.Shapes("txtbx_NoOpenProjects").Visible = wk.Range("B14").Value = ""
 
Upvote 0
I got the following to work. Thanks for pointing me in the right direction.

VBA Code:
Private Sub Worksheet_Calculate()
    Dim wk As Worksheet
    Set wk = Worksheets("Sheet1")
    If wk.Range("B14").Value = "" Then
            wk.Shapes("txtbx_NoOpenProjects").Visible = True
        Else
            wk.Shapes("txtbx_NoOpenProjects").Visible = False
    End If
End Sub
The original question points to the active sheet, and the provided solution in post #2 has been prepared to fulfill that requirement. Therefore, post #2 is the initial correct answer of the question and I switched it as the solution post as @Peter_SSs also mentioned above.
 
Upvote 0
Hi i have a very similar question and can't find a solution. My goal is to show Chart Ratio_All and hide Ratio_Months when the value in cell B1 (from the formula) is equal to 1 and revers it when its 2. Below my VBA. Everything is in Shee1

Private Sub Worksheet_Calculate()
Dim wk As Worksheet
Set wk = Worksheets("Sheet1")
If wk.Range("B1").Value = 1 Then

ActiveSheet.Shapes("Ratio_All").Visible = True
ActiveSheet.Shapes("Ratio_Months").Visible = False

Else

ActiveSheet.Shapes("Ratio_All").Visible = False
ActiveSheet.Shapes("Ratio_Months").Visible = True

End If
End With

End Sub

Any idea what i do wrong ?
 
Upvote 0
Hi i have a very similar question and can't find a solution. My goal is to show Chart Ratio_All and hide Ratio_Months when the value in cell B1 (from the formula) is equal to 1 and revers it when its 2. Below my VBA. Everything is in Shee1

Private Sub Worksheet_Calculate()
Dim wk As Worksheet
Set wk = Worksheets("Sheet1")
If wk.Range("B1").Value = 1 Then

ActiveSheet.Shapes("Ratio_All").Visible = True
ActiveSheet.Shapes("Ratio_Months").Visible = False

Else

ActiveSheet.Shapes("Ratio_All").Visible = False
ActiveSheet.Shapes("Ratio_Months").Visible = True

End If
End With

End Sub

Any idea what i do wrong ?
found the solution

VBA Code:
Private Sub Worksheet_Calculate()
Dim wk As Worksheet
Set wk = Worksheets("Sheet1")
  If wk.Range("B1").Value = 1 Then
 

  wk.Shapes("Ratio_All").Visible = True
  wk.Shapes("Ratio_Months").Visible = False
 
Else

  wk.Shapes("Ratio_All").Visible = False
  wk.Shapes("Ratio_Months").Visible = True
 
  End If

End Sub
 
Last edited by a moderator:
Upvote 0
@michalina
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in post 9 for you this time. Compare that code to your code in post 8 where I have not added the tags. 😊
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top