Running a code based on mouse selected range?

jimayers

Board Regular
Joined
Nov 14, 2010
Messages
99
Hello - I am trying to figure out how to run a code based on multiple cells selected by mouse.
I have been trying some basics to figure it out with no success:
Code:
Sub Color_Green()
Application.ScreenUpdating = False
Dim Ac As Range
Dim a, b As Long

With ActiveSheet
    Set Ac.Range = Selection.Address
    Range(a, b) = Ac
    MsgBox (Ac)
    MsgBox (a)
    MsgBox (b)
End With
Exit Sub

Can anyone help - Thanks - Jim A
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Perhaps something like this in your sheet?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'check if multiple cells are selected




If VBA.InStr(1, Target.Address, ":") <> 0 Then


'run your code here


MsgBox "Multiple cells were selected"


End If




End Sub
 
Upvote 0
I need to run code on each cell selected by the mouse, which is likely going to be in the same column.
 
Upvote 0
Can you demonstrate what code do you intend to run on each cell? For example if user selects Range A1 with mouse do you want to do something? Or if he selects Range ("A1:A2") with mouse then you want to do something to the cells?
 
Upvote 0
I think this may be the type of thing you're looking for:

Code:
Sub Color_Green()
Dim cel As Range

    For Each cel in Selection
        Debug.Print cel.Address, "=", cel.Value
    Next cel

Exit Sub
 
Upvote 0
I think this may be the type of thing you're looking for:

Code:
Sub Color_Green()
Dim cel As Range

    For Each cel in Selection
        Debug.Print cel.Address, "=", cel.Value
    Next cel

Exit Sub

Yes...exactly - thanks :)
PS, I did have to use "cel" inside my If Then statements
 
Upvote 0
I just pointed out a technique I thought you could use. I'm glad you were able to adapt it to your situation! :cool:
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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