Assign value through OLEObjects

2019-08-15 15:50发布

I'm trying to assign a value to an option button through .OLEobjects but I'm stuck as to what to do. Here's what I got

     with sheets.1
       For Each cOBtn In .OLEObjects
           posInstr = InStr(cOBtn.Name, "oBtn")
              If posInstr > 0 Then
                  cOBtn.Value = True
              End If
        Next
     end with

Obviously .Value doesn't work.. What should I put here?

1条回答
贼婆χ
2楼-- · 2019-08-15 16:37

Use this

cOBtn.Object.Value = True

Do you have any other activex control which has "oBtn" in it's name? – Siddharth Rout 6 mins ago

Yes, many of them; Also the same code (mine) works perfectly fine when I put for example .interior.Color instead of .value – Jente van Heuverswyn 5 mins ago

In such a case ensure that you are actually working with an option button and nothing else :)

Sub Sample()
    With Sheet1
        For Each cOBtn In .OLEObjects
            posInstr = InStr(1, cOBtn.Name, "oBtn", vbTextCompare)

            If posInstr > 0 Then
                If TypeName(cOBtn.Object) = "OptionButton" Then
                    cOBtn.Object.Value = True
                End If
            End If
        Next
    End With
End Sub
查看更多
登录 后发表回答