1

May sounds like a duplicate but ive havent seen the exact same one with my research.

This is a really simple question (BigBen may answer it super quicly in comment ;) )

Ive been told .select and .activate was to avoid, havent had time to look why for and so dont understand as well how to use it and when (il look at it and try to understant dw).

Whats better bewteen 1 activate :

Sheets("Vendredi jour").Select
Cells(15, 38).Value = B
Cells(15, 39).Value = C
Cells(15, 40).Value = D
Cells(15, 41).Value = E
Cells(15, 42).Value = F
Cells(15, 43).Value = G

and worksheet specified on every entry

Worksheets("Vendredi jour").Cells(15, 38).Value = B
Worksheets("Vendredi jour").Cells(15, 39).Value = C
Worksheets("Vendredi jour").Cells(15, 40).Value = D
Worksheets("Vendredi jour").Cells(15, 41).Value = E
Worksheets("Vendredi jour").Cells(15, 42).Value = F
Worksheets("Vendredi jour").Cells(15, 43).Value = G

Thx

Patates Pilées
  • 245
  • 1
  • 9

1 Answers1

2

Use a With statement:

With Worksheets("Vendredi jour")
    .Cells(15, 38).Value = B
    .Cells(15, 39).Value = C
    .Cells(15, 40).Value = D
    .Cells(15, 41).Value = E
    .Cells(15, 42).Value = F
    .Cells(15, 43).Value = G
End With

Make sure you have the period . before each Cells call.

BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Two weeks of going trough the VBA section of Overflow made me do it; seems like yall 3-4 guys are helping a lot of newbies and maybe dont get the recongnition for the harsh job of alway correcting the same simple mistakes over and over! – Patates Pilées Jul 30 '20 at 13:26
  • 1
    I appreciate that :-). If you have the time, see [this](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) for the thorough discussion of avoiding `Select`. – BigBen Jul 30 '20 at 13:27