problem code works in worksheet selection event but not in change event

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello

i have this code works in selection event but not in change event and if is possible amending the code or getting alternative the code select cells or change depend on range ("d11:m22") and show in j23 so i want only this range applies when cell is actived not out side this range
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$j$23" Then
Target.Value = ""
Else
[j23].MergeArea = ActiveCell
End If
End Sub


my data
active cell.xlsm
DEFGHIJKLM
1112345678910
1211121314151617181920
1321222324252627282930
1431323334353637383940
1541424344454647484950
16140148181182183184185186187188
17189190191192193194195196197198
18199200201202203204205206207208
19209210211212213214215216217218
20219220221222223224225226222228
21229230
22numbers
23
sheet1
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
try this slight modification:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$j$23" Then
Target.Value = ""
Else
Application.EnableEvents = False
[j23].MergeArea = ActiveCell
Application.EnableEvents = True
End If
End Sub
what is happening your code triggers when a change is made, this makes a change in j23 which triggers the worksheet change event again which immediately clears out the value in j23!!
 
Upvote 0
thanks offthelip it works i have this code in selection change i try more to make target.value="" but it clear range "d11:m22" could you tell me how make j23 ="" if is not within range "d2:m22" just curiosity you've solved my problem
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim cells As Range
    Set cells = ActiveSheet.Range("d11:m22")
    If Not (Intersect(Target, cells) Is Nothing) Then
    
       ActiveSheet.Range("j23").MergeArea = Target.Value
      
    End If
End Sub
 
Upvote 0
If I have understood what you want all you need is:
VBA Code:
    If Not (Intersect(Target, cells) Is Nothing) Then
      ActiveSheet.Range("j23").MergeArea = Target.Value
     else
      ActiveSheet.Range("j23").MergeArea = ""
    End If
 
Upvote 0
It works fine for me, what cell or cells did you have selected and what was in them? Note this was your code!!
it might be worth wrapping the application enable events falst / true around it to check it isn't trigger the worksheet change event e.g.
VBA Code:
If Not (Intersect(Target, cells) Is Nothing) Then
     Application.EnableEvents = False
     ActiveSheet.Range("j23").MergeArea = Target.Value
     else
      Application.EnableEvents = False
     ActiveSheet.Range("j23").MergeArea = ""
    End If
    Application.EnableEvents = True
Note I have deliberately put the application enable event true outside the if statement!!
 
Upvote 0
it works only selection event not change any way the idea when show only values in this range d11:m22 but if i select out the specific range it show like this i don't want
1.xlsm
GHIJ
22numbersnumbers
23
sheet1
 
Upvote 0
let me make clear more the values are existed from d11: m22 when i choose from theses cell is ok the code works but if i select outside this range then also show for instance if i have some cells contain text or value n2,or p22 any cell locates outside this range d11:m22 it doesn't supposes show in j23 show only what locates within this range d11:m22
 
Upvote 0
I think I have understood what you want, try these two subroutines:;
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Application.EnableEvents = False
    If Not (Intersect(Target, Range("d11:m22")) Is Nothing) Then
      ActiveSheet.Range("j23").MergeArea = Target.Value
      Else
      ActiveSheet.Range("j23").MergeArea = ""
    End If
    Application.EnableEvents = True
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not (Intersect(Target, Range("d11:m22")) Is Nothing) Then
[j23].MergeArea = ActiveCell
Else
ActiveSheet.Range("j23").MergeArea = ""
End If
Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,783
Members
449,188
Latest member
Hoffk036

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