How can I reference a cell that was just edited in VBA?

Boopin

New Member
Joined
Feb 27, 2014
Messages
1
Hi, so far with this code I can only reference the active cell that is clicked on AFTER a cell is edited; I've tried to work around this by having the users only click ENTER after editing a cell, and by offsetting everything by -1 row. I would like to directly reference the cell that was edited without having to do this. Here are my codes (two separate macros):

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Check As Range

Set Check = ActiveCell.Offset(-1, 7)

' if activecell is in M column, macro is run
If Not Intersect(Target, Range("M:M")) Is Nothing Then

Call ChangeSpreading

End If

End Sub


Sub ChangeSpreading()

Dim Q1 As Range
Dim Q2 As Range
Dim Q3 As Range
Dim Q4 As Range
Dim Total As Range

Set Q1 = ActiveCell.Offset(-1, 2)
Set Q2 = ActiveCell.Offset(-1, 3)
Set Q3 = ActiveCell.Offset(-1, 4)
Set Q4 = ActiveCell.Offset(-1, 5)
Set Total = ActiveCell.Offset(-1, -1)
Set Spreadmodel = ActiveCell.Offset(-1, 0)

'Coding for Q spreading amounts based on M column
If Spreadmodel = 1 Then

Q1 = Total
Q2 = 0
Q3 = 0
Q4 = 0

ElseIf Spreadmodel = 2 Then

Q1 = 0
Q2 = Total
Q3 = 0
Q4 = 0

etc...

End If
End Sub

Basically, if they enter 2 into a cell in column M for example, i would like it to take the data from Total and correctly spread it to Quarter 2 (Q2), both from the same row (no matter where they click after).

Help is greatly appreciated!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi and welcome to Mr Excel

Target is exactly the cell (or cells) that was just edited.

Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("M:M")) Is Nothing Then
        MsgBox Target.Address
        'more code
        'more code
    End If
End Sub

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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