If Then Statement to Apply Code for TextBox

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
I would like to apply the following code (only if the shape is not already placed in the location):

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    
    Sheets("Pricing").Shapes.AddTextBox(msoTextOrientationHorizontal, 807.3529133858, _
        5.2940944882, 632.6470866142, 180.8823622047).Select
    Selection.ShapeRange.Line.Visible = msoFalse
    Range("F10:J10").Select
    Sheets("Pricing").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    ThisWorkbook.Save
End Sub

For example,

If <textbox> worksheet area doesn't have a textbox then
apply the above code.
Else
exit sub

Currently if I use the above code a second time it throws an error.

Any help would be appreciated.</textbox>
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Here's one approach:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If CtrlExists(SheetName:="Pricing", Ctrltype:=msoTextBox) = False Then
        Sheets("Pricing").Shapes.AddTextbox(msoTextOrientationHorizontal, 807.3529133858, _
        5.2940944882, 632.6470866142, 180.8823622047).Select
        Selection.ShapeRange.Line.Visible = msoFalse
        Range("F10:J10").Select
        Sheets("Pricing").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        ThisWorkbook.Save
    End If
End Sub

Function CtrlExists(SheetName, Ctrltype) As Boolean
    CtrlExists = True
    Dim shp
    For Each shp In Sheets(SheetName).Shapes
        If shp.Type = Ctrltype Then Exit Function
    Next shp
    CtrlExists = False
End Function
 
Upvote 0
tlowry,

I added one line of code to assign a name to the text box created. I need to have it assigned since there are times that I will want to delete the textbox after I have created it and need to reference the object to do so...

Any ideas?

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ScreenUpdating = False

    If CtrlExists(SheetName:="Pricing", Ctrltype:=msoTextBox) = False Then
        ActiveSheet.Shapes.AddTextBox(msoTextOrientationHorizontal, 803.8235433071, _
        1.7647244094, 607.9411811024, 189.7058267717).Select
        Selection.ShapeRange.Line.Visible = msoFalse
        Selection.Name = "txtBox1"
        Range("F10:J10").Select
        
        ''
        Range("D16:O634").Select
        Selection.Locked = True
        Selection.FormulaHidden = True
        Range("Q16:AG55").Select
        Range(Selection, Selection.End(xlDown)).Select

        Selection.Locked = True
        Selection.FormulaHidden = True

        ''
        Sheets("Pricing").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        ''
        Range("B16").Select
        ''
        ThisWorkbook.Save
        ''
    End If
    
    
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here's a suggestion...

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If CtrlExists(SheetName:="Pricing", Ctrltype:=msoTextBox) = False Then
        Sheets("Pricing").Shapes.AddTextbox(msoTextOrientationHorizontal, 807.3529133858, _
        5.2940944882, 632.6470866142, 180.8823622047).Select
        '
        [COLOR=#008000][B]Selection.Name = "TextBox_Godzilla01"[/B][/COLOR]
        '
        Selection.ShapeRange.Line.Visible = msoFalse
        Range("F10:J10").Select
        Sheets("Pricing").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        ThisWorkbook.Save
    End If
End Sub

Function CtrlExists(SheetName, Ctrltype) As Boolean
    CtrlExists = True
    Dim shp
    For Each shp In Sheets(SheetName).Shapes
        If shp.Type = Ctrltype Then Exit Function
    Next shp
    CtrlExists = False
End Function
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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