Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 可以引用其With块中的对象吗?_Vba_Excel - Fatal编程技术网

Vba 可以引用其With块中的对象吗?

Vba 可以引用其With块中的对象吗?,vba,excel,Vba,Excel,在“With”块中,是否有方法引用块本身的主题 例如,如果我想将文本从下面的单元格复制到单元格中,我可以说: With MyRange .cells(1).offset(1,0).copy .cells(1) End With 但我也可以这样做吗 With MyRange.cells(1) .offset(1,0).copy [???] End With “[??]”是表示“MyRange.Cells(1)”的简单方式。.Cells可以工作,因为Cells属性将

在“With”块中,是否有方法引用块本身的主题

例如,如果我想将文本从下面的单元格复制到单元格中,我可以说:

With MyRange    
    .cells(1).offset(1,0).copy .cells(1)
End With
但我也可以这样做吗

With MyRange.cells(1)   
    .offset(1,0).copy [???]
End With

“[??]”是表示“MyRange.Cells(1)”的简单方式。

.Cells
可以工作,因为
Cells
属性将返回范围本身

With MyRange.cells(1)   
    .offset(1,0).copy .Cells
End With
原因是:
.Cells
属性返回一个范围对象,您可以对该范围对象使用
.Cells
,并进一步扩展到任何返回范围的属性,例如:

With Sheet1.ListObjects(1).ListColumns(1).DataBodyRange
    Debug.Print .Cells.Address  '#returns the address of entire "self" range in specified column's DataBodyRange
End With
ListObject的属性
返回一个
范围
ListColumns
headerrorwrange
等也是如此。表示范围的任何对象的任何属性都会返回一个范围。这些其他对象不是它们自己的类,而是另一个对象的属性,它返回一个
范围


这适用于单个或多个单元格范围。当然,如果需要操作单个单元格,可以对
.Cells
进行微调,例如:
.Cells(1)
,等等。

.Cells
可以工作,因为
Cells
属性将返回范围本身

With MyRange.cells(1)   
    .offset(1,0).copy .Cells
End With
原因是:
.Cells
属性返回一个范围对象,您可以对该范围对象使用
.Cells
,并进一步扩展到任何返回范围的属性,例如:

With Sheet1.ListObjects(1).ListColumns(1).DataBodyRange
    Debug.Print .Cells.Address  '#returns the address of entire "self" range in specified column's DataBodyRange
End With
ListObject的属性
返回一个
范围
ListColumns
headerrorwrange
等也是如此。表示范围的任何对象的任何属性都会返回一个范围。这些其他对象不是它们自己的类,而是另一个对象的属性,它返回一个
范围

这适用于单个或多个单元格范围。当然,如果需要操作单个单元格,可以对
.Cells
进行微调,例如:
.Cells(1)
等。

可以使用偏移量(0)来表示自身:

With MyRange.cells(1)   
    .Offset(1).Copy .Offset(0)
End With
可以使用偏移量(0)来指代其自身:

With MyRange.cells(1)   
    .Offset(1).Copy .Offset(0)
End With

这是否意味着.Cells(1)与.Cells(1).Cells(1)或.Cells(1).Cells(1).Cells(1).Cells(1).Cells(1).Cells(1)相同?大卫,我感谢你的回答,我当然不会投反对票,但我更喜欢斯科特的答案。因为有时“MyRange.cells(1)”类似于“CurSheet.ListObjects(1).Listcolumns(“现金余额”).DataBodyRange”。我必须考虑到底用什么来代替“Cells(1)”@ShawnV.Wilson您仍然会使用
.Cells(1)
。任何范围对象(如
DataBodyRange
)都将遵循与“普通”
range
对象相同的协议。@ShawnV.Wilson实际上更简单,您可以只执行
.Cells
,返回整个范围。@n8我想可能有一些(我认为VBE每行只允许一定长度的字符),但实际上,你可能会在一个递归循环中构建它,几乎是无限的,直到你的内存耗尽。这是否意味着。单元格(1)与。单元格(1)。单元格(1)或。单元格(1)。单元格(1)。单元格(1)。单元格(1)。单元格(1)。单元格(1)。单元格(1)?我很感激你的回答,大卫,我肯定不会投反对票,但我更喜欢斯科特的答案。因为有时“MyRange.cells(1)”类似于“CurSheet.ListObjects(1).Listcolumns(“现金余额”).DataBodyRange”。我必须考虑到底用什么来代替“Cells(1)”@ShawnV.Wilson您仍然会使用
.Cells(1)
。任何范围对象(如
DataBodyRange
)都将遵循与“普通”
range
对象相同的协议。@ShawnV.Wilson实际上更简单,您可以只执行
.Cells
,返回整个范围。@n8我想可能有一些(我认为VBE每行只允许一定长度的字符),但实际上,您可能会在递归循环中构建它,几乎是无限的,直到内存耗尽。谢谢您的所有评论。我喜欢这个,因为它很容易记住“.Offset(0)”,它适用于我能想到的每种类型的范围。@ShawnV.Wilson只有一种类型的
范围。范围的其他“类型”只是各种对象的属性,它们返回一个
范围
对象:)谢谢您的评论。我喜欢这个,因为它很容易记住“.Offset(0)”,它适用于我能想到的每种类型的范围。@ShawnV.Wilson只有一种类型的
范围。范围的其他“类型”只是返回
范围的各种对象的属性