VBA change event applied to one or multiple cells

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
How can I change this to apply to multiple cells at once? Right now this will change "A" when I enter something in "B", but what if I select multiple contiguous cells like B2:B10 and then I want A2:A10 to equal "IN".

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Target.Value <> "" Then Range("A" & Target.Row).Value = "IN"
    Application.EnableEvents = True
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
If you change B2:B10 at once (eg with CTRL+ENTER) then Target will hold a reference to that entire range so you could loop through Target.Cells to examine each individual cell. By the way, your code changes column A when any cell across the row (including A) is changed, not just column B, so perhaps you could also modify it to only loop through the Target cells in column B. An example might be something like this:

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

    Dim rngCell As Range
    Dim rngCheck As Range
    
    'only check cells in column B
    Set rngCheck = Intersect(Target, Columns(2))
    
    If Not rngCheck Is Nothing Then
    
        Application.EnableEvents = False
    
        For Each rngCell In rngCheck.Cells
            
            If Not IsEmpty(rngCell.Value2) Then
                
                'put "IN" in column A
                Cells(rngCell.Row, 1).Value = "IN"
            End If
    
        Next rngCell
    
        Application.EnableEvents = True
    End If
    
End Sub
 
Upvote 1
Solution
How about this?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Cel As Range
  Dim Ar As Range
  
  Application.EnableEvents = False
  For Each Ar In Target.Areas               'In case multiple areas have been changed (filters on)
    For Each Cel In Ar                      'Cells in each Area
      If Cel.Value <> "" And Cel.Column = 2 Then Range("A" & Cel.Row).Value = "IN"
    Next Cel
  Next Ar
  
  Application.EnableEvents = True
End Sub
 
Upvote 0
These are both perfect solutions. Thank you for your time.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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