Range.Cells属性语法 [英] Range.Cells property syntax

查看:105
本文介绍了Range.Cells属性语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Range.Cells属性感到困惑。根据MSN文档:



Range.Cells属性(Excel)



语法应该是表达式.Cells



然而,在下面的示例中,代码不符合这种语法,但似乎使用单元格作为功能:

 工作表(Sheet1)。激活
范围(单元格(1,1),单元格,3))。Font.Italic = True


解决方案

。在前面的单元格范围被称为不合格的范围引用; Excel的VBA自动包装到当前活动的工作表。

 单元格(1,1)< ==& Activesheet.Cells(1,1)
范围(A1)< ==> Activesheet.Range(A1)

但是,这被认为是不良做法应该避免,因为它会导致随机的问题和错误;代码的行为取决于用户当前在Excel的GUI中的表格。经验丰富的开发人员总是尽可能地避免这种情况,并使用限定范围;



工作表(someSheetName)。单元格(1,1)


I'm confused by the Range.Cells property. According to MSN documentation:

Range.Cells Property (Excel)

The syntax is supposed to be expression .Cells

Yet at the example further down the code is not following this syntax but seems to use Cells as a function:

Worksheets("Sheet1").Activate 
Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True

解决方案

When no dot . precedes a Cells or Range method, this is called an unqualified range reference; Excel's VBA wraps it automatically to the sheet that is currently active.

Cells(1, 1) <==> Activesheet.Cells(1, 1)
Range("A1") <==> Activesheet.Range("A1")

However, this is considered bad practice and it should be avoided because it leads to random issues and bugs; the code's behavior depends on what sheet the user currently has on top in Excel's GUI. Experienced developers always avoid it as much as possible and use qualified ranges; ie

Worksheets("someSheetName").Cells(1, 1).

这篇关于Range.Cells属性语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆