Find column of value changed

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I want to return the column of a cell whose value has changed.

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
  
    For Each Target In Selection
      
        MsgBox Target.Column
  
    Next Target
  
End Sub

If I enter a value into cell A1, (say 20), then press Enter (assuming the default setting is set to "After pressing Enter, move selection Down"), the message box displays 1, as expected.

However, if I enter a value into cell A1, (say 30), then clicked onto a cell on another column, say cell E6, the message box displays 5 (because E is the fifth column) despite cell A1 (correctly) shows a value of 30.

Similarly, if I enter a value into cell A1, (say 50), then clicked the TAB key, the message box shows 2 (because now the active cell is B1).

How can I amend my code so in these examples, it always displays 1?

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I want to return the column of a cell whose value has changed.

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
 
    For Each Target In Selection
   
        MsgBox Target.Column
 
    Next Target
 
End Sub

If I enter a value into cell A1, (say 20), then press Enter (assuming the default setting is set to "After pressing Enter, move selection Down"), the message box displays 1, as expected.

However, if I enter a value into cell A1, (say 30), then clicked onto a cell on another column, say cell E6, the message box displays 5 (because E is the fifth column) despite cell A1 (correctly) shows a value of 30.

Similarly, if I enter a value into cell A1, (say 50), then clicked the TAB key, the message box shows 2 (because now the active cell is B1).

How can I amend my code so in these examples, it always displays 1?

Thanks
I've experimented and found this works:

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

        MsgBox Target.Column
   
End Sub

but the reason I added the For Next Loop is in case users selected more than a single cell.

Is there a workaround for that scenario?
 
Upvote 0
Maybe
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Rng As Range
   
   For Each Rng In Target
      MsgBox Rng.Column
   Next Rng
End Sub
 
Upvote 0
Solution
Maybe
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Rng As Range
  
   For Each Rng In Target
      MsgBox Rng.Column
   Next Rng
End Sub
Thanks, that does exactly what I want.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

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