Multiple object expression in With function VBA

前端 未结 1 1706
后悔当初
后悔当初 2021-01-21 15:42

I have a button in my Excel spreadsheet and I move the position of this button using the following VBA:

Sub Positioning_Button()
Set Range_Position = Sheet1.Rang         


        
相关标签:
1条回答
  • 2021-01-21 16:09

    Paste this code in a Module and simply pass the button name to the Sub Sample(). Of course you may want to do do error handling to check if the right button name is sent to MoveButton.

    Sub Sample()
        MoveButton "Button 1"
    End Sub
    
    Sub MoveButton(btnName As String)
        Dim ws As Worksheet
        Dim Range_Position As Range
    
        Set ws = ActiveSheet
        Set Range_Position = ws.Range("D9:E11")
    
        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 Sub
    

    The Set ws = ActiveSheet will take of the necessary sheet.

    However, if you want to specify the sheet name as well then use this

    Sub Sample()
        MoveButton Sheet1, "Button 1"
    End Sub
    
    Sub MoveButton(sh As Worksheet, btnName As String)
        Dim Range_Position As Range
    
        Set Range_Position = sh.Range("D9:E11")
    
        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 Sub
    

    EDIT

    Thanks a lot for your answer. It is almost doing what I want. The issue is that the Button 1 should be moved in both sheets (Sheet1 and Sheet2) no matter which one of those sheets is active. So if I am currently on Sheet1 and I run the VBA both in Sheet1 and Sheet2 it should be moved to Range D9:D11

    You mean like this?

    Sub Sample()
        MoveButton Sheet2, "Button 1", Sheet1
    End Sub
    
    Sub MoveButton(sh As Worksheet, btnName As String, Optional shB As Worksheet)
        Dim Range_Position As Range
    
        Set Range_Position = sh.Range("D9:E11")
    
        With sh.Buttons(btnName)
            .Top = Range_Position.Top
            .Left = Range_Position.Left
            .Width = Range_Position.Width
            .Height = Range_Position.Height
            .Text = "Button"
        End With
    
        If Not shB Is Nothing Then
            With shB.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
    

    When you do not want to move a button in both sheets then use only

    MoveButton Sheet2, "Button 1"
    

    Do not specify the 3rd parameter which is optional.

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