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

时间:2015-03-26 09:52:47

标签: excel forms vba excel-vba

我需要下面的宏引用另一个子更改事件来循环引用滚动条的行号,然后调整单元格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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我已经解决了这个问题,所以:

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