Home > Blockchain >  How do I create a selection set of user picked cells from which I can iterate through in Excel vba?
How do I create a selection set of user picked cells from which I can iterate through in Excel vba?

Time:11-09

Good Morning Folks,

My question pertains to Excel vba, how can I create a selection set of user defined cells in one active worksheet from which I can then iterate through? I see many many reference to the use of a range but in my case I'm looking to act on individual cells that could be random and anywhere on the sheet that the user chooses.

A simple example of what I'm trying to do would be: The user starts the command, selects a series of random cells by the Left Mouse Button and the Ctrl Key anywhere in the active sheet, after selection the cells then turn a particular color.

This wouldn't be range right? Because there is no relationship between the cells selected, and they wouldn't be the same cells, or the same pattern of cells every time?

I'm stuck right at the beginning, how do I create a selection set of user select cell addresses and then iterate through them in order to act on them, in this case change the color?

Thanks for everyone's time and assistance, G

CodePudding user response:

I think you want to do something like that :

Sub test()
     Application.InputBox(Prompt:="Select cells", Type:=8).Select
     For Each cell In Selection
        Debug.Print cell.Address
        'What you want
     Next
End Sub

Also I recommand enter image description here

Output :

$U$22
$V$30
$Z$29
$W$27
$U$27
$Y$14
$V$13
$T$14
  • Related