Search code examples
excelvbarangecellselection

Enter values in selected cells using Selection.Cells


I select 3 isolated cells such as a1, f6 and g13, then run this code:

Selection.Cells(1) = 3
Selection.Cells(2) = 4
Selection.Cells(3) = 6

I expect the 3 selected cells to be filled, instead a1, a2 and a3 are filled.

Why? How can I fill the 3 cells of my selection?


Solution

  • If you really need to do this, then I'd still suggest moving away from Selection but the code you provide needs just a small tweak to work:

    Selection.Cells.Areas(1) = 3
    Selection.Cells.Areas(2) = 4
    Selection.Cells.Areas(3) = 6