Consulting

Results 1 to 8 of 8

Thread: The proper way

  1. #1
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location

    The proper way

    Hi, I have a macro that clears all my textboxes on sheet "Data". It looks like this

    Sub ClearText()
    ActiveSheet.Shapes("Text 5").Select
    Selection.Characters.Text = ""
    ActiveSheet.Shapes("Text 22").Select
    Selection.Characters.Text = ""
    ActiveSheet.Shapes("Text 23").Select
    Selection.Characters.Text = ""
    ActiveSheet.Shapes("Text 24").Select
    Selection.Characters.Text = ""
    ActiveSheet.Shapes("Text 25").Select
    Selection.Characters.Text = ""
    ActiveSheet.Shapes("Text 26").Select
    Selection.Characters.Text = ""
    Range("A8").Select
    End Sub
    My question is...is this the proper way or is there a shorter way?

    I know I should take the course and believe me I'm saving for it .

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi,

    No need for all the selections ...


    Sub ClearText() 
    ActiveSheet.Shapes("Text 5").Characters.Text = ""
        ActiveSheet.Shapes("Text 22").Characters.Text = ""
        ActiveSheet.Shapes("Text 23").Characters.Text = ""
        ActiveSheet.Shapes("Text 24").Characters.Text = ""
        ActiveSheet.Shapes("Text 25").Characters.Text = ""
        ActiveSheet.Shapes("Text 26").Characters.Text = ""
    End Sub

  3. #3
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    Thanks firefytr.

  4. #4
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    Sorry firefytr, I was to quick. I get run-time error 438, object doesn't support this property or method.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Airborne, try

    Sub ClearTB()
    Dim tb
    With ActiveSheet
    For Each tb In .Shapes
    If tb.Type = 17 Then
    tb.TextFrame.Characters.Text = ""
    End If
    Next
    End With
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    thanks mdmackillop. Changed Dim sh to Dim tb and all textboxes were cleared.


    Regards.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Oops!

    Code changed for future use.

    Thanks, MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    No problem, I have to keep thinking for myself too. It was the code that helped me .


    Regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •