Multiple object expression in With function looping through all sheets

前端 未结 1 1671
故里飘歌
故里飘歌 2021-01-26 15:32

I have an Excel spreadsheet consisting of two sheets (Sheet1 and Sheet2). In each sheet I have a Button 1.
In order to move this butt

1条回答
  •  星月不相逢
    2021-01-26 16:17

    It is pretty simple. Follow the same logic and declare a Optional boolean variable say, AllSheets.

    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
    

    0 讨论(0)
提交回复
热议问题