Run Macro if selection in specific column?

dwest100

Board Regular
Joined
Jan 2, 2005
Messages
138
If I want to restrict the Selection in the following macro to column C, how would I go about doing that??

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("A2").Value = Application.Sum(Target.SpecialCells(xlCellTypeVisible)) / 1440
End Sub

Thanks!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi there,

Not 100% sure what you are after here, but the following will only update cell A2 if your selection is in column C (or the top left hand cell if you select a range of cells)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 3 Then
Range("A2").Value = Application.Sum(Target.SpecialCells(xlCellTypeVisible)) / 1440
End If
End Sub

Hope this helps, if its not exactly what your after, if you can clarify a little more then we can adjust this to meet your requirements.

Richard
 
Upvote 0
Hi Richard,
That's almost what I need. It works great except if I select a range that has a "C" cell in the top left as you say.

I need the macro to run only if the selected cells are in the C column. If any other cells are selected along with the C cells, then the macro should not run.

Can it be tweaked to accomodate that??

Many thanks for the quick response! :-)
Don
 
Upvote 0
Hi there,

Sorry for the delay in responding, works been keeping me busy!!

Try the following code:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
x = 0
For Each cell In Target
If cell.Column <> 3 Then x = 1
Next cell
If x = 0 Then
Range("A2").Value = Application.Sum(Target.SpecialCells(xlCellTypeVisible)) / 1440
End If
End Sub

Hope it helps

Richard
 
Upvote 0
Beautiful!

I'm learning to think like a programmer by studying the coding techniques. :-) I'm sure that comes easy for you.

Is there a more efficient way for me to learn how to code macros??

Many thanks for your help, it works perfectly!

best regards,
Don
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
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