Selecting named activex scrollbars and then setting scrollbar.value

FrustratedEland

New Member
Joined
Jul 12, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Back again with these pesky scrollbars.

So, I am concatenating the scrollbar name to get the right scrollbar, and then trying to set the scrollbar to a value. Once more, I am running into errors. This is a simplified version of what I am trying to do.
The scrollbar is called SCBlue in this example, and A1 contains 'Blue'. B1 contains the position that I would like to set the scrollbar to.

VBA Code:
Private Sub test()
Dim Bar As Object
Dim Position As Long
Category = Sheet1.Range("A1")
Barname = ("SC" & Category)
Position = Sheet1.Range("B1").Value
Set Bar = Sheets("Sheet1").OLEObjects("Barname").Object
With Bar.Value = Position
End With
End Sub

As a check, everything works fine 'manually', if I change lines 7 to 9 to
SCBlue.Value = Position

Grateful as always for any help!
 

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.
Try to always declare your variables with the correct data types :
VBA Code:
Option Explicit

Private Sub test()
    Dim Bar As Object
    Dim Category As String, Barname As String
    Dim Position As Long
    
    Category = Sheet2.Range("A1")
    Barname = ("SC" & Category)
    Position = Sheet1.Range("B1")
    Set Bar = Sheet1.OLEObjects(Barname).Object
    Bar.Value = Position
End Sub
 
Upvote 0
Solution
Try to always declare your variables with the correct data types :
VBA Code:
Option Explicit

Private Sub test()
    Dim Bar As Object
    Dim Category As String, Barname As String
    Dim Position As Long
   
    Category = Sheet2.Range("A1")
    Barname = ("SC" & Category)
    Position = Sheet1.Range("B1")
    Set Bar = Sheet1.OLEObjects(Barname).Object
    Bar.Value = Position
End Sub
Woah!

Although, I do note that if I take out my quotations around Barname in L7 it does work. ;)

I am so used to getting Objects wrong I cant see the wood for the trees it would seem.

Thank you wholeheartedly for taking the time to help me!
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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