Delete Cell Content at the end of code.

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
Thanks for looking at my post. I am trying to figure out how delete the text typed in cell AE30 at the end of the code below.

Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("AE30")


If target.Value = "unprotect" Then
Call up_all_sheets
End If
If target.Value = "protect" Then
Call p_all_sheets
End If
If target.Value = "add" Then
Call UserForm7.Show


End If


End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
range("EA30")="" or .ClearContents or VBNullString
 
Upvote 0
You do not set "Target" to anything. "Target" is simply the cell that was updated that triggered this code to run.
So, it is already pre-defined and pre-set at the time the code runs.

If you want clear cell AE30 at the end, just add the line:
Code:
Range("AE30").ClearContents
 
Upvote 0
I believe I have the code in the wrong location . Where should it go?

Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("AE30")


If target.Value = "unprotect" Then
Call up_all_sheets
End If
If target.Value = "protect" Then
Call p_all_sheets
End If
If target.Value = "add" Then
Call UserForm7.Show


Range("AE30").ClearContents


End If


End Sub
 
Upvote 0
You placed it within an IF statement, namely:
Code:
If target.Value = "add" Then
Call UserForm7.Show


Range("AE30").ClearContents


End If
That means that line will only run if they value updated is "add".
If you want it to run no matter what, place it under the last "End If" statement.

By the way, do you want this code to only run when a particular cell is updated to "unprotect", "protect", or "add"?
Because right now, the way you have it written, this code will run when ANY cell on your sheet is updated to these values.
 
Upvote 0
Oh wow I thought it only worked if the text was enterd in cell AE30. Can you help with that? Below is the entire code I have in the sheet.

Code:
Option Explicit
Code:
Private Sub Worksheet_SelectionChange(ByVal target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(target, Range("AE29")) Is Nothing Then
            Call PrintUserformShow
        End If
    End If
End Sub
Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("AE30")


If target.Value = "unprotect" Then
Call up_all_sheets
End If
If target.Value = "protect" Then
Call p_all_sheets
End If
If target.Value = "add" Then
Call UserForm7.Show
End If
End Sub
 
Upvote 0
Oh wow I thought it only worked if the text was enterd in cell AE30. Can you help with that? Below is the entire code I have in the sheet.
I thought that was maybe what you were trying to do. You need to check to see if the cell just updated (Target) is cell AE30. Here is one way to accomplish what you want:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

'   Check to see if cell AE30 was updated
    If Target.Address = Range("AE30").Address Then

'       Determine what to do
        Select Case Target.Value
            Case "unprotect"
                Call up_all_sheets
            Case "protect"
                Call p_all_sheets
            Case "add"
                Call UserForm7.Show
        End Select

'       Clear target cell (AE30)
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
    
    End If

End Sub
I also used a Case statement instead of a series of IF/THEN statements. I think they are easier to follow.
See: https://www.techonthenet.com/excel/formulas/case.php
 
Last edited:
Upvote 0
Perfect. Thanks for the help. That would have caused much pains in the future if you wouldnt have told me.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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