Showing posts with label VBA. Show all posts

[VBA] Delete Shapes (Form Controls / ActiveX Controls / Picture)

 

1.Delete all shapes

Sub Delete_All_Shapes()
    'Delete all Objects except Comments
    On Error Resume Next
    
    ActiveSheet.DrawingObjects.Visible = True
    ActiveSheet.DrawingObjects.Delete
    
    On Error GoTo 0
End Sub

2.Delete specific shape
Option Explicit

Private Const TYPE_FORM_CONTROL As Integer = 8      'Forms controls
Private Const TYPE_ACTIVEX_CONTROL As Integer = 12  'ActiveX control (control toolbox) or a linked or embedded OLE object.
Private Const TYPE_PICTURE As Integer = 13          'Picture


Sub Delete_specific_Shapes()
    Dim myshape As Shape
    For Each myshape In ActiveSheet.Shapes
 
        Debug.Print "-----"
        Debug.Print "myshape.Type/Name", myshape.Type, myshape.Name
        Debug.Print "", myshape.TopLeftCell.Address, myshape.BottomRightCell.Address
        
 
        If myshape.Type = TYPE_FORM_CONTROL Then
            myshape.Delete
            
        
        
        ElseIf myshape.Type = TYPE_ACTIVEX_CONTROL Then
'            myshape.Delete

        
        ElseIf myshape.Type = TYPE_PICTURE Then
'            myshape.Delete

        
        End If
        

        ' You can also use  myshape.Visible = False
 
    Next myshape
End Sub



Thursday, December 5, 2019
Tag :

[VBA] Insert ComboBox reference to list using name alias

 

1. Create new name alias

- Ctrl + F3 to open Name Manager

- Click "New"

- Input "Name" and select "Refers to"

- Click OK, close Name Manager


2. Insert ComboBox

- Click on cell where will be insert ComboBox

- From menu bar, select "Data / Data Validation"

- In "Settings" tab, select "List" to "Allow" field

- In "Source" field, input below content

    ="defined name alias in step1"


[VBA] Insert Picture

 



Option Explicit


Sub main()

    Dim imagepath As String
    
    Dim imagePos As Range
    
    imagepath = "C:\Users\mypc\Pictures\myimg\commandbutton.JPG"
    Set imagePos = Range("B2:D8")
    
    Call InsertPictureInRange(imagepath, imagePos)

End Sub


' inserts a picture and resizes it to fit the TargetCells range
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    
    If Dir(PictureFileName) = "" Then Exit Sub
    
    Dim p As Object, t As Double, l As Double, w As Double, h As Double
    
    ' import picture
    Set p = ActiveSheet.Pictures.Insert(PictureFileName)
    
    ' determine positions
    With TargetCells
    
        t = .Top
        
        l = .Left
        
        w = .Offset(0, .Columns.Count).Left - .Left
        
        h = .Offset(.Rows.Count, 0).Top - .Top
    
    End With
    
    ' position picture
    With p
    
        .Top = t
        
        .Left = l
        
        .Width = w
        
        .Height = h
    
    End With
    
    Set p = Nothing

End Sub


- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -