Problem with Worksheet_change event

Caze982

Board Regular
Joined
May 10, 2015
Messages
77
Hi everyone

I have an Excel-sheet where i have some columns. For some of the columns i have lists for each cell in the column. The vba code below makes sure that i for some of the columns can have several items from the list within each cell and some free text. The problem is for the other columns where i dont have lists but just want to write some text, when i try delete some of the text and go out of the cell the text i have deleted comes right back. Does anyone know how to modify the code below so i for the columns the have cells with with no lists, can write some text, delete the things i want, and make sure that the text i have deleted does not come back?

Hope it makes sense.



Private Sub Worksheet_Change(ByVal Target As Range)

Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 7 Or 3 Or 4 Or 6 Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
When posting code, please click the </> icon and paste it into the popup window, it is much easier to read when it is formatted correctly.

The first Application.EnableEvents should be False, not True.

If Target.Column = 7 Or 3 Or 4 Or 6 Then
You need Target.Column = for each number, not just the first one. If Target.Column = 7 Or Target.Column = 3 Or Target.Column = 4 Or Target.Column = 6 Then
That may or may not fix the problem, those are just 2 things that I noticed without even reading the code in detail.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,254
Members
448,556
Latest member
peterhess2002

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