

Please note that other Pearson websites and online products and services have their own separate privacy policies. This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site. Whenever Excel sees a period without an object reference directly to the left of it, it looks up the code for the closest With statement and uses that as the object reference. That’s because With Worksheets("Sheet2") implies that the object of the range is the worksheet. Range in your code, but without the preceding object reference. WorksheetFunction.Sum(.Range(.Range("A1"). Range("A1"), Worksheets("Sheet2").Range("A7")))īut this not only is a long line of code but also is difficult to read! Thankfully, there is a simpler way, using With.End With: With Worksheets("Sheet2")

So what do you do? Well, you could write this: WorksheetFunction.Sum(Worksheets("Sheet2").Range(Worksheets("Sheet2"). However, Excel does not assume that you want to carry the Worksheet object reference over to these other Range objects and assumes they refer to Sheet1. Why not? Because Range("A1"), Range("A7") is meant to refer to the sheet at the beginning of the code line. For example, suppose that Sheet1 is your active sheet and you need to total data from Sheet2: WorksheetFunction.Sum(Worksheets("Sheet2").Range(Range("A1"), Range("A7"))) You must identify the range fully each time. If you need to reference a range in another workbook, include the Workbook object, the Worksheet object, and then the Range object: Workbooks("InvoiceData.xlsx").Worksheets("Sheet1").Range("A1")īe careful if you use the Range property as an argument within another Range property. This line of code references Sheet1 of the active workbook even if Sheet2 is the active sheet. To avoid this slowdown, you can refer to a sheet that is not active by first referencing the Worksheet object: Worksheets("Sheet1").Range("A1") Switching between sheets by activating the needed sheet can dramatically slow down your code. Learn More Buy Referencing Ranges in Other Sheets
