从vba一次添加多个表单按钮滚动条



我需要下面的宏引用另一个子更改事件来循环引用滚动条的行号I,然后调整单元格Bi。到目前为止,我只能获得100个滚动条来参考B2

Sub Tester88()
    Dim ScrollBar As Object
    Dim rng As Range
    Dim i As Long
    Dim lastRow As Long
    lastRow = 99 'Modify as needed this will be the last possible row to add a button
    For i = 2 To lastRow Step 4
        Set rng = ActiveSheet.Cells(i, 18)  'Column 3, row i
        '## Create the button object and assign a variable to represent it
        Set ScrollBar = ActiveSheet.ScrollBars.Add(1, 1, 1, 1)
        '## use the btn variable to manipulate the button:
        With ScrollBar
            .Top = rng.Top
            .Left = rng.Left
            .width = rng.width
            .height = rng.RowHeight
            .Value = 1
            .Min = 1
            .Max = 100
            .SmallChange = 1
            .LargeChange = 10
            .LinkedCell = "$B$2"
            .Display3DShading = True
        End With
    Next
End Sub

看起来你可以把行放在.LinkedCell中,而不是硬编码。您已将其设置为1-100的范围;请记住,如果您使用LinkedCell,则直接控制单元格的值,因此,如果您控制的数据具有一组现有值,则需要将范围(和值)设置为单元格的现有值,或者将其作为一个仅显示滚动条值的单元格,并使用引用该单元格的公式来获得所需的最终结果。+

我已经解决了这个任务,所以:

Sub Tester88()
    Dim ScrollBar As Object
    Dim rng As Range
    Dim i As Long
    Dim lastRow As Long
    lastRow = 99 'Modify as needed this will be the last possible row to add a button
    For i = 2 To lastRow Step 4
        Set rng = ActiveSheet.Cells(i, 13)  'Column 3, row i
        '## Create the button object and assign a variable to represent it
        Set ScrollBar = ActiveSheet.ScrollBars.Add(1, 1, 1, 1)
        '## use the btn variable to manipulate the button:
        With ScrollBar
            .Top = rng.Top
            .Left = rng.Left
            .Width = rng.Width
            .Height = rng.RowHeight
             .Min = 1
        .Max = 100
        .SmallChange = 1
    .LargeChange = 1
    .LinkedCell = "B" & i
        End With
    Next
End Sub

最新更新