Results 1 to 6 of 6

Adding/Deleting Shapes

Threaded View

  1. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Adding/Deleting Shapes

    Hi again,

    Attached is a simplified workbook showing the approach I mentioned above. The useful aspect of it is that it uses only a single routine for opening/closing a switchgear item, regardless of how many such items the worksheet contains. The code is as follows:

    
    
    
    
    Option Explicit
    
    
    Sub OperateSwitch()
    
        Const sBUTTON_PREFIX    As String = "btn"
        Const sSHAPE_PREFIX     As String = "shp"
    
        Const lGREEN            As Long = 32768
        Const lRED              As Long = 255
    
        Const iROTATION         As Integer = -30
        Const sCLOSE            As String = "Close"
        Const sOPEN             As String = "Open"
        Const iLEFT             As Integer = 5
        Const iTOP              As Integer = -2
    
        Dim sSwitchName         As String
        Dim rSwitchCell         As Range
        Dim rDataTable          As Range
        Dim shpButton           As Shape
        Dim shpSwitch           As Shape
        Dim rState              As Range
        Dim sState              As String
        Dim iRowNo              As Integer
    
        Set rDataTable = ActiveSheet.Range("ptrDataTable")
    
    '   Identify the button which called this routine - this in turn will identify
    '   which switch to operate
        Set shpButton = ActiveSheet.Shapes(Application.Caller)
    
    '   Determine the name of the associated switch shape
        sSwitchName = Replace(Application.Caller, sBUTTON_PREFIX, vbNullString)
    
    '   Locate the above switch in the data table
        With rDataTable.Columns(1)
    
            Set rSwitchCell = Nothing
            Set rSwitchCell = .Cells.Find(What:=sSwitchName, _
                                          LookIn:=xlValues, LookAt:=xlWhole)
    
        End With
    
        If Not rSwitchCell Is Nothing Then
    
    '         Create a reference to the required switch shape
              Set shpSwitch = ActiveSheet.Shapes(sSHAPE_PREFIX & sSwitchName)
    
    '         Determine the next state of the switch - i.e. Open if the swicth is
    '         currently closed, and Closed if the switch is currently open
              Set rState = rSwitchCell.Offset(0, 1)
                  sState = rState.Value
    
    '         Change the state of the required switch and the colour of the associated button
              With shpSwitch
    
                  If sState = sOPEN Then
    
                        .IncrementLeft -iLEFT
                        .IncrementTop -iTOP
                        .Rotation = iROTATION
                        rState.Value = sCLOSE
    
                        shpButton.TextFrame.Characters.Font.Color = lGREEN
    
                  Else: .IncrementLeft iLEFT
                        .IncrementTop iTOP
                        .Rotation = 0
                        rState.Value = sOPEN
    
                        shpButton.TextFrame.Characters.Font.Color = lRED
    
                  End If
    
              End With
    
        Else: MsgBox "The table does not contain data for switch " & sSwitchName
    
        End If
    
    End Sub

    Hope this helps.

    Regards,

    Greg M
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA - Grouping Shapes & Deleting Them
    By CUCE in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-03-2020, 06:39 PM
  2. [SOLVED] Deleting shapes not working
    By peakoverload in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-15-2018, 01:09 PM
  3. [SOLVED] Shapes and Deleting Shapes
    By rob_h in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-24-2017, 10:07 AM
  4. Deleting Shapes
    By Dean81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-25-2011, 01:25 PM
  5. Deleting Specific Shapes
    By FrankStallone in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-15-2006, 01:43 AM
  6. [SOLVED] Deleting Shapes
    By aftamath in forum Excel General
    Replies: 5
    Last Post: 11-04-2005, 08:45 PM
  7. [SOLVED] deleting all shapes and lines
    By thadpole in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-28-2005, 06:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1