When you do Sheet1.Range
, you're referencing a global-scope Worksheet
object that VBA automatically declares for you. That "free" object points to ThisWorkbook.Worksheets("Sheet1")
by default, and using it instead of fetching the exact same object reference from the Worksheets
collection is a very good idea.
Now, if you labelled that Sheet1
worksheet "Latency", you've set its Name
, but not its CodeName
- and it's the CodeName
that VBA uses to create the global identifier you can use in code. You can change that in the Properties toolwindow (F4):
Notice the (Name)
(first) property still says "Sheet1"? Change that to Latency
and now this code becomes valid:
lRow = Latency.Cells.SpecialCells(xlLastCell).Row
Now, once you start using the Latency
global identifier, use it everywhere - don't go and start fetching the exact same worksheet reference off the ActiveWorkbook.Worksheets
or ActiveWorkbook.Sheets
collections!
Instead of this:
Sheets("Latency").Range("M" & i).Copy
You do that:
Latency.Range("M" & i).Copy
Do the same for the other sheet, too. Note that the Sheets
collection can contain charts, not just worksheets - when you do fetch a worksheet object reference from a collection (say, from a worksheet you've created at runtime), you should use the Worksheets
collection to do so.
You should also look into properly indenting (disclaimer: this link points to my pet project's website) your code, too.
Sub
do you haveDim Latency As Worksheet
, and laterSet Latency = Worksheets("Yoursheetname")
? While later you are using it correctly withSheets("Latency")
– Shai Rado