worksheet change event code problem

supermom28115

New Member
Joined
May 9, 2022
Messages
43
Office Version
  1. 365
Platform
  1. Windows
I am working with a worksheet where the user enters data and at several places they indicate whether the value in a cell will be the same value as the cell just entered. I was trying to write a worksheet change event code that would check for the target cell (row 20 of any column) value to be equal to yes and if so make the value in cell 21 (same column) equal to cell 19(same column). This is only my 4th or 5th set of code that I have written that has been productive in that something happened. I can not enter anything into my worksheet at all now. It is a new workbook and only has 5 sheets and this sheet has one column of labels and one column of data. It did have 6 columns of data, but stuff happened. Anyway, I tried to debug but it didn't step into the code even after i reset the code. I am sure I have done something wrong that is so simple but I have already spent 6 hours trying to find any reference to dealing with a target range that was an entire row. I found a few that discussed columns but the data and action needed were extremely different than my needs.
I am simply looking for a set of code that will recognize that the cell changed, check for yes (no means do nothing) and make the two cells values the same.
This set of code will be a workhorse in my spreadsheets due to so much of my data is very likely the same, but the data structure for each data type will be different so I will need to be able to repurpose the code in multiple places on the sheet, in the workbook, and in other workbooks.
This is my first post so if I haven't provided the right or enough information I apologize and do appreciate any help you can provide this new vba user.



VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changed As Range

 If Not Intersect(Target, Range("20:20")) Is Nothing Then
 
 Set changed = Target.Address
    If changed = "yes" Then
        changed.Offset(1, 0).Value = changed.Offset(-1, 0).Value
        
    
   Application.EnableEvents = False
   Target = newinput
   Application.EnableEvents = True
   End If
 End If
 
End Sub
 
You're welcome. :)

The follow-on is a bit confusing, but I understand that your issue is that you want several different Worksheet_Change codes.
You can only have one Worksheet_Change procedure for each worksheet, but you can do the same thing as if you could have multiple.

For example, suppose that you want to ..
- do as above if "yes' is entered in row 20
- clear column Q if anything in in the range L1:N7 is changed and
- put "abc" in cell A1 if anything starting with "A" is entered in cell D4

.. then one way (there are many ways to structure this) would be like this.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim cell As Range
  
  Application.EnableEvents = False
  Select Case True
    Case Not Intersect(Target, Rows(20)) Is Nothing
      For Each cell In Intersect(Target, Rows(20))
        If LCase(cell.Value) = "yes" Then cell.Offset(1, 0).Value = cell.Offset(-1, 0).Value
      Next cell
      
    Case Not Intersect(Target, Range("L1:N7")) Is Nothing
      Columns("Q").Clear
      
    Case Not Intersect(Target, Range("D4")) Is Nothing
      If Range("D4").Value Like "A*" Then Range("A1").Value = "abc"
      
  End Select
  Application.EnableEvents = True
End Sub
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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