Home » excel » vba – Loop to move buttons in Excel file excluding some sheets

vba – Loop to move buttons in Excel file excluding some sheets

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an Excel spreadsheet consisting of several sheets. In each sheet I have a Button 1.
In order to move this button to Range("D9:E11") in all of the sheets I use the following VBA refering to the solution here:

Sub Sample()
    MoveButton Sheet1, "Button 1", True
End Sub

Sub MoveButton(sh As Worksheet, btnName As String, Optional AllSheets As Boolean)
    Dim Range_Position As Range
    Dim ws As Worksheet

    Set Range_Position = sh.Range("D9:E11")

    If AllSheets = True Then
        For Each ws In ThisWorkbook.Sheets
            With ws.Buttons(btnName)
                .Top = Range_Position.Top
                .Left = Range_Position.Left
                .Width = Range_Position.Width
                .Height = Range_Position.Height
                .Text = "Button"
            End With
        Next ws
    Else
        With sh.Buttons(btnName)
            .Top = Range_Position.Top
            .Left = Range_Position.Left
            .Width = Range_Position.Width
            .Height = Range_Position.Height
            .Text = "Button"
        End With
    End If
End Sub

All this works perfectly so far.


However, now it can happen that some of the sheets (For example Sheet3 and Sheet5) do not have the Button 1.
In this case I get runtime error 1004 if I use the above VBA.
Therefore, I am looking for way to check if Button 1 exists in the sheet and if not the VBA should continue to the next sheet.
Something like this before the line With ws.Buttons(btnName):

If Shapes("Button 1").Exists Then
Run VBA
Else
End If

I tried to combine the VBA code above with this function in several ways but could not make it work so far.
Do you have any idea how to solve it?

How to&Answers:

It can be solved with a parody of “try..catch” construction. It’s always bad to use on error check but in VBA not so many options for this.

Function like this should work for you:

Public Function isBtnExists(Optional ws As Worksheet = Nothing, Optional btnName As String = "Button 1") As Boolean
    If ws Is Nothing Then
        Set ws = ActiveSheet
    End If
   'turn off errors'
    On Error Resume Next
    Dim q As Object
    'trying to assign button to a variable, if it doesn't exist - error number will appear in global Err object' 
    Set q = ws.Buttons(btnName)
    'by checking the error we know exists button or not'
    isBtnExists = (Err.Number = 0)
    'dismiss "On Error Resume Next" not really needed here but in some cases VBA can behave weirdly, so it is better to keep it'
    On Error GoTo -1
End Function

And in your code will be something like this:

 For Each ws In ThisWorkbook.Sheets
      If isBtnExists(ws) Then
          With ws.Buttons(btnName)
              .Top = Range_Position.Top
              .Left = Range_Position.Left
              .Width = Range_Position.Width
              .Height = Range_Position.Height
              .Text = "Button"
          End With
      End If
  Next ws

Answer:

I think either an error handling procedure (as per @AlexandruHapco’s answer) or an iteration over buttons:

'....
For Each ws In ThisWorkbook.Sheets
    For Each btn In ws.Buttons
        If btn.Name = btnName Then
            'Do something
            Exit For
        End If
    Next btn
Next ws
'....