Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an automatically generated Option Button to 'True' based on the value in another range in VBA?

Tags:

excel

vba

I generated radio buttons with the help of the answer to How to set an automatically generated radio button to true in VBA?.

My requirement is to set the automatically generated Option button to 'True' when there is a value x in another sheet.

Figure 1: The source to check the value.
enter image description here

Figure 2: The sheet to which the Mark x should be reflected as True.
enter image description here

The radio buttons that are generated are as Indexed as OB2_2 for the option button in 2 row and 2 column.

Here is the code

Private Sub AddOptionButtons(ByRef TargetRange As Range)

Dim m As Variant
m = Sheets("ALLO").Range("D23").Value + 1

Sheets("Final").Range("A2:A" & m).Copy Destination:=Sheets("Int_Result").Range("A2:A" & m)

Dim oCell As Range
For Each oCell In TargetRange
    oCell.RowHeight = 20
    oCell.ColumnWidth = 6
    Dim oOptionButton As OLEObject
    Set oOptionButton = TargetRange.Worksheet.OLEObjects.Add(ClassType:="Forms.OptionButton.1", Left:=oCell.Left + 1, Top:=oCell.Top + 1, Width:=15, Height:=18)
    oOptionButton.Name = "OB" & oCell.row & "_" & oCell.Column
    oOptionButton.Object.GroupName = "grp" & oCell.Top

Next
Call OB2_Click(oCell)

End Sub

Sub OB2_Click(oCell)

Dim col, ro, m As Variant
Dim Shap As Shape
m = Sheets("ALLO").Range("D23").Value + 1

For Each Shap In Sheets("Int_Result").Shapes
    For ro = 2 To m Step 1
        For col = 1 To 13 Step 1
            If Sheets("Final").Cells(ro, col).Value = "" Then
               Sheets("Int_Result").Shapes(ro, col).ControlFormat.Value = False
            Else
               Sheets("Int_Result").Shapes(ro, col).ControlFormat.Value = True
            End If
        Next col
    Next ro
Next Shap

End Sub

I get

"Object variable or With block variable not set" or "Wrong number of arguments or Invalid Property assignment".

on this line

Sheets("Int_Result").Shapes(ro, col).ControlFormat.Value = False 

How do I access the automatically generated radio buttons?

like image 818
Moghen Avatar asked Oct 17 '22 10:10

Moghen


1 Answers

You need to use

 Sheets("Int_Result").OLEObjects("OB2_2").Object.Value = True

Set loop not for shapes, but normal to last row and last column.

So for example:

Dim oCell As Range
Dim LastCell As Range
For Each oCell In TargetRange
    oCell.RowHeight = 20
    oCell.ColumnWidth = 6
    Dim oOptionButton As OLEObject
    Set oOptionButton = TargetRange.Worksheet.OLEObjects.Add(ClassType:="Forms.OptionButton.1", Left:=oCell.Left + 1, Top:=oCell.Top + 1, Width:=15, Height:=18)
    oOptionButton.Name = "OB" & oCell.Row & "_" & oCell.Column
    oOptionButton.Object.GroupName = "grp" & oCell.Top
    Set LastCell = oCell

Next
Call OB2_Click(LastCell)

Sub OB2_Click(oCell as Range)

Dim col As Long, ro As Long
dim m as long, k as long

col = oCell.Column
ro = oCell.Row

For m = 2 to ro
    For k = 2 to col
         If Sheets("Final").Cells(m, k).Value = "" Then
             Sheets("Int_Result").OLEObjects("OB" & m & "_" & k).Object.Value = False
         Else
             Sheets("Int_Result").OLEObjects("OB" & m & "_" & k).Object.Value = True
         End If
    Next k
Next m
End sub
like image 199
Gadziu Avatar answered Oct 20 '22 23:10

Gadziu