VBA to create check boxes on top of picture objects, on multiple sheet?

ajjava

Board Regular
Joined
Dec 11, 2018
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Our ERP generates reports that contain charts. When the reports are exported to Excel, each chart is rendered as a picture object.

I'm looking for a macro that will find each picture object in a workbook and ADD A CHECK BOX IN THE UPPER CORNER. I've added a thick red border around each picture object, for ease of identification in this post.

We send these workbooks out to end-users, who then are required to select which charts they'd like to "keep". I want them to be able to simply check the box for any chart/picture that they'd like to keep.

Can this be done?

I know it would be far easier to put the check boxes in cells NEAR the charts, but since they are grouped so tightly together, there's no good, consistent cell to put them in (and let's operate under the assumption that the end-users are basically clueless in Excel).

uL60xJU.jpg
[/URL][/IMG]
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The following macro assumes that the sheet containing the pictures is the active sheet...

Code:
Option Explicit

Sub AddCheckBoxesToPictures()


    Dim targetSheet As Worksheet
    Dim currentShape As Shape
    Dim currentCheckBox As CheckBox
    Dim pictureCount As Long
    
    Set targetSheet = ActiveSheet
    
    If TypeName(targetSheet) <> "Worksheet" Then
        MsgBox "No worksheet is active!", vbExclamation
        Exit Sub
    End If
    
    pictureCount = 0
    For Each currentShape In targetSheet.Shapes
        If currentShape.Type = msoPicture Then
            pictureCount = pictureCount + 1
            With currentShape
                Set currentCheckBox = targetSheet.CheckBoxes.Add(Left:=.Left + 5, Top:=.Top + 5, Width:=45, Height:=18)
                currentCheckBox.Caption = "Check"
            End With
        End If
    Next currentShape
    
    If pictureCount = 0 Then
        MsgBox "No pictures found.", vbInformation
    End If
    
End Sub

Hope this helps!
 
Upvote 0
This is perfect. Thank you very, VERY much!!!
 
Upvote 0
Just one more thing - how would i modify this code so that the macro loops through all the sheets in a workbook and ignores the very first picture on each sheet?
 
Upvote 0
Try...

Code:
Option Explicit

Sub AddCheckBoxesToPictures()


    Dim currentSheet As Worksheet
    Dim currentShape As Shape
    Dim currentCheckBox As CheckBox
    Dim pictureCount As Long
    Dim pictureCountTotal As Long
    
    If TypeName(ActiveWorkbook) <> "Workbook" Then
        MsgBox "No workbook is active!", vbExclamation
        Exit Sub
    End If
    
    pictureCountTotal = 0
    For Each currentSheet In ActiveWorkbook.Worksheets
        pictureCount = 0
        For Each currentShape In currentSheet.Shapes
            If currentShape.Type = msoPicture Then
                pictureCount = pictureCount + 1
                pictureCountTotal = pictureCountTotal + 1
                If pictureCount > 1 Then
                    With currentShape
                        Set currentCheckBox = currentSheet.CheckBoxes.Add(Left:=.Left + 5, Top:=.Top + 5, Width:=45, Height:=18)
                        currentCheckBox.Caption = "Check"
                    End With
                End If
            End If
        Next currentShape
    Next currentSheet
    
    If pictureCountTotal = 0 Then
        MsgBox "No pictures found.", vbInformation
    End If
    
End Sub

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,215,205
Messages
6,123,634
Members
449,109
Latest member
Sebas8956

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