Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if more than one cell selected

I am wondering how I can check if more than one cell is celected in a range?

I have 3 cells i want to check the Ranges are "A:B" and also "D", and im trying this code but its not working for me.

If 3 - CountA(range) < 1 Then

How can I do it in anther way?

like image 903
angular2neewbie Avatar asked Jun 16 '15 06:06

angular2neewbie


People also ask

How do you check if a cell has multiple values?

To check if multiple values match, you can use the AND function with two or more logical tests: AND(cell A = cell B, cell A = cell C, …) In dynamic array Excel (365 and 2021) you can also use the below syntax.

How do you highlight multiple cells in Excel?

Select one or more cells To select a range, select a cell, then with the left mouse button pressed, drag over the other cells. Or use the Shift + arrow keys to select the range. To select non-adjacent cells and cell ranges, hold Ctrl and select the cells.

Why is Excel selecting multiple cells when I click on one?

Microsoft Help Text States: “If the selection is extended when you click a cell or press keys to move around the worksheet, it may be because you pressed F8 or SHIFT+F8 to extend or add to the selection.


2 Answers

Something like this for selected range:

If 3-Selection.Cells.Count < 1 then

or, if there is possibility that you will have selected really a lot of cells use this one:

If 3-Selection.Cells.Countlarge < 1 Then
like image 75
Kazimierz Jawor Avatar answered Sep 22 '22 13:09

Kazimierz Jawor


If you are using Worksheet_Change(ByVal Target As Range) or Worksheet_SelectionChange(ByVal Target As Range) then use this code:

If InStr(Target.Address, ":") > 0 Or InStr(Target.Address, ",") > 0 Or InStr(Target.Address, ";") > 0 Then

This will check if the selected range is for example:

`A1;C1` (Cells A1 and C1 are selected) or
`E1:E4` (E1 to E4 are selected)

Sometimes it is used ";" and sometimes "," so that we check both of them.

Instead of Target you can use the range that you defined in your code, for example:

If InStr(MyRange.Address, ":") > 0 Or InStr(MyRange.Address, ",") > 0 Or InStr(MyRange.Address, ";") > 0 Then
like image 40
elano7 Avatar answered Sep 18 '22 13:09

elano7