引用单元格时错误 - 需要恒定表达式

发布于 2025-02-03 12:20:35 字数 1982 浏览 2 评论 0原文

我想做什么?

代码的第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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2025-02-10 12:20:35

a>您声明一个常数。

声明代替字面价值的常数。

如果您希望它是可变的(例如,一个可变单元格值,则需要声明变量而不是常数!

因此,请声明一个变量

Public RunIntervalSeconds As Long

并在Workbook_open中初始化它。 worksheet_change 记录工作表的事件:

RunIntervalSeconds = ThisWorkbook.Worksheets("Record").Range("F2").Value

将一个值设置为variabel,您可以在任何模块中使用它


。返回单元格F2的值,如果您要避免在每次使用该功能时重复读取单元格的值,则可以使用static变量

Public Function RunIntervalSeconds(Optional ByVal ForceRefresh As Boolean = False) As Long
    Static CellValue As Long  ' this variable will keep the value because it is static
    If CellValue = 0 Or ForceRefresh Then  ' if the variable was not initialized yet read 
        ' read the value from the cell
        CellValue = ThisWorkbook.Worksheets("Record").Range("F2").Value
    End If
    ' and return it from the function
    RunIntervalSeconds = CellValue 
End Function

而不是公共变量。

可以使用此功能, Forcerefresh:= true)使其从单元格中刷新值。

With the Const statement you declare a constant.

Declares constants for use in place of literal values.

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

Public RunIntervalSeconds As Long

And initialize it eg on Workbook_Open or in the Worksheet_Change event of the Record worksheet:

RunIntervalSeconds = ThisWorkbook.Worksheets("Record").Range("F2").Value

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 a Static variable so it reads it only on the first use:

Public Function RunIntervalSeconds(Optional ByVal ForceRefresh As Boolean = False) As Long
    Static CellValue As Long  ' this variable will keep the value because it is static
    If CellValue = 0 Or ForceRefresh Then  ' if the variable was not initialized yet read 
        ' read the value from the cell
        CellValue = ThisWorkbook.Worksheets("Record").Range("F2").Value
    End If
    ' and return it from the function
    RunIntervalSeconds = CellValue 
End Function

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 use RunIntervalSeconds(ForceRefresh:=True) to make it refresh the value from the cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文