[英]Excel VBA loop folder and check if Excel file name is equal to worksheet
[英]VBA check excel worksheet name
如何检查正在其中运行VBA宏的工作表或工作簿的名称? 用作条件语句(如果工作簿/工作表名称=“”,则...)。
如果工作表是屏幕上显示的工作表,则可以使用以下方法:
If ActiveSheet.Name = "Name of Sheet" then
*Code here
End If
您应该避免在运行宏时未明确指定工作表(如果打开了多个工作表, 则不明确指定工作表)。 使用With ... End With语句可以很容易地指定工作表。 属于工作表的每个范围或单元格引用都只需要一个句点(即.
或句点 )作为前缀,即可将工作表分配为父表。 仅当您要引用其他工作表时,才需要指定其他父工作表。 例如从一个工作表复制到另一个工作表。 使用工作表对象的变量Set可以轻松解决这一问题。
例:
dim ws2 as worksheet
set ws2 = Sheets("Sheet2")
with Sheets("Sheet1")
.range("A2:D4).copy destination:=ws2.range("A2")
'more operations on Sheet1 here using .Range and .Cells
end with
话虽如此,如果您想确定代码中某个位置像Range("A1")
类的单元格引用属于哪个工作表,请获取任何单元格引用的Range .Parent属性 ,然后可以确定父级工作表的.Name属性。从那开始。 Address属性的External参数也可以用于提取工作簿和工作表的字符串单元格地址,但这需要一些文本操作才能确定工作表名称。
例:
debug.print Range("A1").parent.name
debug.print Cells(1, 1).parent.name
debug.print Range("A1").address(0, 0, external:=True)
debug.print Cells(1, 1).address(0, 0, external:=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.