VBA to Link ScrollBar Max to Specific Cell

tquist

Board Regular
Joined
Jul 18, 2008
Messages
53
Hi Folks,

I am trying to link the maximum values of a set of scroll bars (across many sheets in the same workbook) to a range of cells set to display the current maximum value. The maximum value for each scrollbar will change each month, necessitating this update. I have almost 100 scroll bars across my workbook, so this would be a prime candidate for automation via VBA.

Here is the deal. Each scroll bar is already linked to a given cell on my "controls" sheet. Two cells to the right of each linked cell is the maximum value that I would like the scroll bar to display. Is there a simple vba code that would assign the each scroll bar's maximum to the value in the cell that is offset 2 columns from each scroll bar's linked cell?

Thank you and please let me know if you need any clarification on my issue. I am using Excel 2007.

Tom
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Assuming your ScrollBars are from the Control Toolbox, try:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim Obj As OLEObject
    For Each Sh In ThisWorkbook.Worksheets
        For Each Obj In Sh.OLEObjects
            If TypeName(Obj.Object) = "ScrollBar" Then
                Obj.Object.Max = Sh.Range(Obj.LinkedCell).Offset(, 2).Value
            End If
        Next Obj
    Next Sh
End Sub
 
Upvote 0
Andrew,

Thanks - this looks promising. However, I'm using Form Controls (not ActiveX) so I don't think that code would work. I should have noted that in my original post - sorry! Could you show the changes to make this work with Form Controls?

Thank you very much.

Tom
 
Upvote 0
Much more straightforward:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim Bar As ScrollBar
    For Each Sh In ThisWorkbook.Worksheets
        For Each Bar In Sh.ScrollBars
                Bar.Max = Sh.Range(Bar.LinkedCell).Offset(, 2).Value
        Next Bar
    Next Sh
End Sub
 
Upvote 0
Andrew,

Looks great - however I tried it and got an error:

Method 'Range' of object '_Worksheet' failed

Any ideas?

Thanks,
Tom
 
Upvote 0
Ah - figured it out. Removing the Sh before the Range:

Bar.Max = Range(Bar.LinkedCell).Offset(, 2).Value

Hopefully this won't have any negative, unintended consequences!

Thanks again for your help, Andrew.

Tom
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top