引用单元格时错误 - 需要恒定表达式
我想做什么?
代码的第3行具有时间值(30秒),用于重复一段时间后重复宏,但我无论如何都在寻找,而不是在代码中写30秒,我想在f2
单元格记录>记录
中编写30个或其他内容,并每次我想要的时间从那里更改时间值。
解决方案我已经尝试过
我替换了第三代码行
Public Const cRunIntervalSeconds =ThisWorkbook.Worksheets("Record").Range("F2").Value
' run my code every 30 seconds or whenever I want from the F2 cell
,也有一个错误,所以我尝试使用(“ $ f $ 2”)
而不是( “ F2”)
,但仍未解决。我是VBA的新手,但相信我缺少一些简单的事情。
即将到来的问题!
线号。 3正在给我一个错误,.value
通过说出突出显示
编译错误:需要常数表达
是代码
Option Explicit
Public RunWhen As Double
Public Const cRunIntervalSeconds = ThisWorkbook.Worksheets("Record").Range("F2").Value ' time value in Seconds to run a macro from F2 cell
' When I use the below code in the place of 3rd line, it works fine but I need to control it from F2 cell
' Public Const cRunIntervalSeconds = 30
Public Const cRunWhat = "The_master" ' the name of the procedure to run
Dim FirstTime As Boolean
Sub StartTimer()
Set cRunIntervalSeconds = ThisWorkbook.Worksheets("Record").Range("F2").Value
If FirstTime Then
'change for today + 9:00 AM
RunWhen = Date + TimeSerial(8, 55, 0)
Else
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
End If
Application.OnTime EarliestTime:=RunWhen, _
Procedure:=cRunWhat, Schedule:=True
End Sub
Sub The_master()
Call Macro2
' Call StartTimer to run macro again
If Time > TimeSerial(12, 0, 0) Then
'do nothing
Else
StartTimer
End If
End Sub
Sub StopTimer()
'useful for testing to stop any scheduled macro
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, _
Procedure:=cRunWhat, Schedule:=False
End Sub
Sub Auto_Open()
FirstTime = True
'Change for 9:00 AM
If Time > TimeSerial(8, 55, 0) Then
FirstTime = False
End If
Call StartTimer
FirstTime = False
End Sub
我的完整代码有点复杂,它在上午9点开始一个宏,然后每30秒重复一次至下午2点。
What do I want to do?
Line 3 of code has a time value (30 seconds) for repeating the macro after a certain time, but I'm looking anyway to, instead of writing 30 seconds in code, I want to write 30 or whatever in the F2
cell of sheet Record
and change time value from there every time I want.
Solutions I have tried
I replaced 3rd code line with below
Public Const cRunIntervalSeconds =ThisWorkbook.Worksheets("Record").Range("F2").Value
' run my code every 30 seconds or whenever I want from the F2 cell
Then also I got an error, so I tried to use ("$F$2")
instead of ("F2")
, but still not resolved. I'm new to VBA, but believe that some simple thing is behind it that I missing.
The problem which is coming!
Line no. 3 is giving me an error and .Value
is highlighted by saying
Compile Error: Constant Expression Required
Below is the code
Option Explicit
Public RunWhen As Double
Public Const cRunIntervalSeconds = ThisWorkbook.Worksheets("Record").Range("F2").Value ' time value in Seconds to run a macro from F2 cell
' When I use the below code in the place of 3rd line, it works fine but I need to control it from F2 cell
' Public Const cRunIntervalSeconds = 30
Public Const cRunWhat = "The_master" ' the name of the procedure to run
Dim FirstTime As Boolean
Sub StartTimer()
Set cRunIntervalSeconds = ThisWorkbook.Worksheets("Record").Range("F2").Value
If FirstTime Then
'change for today + 9:00 AM
RunWhen = Date + TimeSerial(8, 55, 0)
Else
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
End If
Application.OnTime EarliestTime:=RunWhen, _
Procedure:=cRunWhat, Schedule:=True
End Sub
Sub The_master()
Call Macro2
' Call StartTimer to run macro again
If Time > TimeSerial(12, 0, 0) Then
'do nothing
Else
StartTimer
End If
End Sub
Sub StopTimer()
'useful for testing to stop any scheduled macro
On Error Resume Next
Application.OnTime EarliestTime:=RunWhen, _
Procedure:=cRunWhat, Schedule:=False
End Sub
Sub Auto_Open()
FirstTime = True
'Change for 9:00 AM
If Time > TimeSerial(8, 55, 0) Then
FirstTime = False
End If
Call StartTimer
FirstTime = False
End Sub
My full code is a bit complex, it starts one macro at 9 am and then repeats it every 30 seconds till 2 pm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用 a>您声明一个常数。
如果您希望它是可变的(例如,一个可变单元格值,则需要声明变量而不是常数!
因此,请声明一个变量
并在
Workbook_open
或中初始化它。 worksheet_change
记录
工作表的事件:将一个值设置为variabel,您可以在任何模块中使用它
。返回单元格F2的值,如果您要避免在每次使用该功能时重复读取单元格的值,则可以使用
static
变量而不是公共变量。
可以使用此功能, Forcerefresh:= true)使其从单元格中刷新值。
With the Const statement you declare a constant.
If you want this to be variable (eg. a variable cell value, then you need to declare a variable instead of a constant!
So declare a variable
And initialize it eg on
Workbook_Open
or in theWorksheet_Change
event of theRecord
worksheet:After you set/initialized a value to the variabel you can use it in any module.
Alternatively you can use a
function
to return the value of the cell F2. If you want to avoid repeating to read the value from the cell everytime you use the function, you can use aStatic
variable so it reads it only on the first use:This function can be used instead of a public variable then.
You can use it like
RunIntervalSeconds
and it will use the value that was read from the cell at it's first call or you can useRunIntervalSeconds(ForceRefresh:=True)
to make it refresh the value from the cell.