VBA to create text box

Russk68

Well-known Member
Joined
May 1, 2006
Messages
589
Office Version
  1. 365
Platform
  1. MacOS
Hi
I am using a Mac and I want to create a text box that appears and then is deleted after 3 seconds. With this code, the text box is waiting 3 seconds to be inserted and the instantly deleted. It's doing the opposite of what I need. I'm baffled that the time value is being used by code that appears before it.
Thanks in advance!
VBA Code:
Sub InsertAndDeleteTextBox()
    Dim shp As Shape
    Set shp = ActiveSheet.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=100, Height:=30)
    shp.TextFrame.Characters.Text = "Hello"
 
    ' Wait for 3 seconds before deleting the text box
    Application.Wait Now + TimeValue("00:00:03")
 
    'Delete the text box
    shp.Delete
End Sub
 
Last edited by a moderator:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi Rusk68. I'll share some code that Jon Peltier has offered which seems to resolve this mystery. HTH. Dave
Code:
Sub InsertAndDeleteTextBox()
    Dim shp As Shape
    Set shp = ActiveSheet.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=100, Height:=30)
    shp.TextFrame.Characters.Text = "Hello"
    
    Dim T As Double
    T = Timer
    Do Until Timer - T > 1
      DoEvents
    Loop
    
    ' Wait for 3 seconds before deleting the text box
    Application.Wait Now + TimeValue("00:00:03")
  
    'Delete the text box
    shp.Delete
End Sub
 
Upvote 0
Hi
The code you provided is doing the same thing. The shape takes 3 seconds to insert and then is instantly deleted. I commented out the shp.delete to see the results. I just don't understand how preceding code is being affected.
 
Upvote 0
Hmmm... my trial was a success but was done with Windows os. Sorry, I don't have any further suggestions. Good luck. Dave
 
Upvote 0
I works for me too on my Windows computer. For your Mac, try replacing...

VBA Code:
Do Until Timer - T > 1

with

VBA Code:
Do Until Timer - T > 3

Then remove the following lines from your code...

VBA Code:
' Wait for 3 seconds before deleting the text box
    Application.Wait Now + TimeValue("00:00:03")

Here's another way of doing the same thing...

VBA Code:
Sub InsertAndDeleteTextBox()
    Dim shp As Shape
    Set shp = ActiveSheet.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=100, Height:=30)
    shp.TextFrame.Characters.Text = "Hello"
    
    PauseMacro 3 'seconds
 
    'Delete the text box
    shp.Delete
End Sub

Sub PauseMacro(ByVal secs As Long)

    Dim endTime As Single
    endTime = Timer + secs
    
    Do
        DoEvents
    Loop Until Timer > endTime
    
End Sub

Hope this helps!
 
Upvote 0
Same result. When I comment out shp.delete it takes 3 seconds for the shape to appear. It takes 10 seconds to appear when I change PauseMacro to 10. I will have access to a windows machine next week. It's just boggling my mind on why the PauseMacro line is telling code above it what to do.
Is there a way to show a video here?
 
Upvote 0
Because the VBA code executes very quickly, the time elapsed between two lines is almost negligible. Therefore, when the line above requests inserting the shape, the line below requests to "wait" for 3 seconds, and the shape is hardly inserted before it is instructed to pause for 3 seconds.

Hence, DoEvents is necessary within the 3-second interval. (DoEvents is a command that instructs the code to pause execution for 3 seconds) During these 3 seconds, the shape will be inserted into the sheet.

VBA Code:
Option Explicit
Sub ShowTextBox()
    Dim WTime As Double, shp As Shape
    WTime = Now ' starting time
   
   'Insert Textbox first
    Set shp = ActiveSheet.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
        Left:=100, Top:=100, Width:=100, Height:=30)
    shp.TextFrame.Characters.Text = "Hello"

    ' Pause the code for 3 seconds, to insert Textbox
    Do While Now <= WTime + TimeValue("00:00:03")
        DoEvents
    Loop

   'after 3s, delete
    shp.Delete
End Sub
 
Last edited:
Upvote 0
Try this. Making Excel do something like select a cell and then DoEvents seems to fix the timing issue.

VBA Code:
Sub ShowTextBox()
    Dim WTime As Double, shp As Shape
    WTime = Now ' starting time
   
   'Insert Textbox first
    Set shp = ActiveSheet.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
        Left:=100, Top:=100, Width:=100, Height:=30)
    shp.TextFrame.Characters.Text = "Hello"
    shp.Name = "MyBox"""
    Range("A1").Select
    DoEvents
    
    ' Pause the code for 3 seconds, to insert Textbox
    Application.Wait Now + TimeSerial(0, 0, 3)

   'after 3s, delete
    shp.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top