TorontoUser
TorontoUser

Reputation: 111

How does Range work?

I have this partial code.

  ActiveSheet.Range("A1:B150").Copy
  Milestone.Sheets("Transit").Activate
  ActiveSheet.Paste
  TransitFile.Close

  'Vlookup L4 Names
  Range("F2").Formula = "=1+5"

"Transit" is the second worksheet in my workbook. In the last line (the formula line), it inputs the formula into the first sheet of my workbook (Sheet1). I don't understand why is that when the "Transit" sheet is the one that is Active. Can anybody help me explain why?

And how come I don't need to use ActiveSheet in Range("F2").Formula but I need to if I use select as in ActiveSheet.Range("A2:B4").Select

Upvotes: 0

Views: 81

Answers (1)

Hearner
Hearner

Reputation: 2709

To avoid mistakes you can use sheets("Name").range. If you simply use range the program will assume you want the range in the active sheet.

For example,

Sheets("Feuil1").Select
Range("A1:B150").Copy

works as well as

Sheets("Feuil1").Select
ActiveSheet.Range("A1:B150").Copy

The best thing to try is to use Sheets("Feuil1").Range("A1:B150").Copy so excel knows which range of which sheet to use

Upvotes: 1

Related Questions