1
votes

Basically I need to select specific cells on an excel sheet in vba. For example the code should be able to select a5, a10, a15. I do not want the cells in between them, just the ones I listed. Is there a specific function that will do this? It seems like .Range could only take start cell and ending cell.

1

1 Answers

6
votes

You'd use this: Range("A5,A10,A15").Select

To add additional cells, just use more commas.

Alternately, you can utilize the Union method to join multiple range objects into a single Range object.

Note that it is generally not a good idea to select cells in VBA as nearly everything can be done without selection. This is a frequent mistake made by people new to VBA due to generated macros (Record Macro) recreating what you do rather than the result you want.