Clear TextBox Contents with Worksheet_Change Event

jmitchells5w

New Member
Joined
Jun 14, 2007
Messages
35
I have a Worksheet_Change event set up so that when a value is entered/selected into cell C3 several textboxes update with values. These values are a result of using the linkedcell property. The problem is that when I delete the contents of C3 the Worksheet_Change event does not trigger and the values previously in the text boxes stay. I would like these to clear out. Here is the code I am currently using:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

If Target.Address = "$C$3" Then
Call AssignLinkedCells
End If

End Sub

Sub AssignLinkedCells()
'
Addr = "CompanyInfo!" & Range("D37").Value
Addr2 = "CompanyInfo!" & Range("D72").Value
Addr3 = "CompanyInfo!" & Range("C11").Value
Addr4 = "CompanyInfo!" & Range("C12").Value
Addr5 = "CompanyInfo!" & Range("D58").Value
TextBox1.LinkedCell = Addr
TextBox2.LinkedCell = Addr2
TextBox3.LinkedCell = Addr3
TextBox4.LinkedCell = Addr4
TextBox5.LinkedCell = Addr5

End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Substitute the Worksheet_Change event code you currently have with this, and see if it does what you want.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$C$3" Then Exit Sub
If IsEmpty(Target) Then
Dim i%
For i = 1 To 5
ActiveSheet.OLEObjects(i).Object.Text = ""
Next i
Else
Call AssignLinkedCells
End If
End Sub
 
Upvote 0
No, it does not solve the problem. When I select C3 and hit delete the text boxes do not update. I have been reading other posts and should note that C3 is actually 11 cells merged together (C3 through M3). When I unmerge the cells then the text boxes do update as I want them to however because of the layout of the spreadsheet I really need to keep those cells merged.
 
Upvote 0
Gotta love those merged cells.

See if this does what you want.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$3" And IsEmpty(Target) = False Then
Call AssignLinkedCells
Else
If Intersect(Target, Range("C3:M3")) Is Nothing Then Exit Sub
Dim i%
For i = 1 To 5
ActiveSheet.OLEObjects(i).Object.Text = ""
Next i
End If
End Sub
 
Upvote 0
I thought that worked but it but causes a problem. If the Textboxes were populated before deleting C3 then that means the linkedcell property of those textboxes hold a value. The code above clears the text boxes which then affect the cells they are linked to. I am working on the code now to clear the linkedcell property before clearing the textboxes. That should solve my problem. Thank you, Tom!
 
Last edited:
Upvote 0
OK, here is the code I'm using and it seems to work fine. Thanks again, Tom!

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$3" And IsEmpty(Target) = False Then
Call AssignLinkedCells
Else
If Intersect(Target, Range("C3:M3")) Is Nothing Then Exit Sub
Dim i%
For i = 1 To 5
ActiveSheet.OLEObjects(i).LinkedCell = ""
ActiveSheet.OLEObjects(i).Object.Text = ""
Next i
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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