简体   繁体   English

在“立即”窗口中使用“执行...结束为语句”,Excel VBA

[英]Execute With…End With Statement in Immediate window, Excel VBA

I'm trying to execute With...End With Statement in the Immediate Window, 我正在尝试在即时窗口中执行With ... End With语句,

but I'm not getting it to work. 但我没有让它工作。

So my question is how do you execute multiple line of Code concerning With...End With Statement in the Immediate window? 所以我的问题是如何在即时窗口中执行与With ... End With语句有关的多行代码?

My code I'm trying to execute in the Immediate window: 我想在立即窗口中执行的代码:

With Date_Per_Month.Range("A2:H32")
.Offset(1).Resize(.rows.Count - 1, .Columns.Count - 1).select
end with

Ofcourse it works to write a single line of code like this in the immediate window (but that doesn't answer the question).: 当然,它可以在立即窗口中编写一行这样的代码(但这不能回答问题)。

Date_Per_Month.Range("A2:H32").Offset(1).Resize _(Date_Per_Month.Range("A2:H32").Rows.Count-1,Date_Per_Month.Range("A2:H32").Columns.Count-1).Select

Tried to concatenate each code line with ":" at the end of lines but it didn't work. 试图在每行代码的末尾用“:”连接起来,但是没有用。

Any help is highly appreciated. 非常感谢您的帮助。

The immediate toolwindow is essentially a console. 直接的工具窗口本质上是一个控制台。 You can use it to evaluate expressions without an execution context, or in break mode in the context of the current procedure. 您可以使用它来评估没有执行上下文的表达式,也可以在当前过程的上下文中以中断模式对表达式求值。

Each "line" can be [almost] any executable instruction, but 每个“行”几乎可以是任何可执行指令,但是

You can assign to a variable that doesn't exist... 您可以分配一个不存在的变量...

foo = 42

...and then this identifier exists in the "immediate" context and you can use it in subsequent statements: ...然后此标识符存在于“立即”上下文中,您可以在后续语句中使用它:

?TypeName(foo)
Integer

...until you explicitly reset that context: ...直到您明确重置该上下文:

End
?TypeName(foo)
Empty

But you can't declare a variable: 但是您不能声明变量:

Dim foo

编译错误:立即窗格中无效

A With statement is special: it witholds an object reference, but syntactically it's a block statement that needs to be terminated with an End With token: if you try to type With Sheet1 in the immediate pane, you'll get a compile error saying End With is missing - again because each statement in the immediate pane is a standalone instruction. With语句很特殊:它取消了对象引用,但从语法上讲,它是一个语句,需要以End With令牌终止:如果尝试在即时窗格中键入With Sheet1 ,则会出现编译错误,提示End With丢失-又因为在不久的窗格中的每个语句是一个独立的指令。

We could try to inline it: 我们可以尝试内联它:

With Sheet1 : .Cells(1, 1).Value = 42 : End With

But then we get a weird "invalid watch expression" error: 但是然后我们得到一个奇怪的“无效的监视表达式”错误:

编译错误:无效的监视表达式

Regardless of the reason, like declaring variables with a Dim statement, defining a With variable makes no sense in the context of the immediate pane, where the next instruction to run is whatever instruction you happen to hit ENTER on: there's no scope , no sequence of operations - an instruction runs immediately , and that's all: whatever the contents of the immediate pane is, no other instruction will run until you hit ENTER on it. 不管原因如何,例如使用Dim语句声明变量,在即时窗格的上下文中定义With变量都没有意义,在下一个要执行的指令就是您碰到ENTER的任何指令:没有作用域 ,没有序列操作-一条指令立即运行,仅此而已:无论立即窗格的内容是什么,在您按Enter之前都不会运行其他指令。

how do you execute multiple lines of code [...] in the Immediate window? 您如何在立即窗口中执行多行代码[...]?

The answer is, you don't - because the immediate toolwindow has no concept of "lines of code". 答案是,您不会-因为直接的工具窗口没有“代码行”的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM