Counting cell changes in range of cells

pinchy1964

New Member
Joined
Aug 15, 2007
Messages
9
Hi all,

I'm new to VB so may be asking a daft question but hey, if I don't ask, I won't know :)

I am trying to add counters to a simple spreadsheet where each time a cell is modified, the cell next to it will increment.

I can do it for a single row but would like a simple way of applying to every row in a range without having to have a huge number of lines to apply the count to each row eg for a single row I am using -

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$1" Then [B1].Value = [B1].Value + 1

End Sub

But would like to apply to all rows up to row 100 for example -

Code:
If Target.Address = "$A$100" Then [B100].Value = [B100].Value + 1

In this example, how can I apply this count without having 100 lines of code (one for each row) - Hope I'm making sense

Thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi and welcome to the board!! :)

Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
    Cells(Target.Row, 2) = Cells(Target.Row, 2) + 1
End If
End Sub
 
Upvote 0
Hi pinchy1964
Welcome to the board

But would like to apply to all rows up to row 100

If you just want it up to row 100 (and not below), also:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then _
    Cells(Target.Row, "B") = Cells(Target.Row, "B") + 1
End Sub

Hope this helps
PGC
 
Upvote 0
pinchy1964

Welcome to the MrExcel board!

Is it possible that multiple cells could be changed at once? If so, code modification would be needed. Maybe something like:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)
    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range
    
    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">If</SPAN> Intersect(Target, Range("A1:A100")).Address = Target.Address <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Target
            c.Offset(, 1).Value = c.Offset(, 1).Value + 1
        <SPAN style="color:#00007F">Next</SPAN> c
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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