Max String Size Workaround in VBA OnTime Function - Tips & Tricks

If you're working with the VBA OnTime function, you may have encountered an issue with the maximum string size allowed in the function. The OnTime function allows you to schedule a procedure to run at a specific time, but it has a limitation of 255 characters for the procedure name.

If you need to use a procedure name that exceeds this limit, there are a few workarounds you can try. One option is to create a separate module with a shorter name and use that name in the OnTime function. Another option is to create a wrapper function that calls the longer procedure name and use the wrapper function in the OnTime function.

├Źndice
  1. Workaround 1: Create a Separate Module
  2. Workaround 2: Create a Wrapper Function

Workaround 1: Create a Separate Module

<!-- language: vba -->
Sub ScheduleLongProcedure()
    Application.OnTime Now + TimeValue("00:00:01"), "ShortProcedureName"
End Sub

Sub ShortProcedureName()
    ' Call the long procedure here
    LongProcedureName
End Sub

In this example, we create a new module with a shorter procedure name "ShortProcedureName" which then calls the longer procedure "LongProcedureName". We then use "ShortProcedureName" in the OnTime function.

Workaround 2: Create a Wrapper Function

<!-- language: vba -->
Sub ScheduleLongProcedure()
    Application.OnTime Now + TimeValue("00:00:01"), "WrapperFunction"
End Sub

Sub WrapperFunction()
    ' Call the long procedure here
    Call LongProcedureName
End Sub

In this example, we create a wrapper function "WrapperFunction" which simply calls the longer procedure "LongProcedureName". We then use "WrapperFunction" in the OnTime function.

By using these workarounds, you can overcome the limitation of the maximum string size in the VBA OnTime function and schedule procedures with longer names.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information