Editting Attributes of selected OptionButton VBA - vba

I'm trying to make a grid of Option Buttons from about 10x60 and would like to do so with VBA, but I can't get the attribute changing to work.
So far I got this:
Sub Buttons()
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = 48
For i = 8 To 9
For j = 5 To 15
ActiveSheet.Shapes.Range(Array("OptionButton" & k)).Select
k = k + 1
Selection.Copy
With Sheets("Weekreview")
.Cells(i, j).Select
.Paste
.Shapes.Range(Array("OptionButton" & k)).Select
.OptionButtons(k).GroupName = i - 1
.OptionButtons(k).LinkedCell = Range(j, i)
End With
Next
Next
End Sub
The problem with this is that the program errors at .OptionButtons(k).GroupName with the message "Unable to get the OptionButtons property of the Worksheet class".
Anyone who can help me?
Edit 1: My first try (before I tried pretty much all the ways I could find googling the issue) was to use Selection.GroupName, this didn't work either. It looks like it can't access the attributes. So either the attribute changing is wrong, or the selection is wrong.
Edit 2: I got the entire program working except the changing of the GroupName of an existing OptionButton. Even though Selection.LinkedCell works, Selection.GroupName doesnt.

Your code copy and paste OptionButton & k then refers to OptionButton & k+1 (object doesn't exist).
Look at line were k is incremented:
k = k + 1

Please change all the words
ActiveSheet.Shapes.Range(Array("OptionButton" & k))
to
ActiveSheet.Shapes.Range("Option Button " & k)

Please try this code:
Sub Buttons()
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = 48
For i = 8 To 9
For j = 5 To 15
ActiveSheet.Shapes.Range(Array("OptionButton" & k)).Select
k = k + 1
Selection.Copy
ActiveSheet.Paste
With Selection
.Name = "OptionButton" & k
.Top = Worksheets("Weekreview").Cells(i, j).Top
.Left = Worksheets("Weekreview").Cells(i, j).Left
.GroupName = i - 1
.LinkedCell = Range(j, i)
End With
Next
Next
End Sub

Controls with a naming convention of TypeName# are ActiveX controls (e.g. "OptionButton1","TextBox1"). The object itself is wrapped in an OLEObject. ActiveX controls on a Worksheet should be references using the Worksheet's OLEObjects collection.
Properties not available directly from the OLEObject can be access by the OLEObject.Object.
Sub Buttons()
Application.ScreenUpdating = False
Dim opt As OLEObject
Dim cell As Range
With Sheets("Weekreview")
For Each cell In Range(Cells(8, 5), Cells(9, 15))
Set opt = .OLEObjects.Add(ClassType:="Forms.OptionButton.1", Link:=False, DisplayAsIcon:=False, Width:=108, Height:=21)
With opt
.Left = cell.Left
.Top = cell.Top
.Width = cell.Width
.LinkedCell = cell
.Name = cell.Address(False, False)
With opt.Object
.GroupName = cell.Row
.Caption = cell.Address(False, False)
End With
End With
Next
End With
Application.ScreenUpdating = True
End Sub

Related

Option Button Existing in two group boxes

I'm trying to create an array of option buttons (or check boxes if you find that works better) where only one option can be selected in each row and column.
I'm trying to do this so that a user can choose an order in which a certain list goes.
The following code takes the column headers in an excel sheet, and creates bunch of option buttons x columns across and x rows down.
Sub Option_Buttons()
Dim str As String
lCol = Sheets("Headers").UsedRange.Columns.Count
Dim OpBtn() As OptionButton
ReDim OpBtn(lCol, lCol) As OptionButton
Dim GrBx() As GroupBox
ReDim GrBx(lCol) As GroupBox
Dim i
Dim j
'
For i = 1 To lCol
Set GrBx(i) = Sheets("OPBTN").GroupBoxes.Add(Cells(i, 1).Left, _
Cells(i, 1).Top, lCol * 50, 17.25)
With GrBx(i)
.Caption = ""
'.Visible = False
End With
For j = 1 To lCol
Set OpBtn(i, j) = Sheets("OPBTN").OptionButtons.Add _
(Cells(i,j).Left, Cells(i, j).Top, 72, 17.25)
str = Sheets("Turf").Cells(1, j)
check = OpBtn(i, j).Value
With OpBtn(i, j)
.Caption = str
.Value = xlOff '
.LinkedCell = "'OData'!" & Sheets("OData").Cells(i, 1).Address
.Display3DShading = False
End With
Next
Next
End Sub
Is there any way I can make it so that only one option can exist in each row AND column? (so if there are 5 column headers, there are 25 option buttons of which only 5 can be selected).
Is there any way to deal with the option that a user doesn't want a header selected?
I'm trying to avoid all possible uses of the controls tab because this is part of a macro that needs to be used by completely excel illiterate people and needs to be applied to highly variable
I was able to accomplish this using checkboxes. The key was to name the checkboxes using R1C1 notation and assigning a macro (ManageCheckBoxes) to all the checkboxes. After identifying what checkbox was clicked using Application.Caller I iterate over all the checkboxes parsing their names to identify which rows and columns they are in.
Sub Option_Buttons()
Dim str As String
Dim r As Integer, c As Integer
Dim check As CheckBox
lCol = Sheets("Headers").UsedRange.Columns.Count
With Sheets("OPBTN")
For r = 1 To lCol
For c = 1 To lCol
Set check = .CheckBoxes.Add(.Cells(r, c).Left, .Cells(r, c).Top, 72, 17.25)
str = Sheets("Turf").Cells(r, c)
With check
.Caption = str
.Value = xlOff '
.Name = "R" & r & "C" & c
.Display3DShading = False
.OnAction = "ManageCheckBoxes"
End With
Next
Next
End With
End Sub
Sub ManageCheckBoxes()
Dim arrCaller As Variant, arrCheck As Variant
Dim check As CheckBox, ckCaller As CheckBox
arrCaller = getRC(Application.Caller)
With Sheets("OPBTN")
Set ckCaller = .CheckBoxes(Application.Caller)
For Each check In .CheckBoxes
If ckCaller.Name <> check.Name Then
If check.Name Like "R#*C#*" Then
arrCheck = getRC(check.Name)
If arrCheck(0) = arrCaller(0) Or arrCheck(1) = arrCaller(1) Then
check.Value = False
End If
End If
End If
Next
End With
End Sub
Function getRC(sName As String)
Dim a(1) As Long
Dim arr As Variant
arr = Split(sName, "C")
a(0) = Right(arr(0), Len(arr(0)) - 1)
a(1) = arr(1)
getRC = a
End Function
use GroupName property of option buttons. GroupName property determines the group of option button. user will be able to select only one option button from the group.
Set same groupname for your five option buttons and same for other five.
For Example :
Option1 GroupName: grp1
Option2 GroupName: grp1
Option3 GroupName: grp2
Option4 GroupName: grp2
in the above case user will be able to select one from option1 and option2. one button from option3 and option4.

Combo box not returning chosen listindex value

I'm creating a workbook, which tracks available rentals per month. It is divided into 12 sheets, one for each month. The first three columns of each sheet track the type of accommodation, number of bedrooms and what's included in the rental price. The concept there is that there will be a drop-down combo box that allows the user to fill in with a point-and-click option rather than typing things out in order to reduce input errors.
I set up a fixed array, the contents in which changes depending on what column that active cell is in, and then the array is assigned to the combo box. The code lives in the Sheet1 Module under the combo box code and the ThisWorkbook module calls it under SheetSelectionChange, so as to avoid repeating the code in each sheet.
A Standard Module makes the array public
All 12 combo boxes share the same name, cboOptions, and they populate correctly, regardless of what sheet is chosen. My problem is that none of the combo boxes return the listindex value of the choice that's made, regardless of the code telling it to do so. I've been testing to see the value of the position returned against the value of the position chosen, but I have not been able to establish a pattern. I thought about clearing the variables and arrays, thinking that might be what's messing with the code, but it seems to be having no effect. I've read what I could on the issue, but I'm out of ideas on what might be the problem...thank you in advance!
Code in Sheet1 module:
Private Sub cboOptions_Change()
Erase myarray()
cboOptions.Visible = True
cboOptions.Enabled = True
cboOptions.Clear
n = ActiveCell.Row
If n >= 3 And n < 10000 Then
If ActiveSheet.Range(ActiveCell.Address).Address = Range("A" & n).Address Then
myarray(1) = "Apartment"
myarray(2) = "Room"
myarray(3) = "Townhouse"
myarray(4) = "House"
ElseIf ActiveSheet.Range(ActiveCell.Address).Address = Range("B" & n).Address Then
myarray(1) = "1"
myarray(2) = "2"
myarray(3) = "3"
myarray(4) = "4"
myarray(5) = "5"
ElseIf ActiveSheet.Range(ActiveCell.Address).Address = Range("C" & n).Address Then
myarray(1) = "Heat & Water"
myarray(2) = "All-inclusive"
Else
cboOptions.Enabled = False
cboOptions.Visible = False
End If
End If
'ActiveSheet.cboOptions.ListIndex = 0
'Dim x As Long
'MsgBox ActiveSheet.Name
With ActiveSheet
.cboOptions.Left = .Range(ActiveCell.Address).Left
.cboOptions.Top = .Range(ActiveCell.Address).Top
.cboOptions.List = myarray()
With .cboOptions
'the problem is that x needs to get assigned a value from the combo box before it continues to execute
x = .List(.ListIndex)
'MsgBox x
End With
.Range(ActiveCell.Address) = x 'myarray(x)
.Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
x = 0
Erase myarray()
End With
End Sub
Code in ThisWorkbook:
Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)
Application.Run "Sheet1.cboOptions_Change"
End Sub
Code in Module1:
Option Explicit
Public myarray(0 To 5) As String
The nature of the problem seems to be that using more than one array for one combo box breaks down how the listindex values are calculated. I broke down the code to its component features to see if the issue persisted
1) Made a new file and put the code in Sheet1
2) Made separate fixed arrays for each kind of input
3) Created a separate routine for each kind of input
Using ON ERROR RESUME NEXT at the beginning of each routine overlooks the error and the code works properly. Alternatively, putting in a break where the integer variable is given the listindex value of the combo box allows the user to make a choice and assign a value to the integer variable, before continuing. Otherwise, its default value is -1 and returns an error; using .list(.listindex) did not make any difference, suggesting that the code needs to wait for user input (using a combobox event other than Change?).
May just need to establish a separate combo box for each column. Anyway, the code below is the sticks-and-stones version of the above, for a single sheet, and it will do the job if applied to each sheet module in the workbook:
Sub monthnames()
'add month names to the first cell of each sheet
Dim n As Integer
'Sheets(1).Activate
For n = 1 To 12
Sheets.Add After:=ActiveSheet
ThisWorkbook.Sheets(n).Cells(1, 1) = MonthName(n)
Next
End Sub
Private Sub cboOptions_Change()
Dim myarray(1 To 4) As String
Dim myarray2(1 To 5) As String
Dim myarray3(1 To 2) As String
cboOptions.Enabled = True
cboOptions.Visible = True
Dim n As Integer
n = ActiveCell.Row
If n >= 3 And n < 10000 Then
If Range(ActiveCell.Address).Address = Range("A" & n).Address Then
myarray(1) = "Apartment"
myarray(2) = "Room"
myarray(3) = "Townhouse"
myarray(4) = "House"
cboOptions.List = myarray()
inputdata myarray(), n
ElseIf Range(ActiveCell.Address).Address = Range("B" & n).Address Then
myarray2(1) = "1"
myarray2(2) = "2"
myarray2(3) = "3"
myarray2(4) = "4"
myarray2(5) = "5"
cboOptions.List = myarray2()
inputdata2 myarray2(), n
ElseIf Range(ActiveCell.Address).Address = Range("C" & n).Address Then
myarray3(1) = "Heat & Water"
myarray3(2) = "All-inclusive"
cboOptions.List = myarray3()
inputdata3 myarray3(), n
Else
cboOptions.Enabled = False
cboOptions.Visible = False
End If
End If
End Sub
Sub inputdata(myarray, n) 'myarray3, )
On Error Resume Next
Dim x As Integer
cboOptions.Left = Range(ActiveCell.Address).Left
cboOptions.Top = Range(ActiveCell.Address).Top
Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
If Range(ActiveCell.Address).Address = Range("A" & n).Address Then
x = cboOptions.ListIndex + 1
Range(ActiveCell.Address) = myarray(x)
Else
Exit Sub
End If
End Sub
Sub inputdata2(myarray2, n)
On Error Resume Next
Dim y As Integer
cboOptions.Left = Range(ActiveCell.Address).Left
cboOptions.Top = Range(ActiveCell.Address).Top
Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
If Range(ActiveCell.Address).Address = Range("B" & n).Address Then
y = cboOptions.ListIndex + 1
Range(ActiveCell.Address) = myarray2(y)
Else
Exit Sub
End If
End Sub
Sub inputdata3(myarray3, n)
On Error Resume Next
Dim z As Integer
cboOptions.Left = Range(ActiveCell.Address).Left
cboOptions.Top = Range(ActiveCell.Address).Top
Columns(ActiveCell.Column).ColumnWidth = cboOptions.Width * 0.18
If Range(ActiveCell.Address).Address = Range("C" & n).Address Then
z = cboOptions.ListIndex + 1
Range(ActiveCell.Address) = myarray3(z)
Else
Exit Sub
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call cboOptions_Change
End Sub

Hide image when macro successfully done

I have an image on which I have bound macro "Zadat2". I would like to hide the image after the macro is successfully done but if I change the value in cell C3 that image is made visible again.
done.visible = False does not function.
My code:
Sub zadat2()
Dim reg, check1 As String
Dim i, j, done As Integer
reg = Cells(2, 3).Value
check1 = Range("C4").Value
If check1 = "PRAVDA" Then
i = 2
j = 1
done = 0
Do While Sheets("data").Cells(i, j) <> ""
If Sheets("data").Cells(i, j) = reg Then
vytisteno = ZkontrolovatAVytiskoutSoubor()
cekej = Wait()
done = Sheets("data").Cells(i, j + 3)
Sheets("data").Cells(i, j + 3) = done
done.Visible = False
Exit Do
End If
i = i + 1
Loop
Else
MsgBox ("Opravit, špatný štítek!!!")
End If
Cells(3, 3) = ""
Cells(3, 3).Select
ActiveWindow.ScrollRow = Cells(1, 1).Row
End Sub
I think you have to work with object Shape if you want to hide Image. I`m not sure if there is a function which gives you Shape from a Cell, but there is a function which gives you top-left cell from a shape. You will have to slightly change your approach, but i think some of this functions may be usefull.
Sheets("data").Shapes("Picture 1").Visible = True
Sheets("data").Shapes("Picture 1").TopLeftCell.Row
Sheets("data").Shapes("Picture 1").TopLeftCell.Column
For Each s In ActiveSheet.Shapes
With s
.Visible = False
End With
Next s
assuming your image is named after "ZadatShape"
place the following statement in your Sub zadat2() right after the statement that certifies the "macro successfully done" :
ActiveSheet.Shapes("ZadatShape").Visible = False '<--| change "ZadatShape" to your actual shape name
place the following code in the code pane of the worksheet where the shape is:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$3" Then ActiveSheet.Shapes("ZadatShape").Visible = True '<--| change "ZadatShape" to your actual shape name
End Sub

VBA Excel 2010 - Runtime 1004 error when using range

I am having an issue referencing ranges in my vba program. The following snippet of code shows my original code:
With Worksheets("Overall 6 mo")
.Columns("A:G").ColumnWidth = 13.57
.Range("A1:Z100").Rows.RowHeight = 15
.Columns("F:G").NumberFormat = "0.00%"
.Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value
.Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula
.Range("A1").NumberFormat = "#"
.Range("A1") = .Name
End With
This would throw the "runtime 1004 application-defined or object-defined error" after going through line 3.
So then, I changed
.Range("A1:Z100").Rows.RowHeight = 15
to
.Rows.RowHeight = 15
The point was to make the cells that i need to use have a height of 15 so the change didn't hurt my program. And now, it will allow that but then throw the same error at the next line, where I reference a range again. So I'm trying to figure out why it won't allow me to use .range ? Or at least how I can fix it?
UPDATE:
I have come to realize that I cannot use the .Range method anywhere in my workbook (not just in the instance above). What would disable me to use .Range everywhere?
UPDATE2:
It will now no longer let me use the .Columns method in the second line. I haven't done anything but step through it a couple times. What is wrong with this thing?
UPDATE3:
It seems that when i restart excel, it will allow me to run the worksheet "Overall 6 mo" code once, and then starts throwing the error every time after that. I've included the code for the rest of the sheet.
Option Explicit
Private Sub Worksheet_Activate()
Application.ScreenUpdating = False
Dim shIndex As Integer
Dim rowIndex As Integer
Dim myLastRow As Integer
Dim shLastRow As Integer
Dim col As Integer
myLastRow = Worksheets("Overall 6 mo").Cells(65536, 1).End(xlUp).Row
' Format Worksheet
Sheets("Overall 6 mo").Cells.Clear
With Worksheets("Overall 6 mo")
.Columns.ColumnWidth = 13.57
.Rows.RowHeight = 15
.Columns("F:G").NumberFormat = "0.00%"
.Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value
.Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula
.Range("A1").NumberFormat = "#"
.Range("A1") = .Name
End With
' Clear current sheet data
myLastRow = Worksheets("Overall 6 mo").Cells(65536, 2).End(xlUp).Row
Worksheets("Overall 6 mo").Range(Cells(4, 1), Cells(myLastRow, 7)).Clear
' Compile data from last six months and add to and display on "Overall 6 mo" sheet
For shIndex = Worksheets.Count - 5 To Worksheets.Count
Worksheets(shIndex).Activate
myLastRow = Worksheets("Overall 6 mo").Cells(65536, 2).End(xlUp).Row
shLastRow = Worksheets(shIndex).Cells(65536, 1).End(xlUp).Row
Worksheets("Overall 6 mo").Cells(myLastRow + 1, 1).Value _
= MonthName(Month(CDate(Worksheets(shIndex).Name)), False)
Worksheets(shIndex).Range("A4:D" & shLastRow) _
.Copy (Worksheets("Overall 6 mo").Cells(myLastRow + 1, 2))
Next shIndex
' Call UpdateChart to clear and re-add Quality and Cost charts to wks
Call UpdateCharts(Worksheets("Overall 6 mo").Index)
Worksheets("Overall 6 mo").Activate
Application.ScreenUpdating = True
End Sub
You can do row height changes with:
.Range("A1:Z100").RowHeight = 15
And can you use Range Copy method
Worksheets("TEMPLATE").Range("A3:F3").Copy .Range("B3")
Worksheets("TEMPLATE").Range("E4:F100").Copy .Range("F4")
UPDATE:
Option Explicit
Private Sub Worksheet_Activate()
Dim oSh As Worksheet
Dim shIndex As Long
Dim rowIndex As Long
Dim myLastRow As Long
Dim shLastRow As Long
Application.ScreenUpdating = False
Set oSh = ThisWorkbook.Worksheets("Overall 6 mo")
' Format Worksheet
With oSh
.Cells.Clear
.Columns.ColumnWidth = 13.57
.Rows.RowHeight = 15
.Columns("F:G").NumberFormat = "0.00%"
.Range("A1").NumberFormat = "#"
.Range("A1") = .Name
.Range("B3:G3") = Worksheets("TEMPLATE").Range("A3:F3").Value
.Range("F4:G100") = Worksheets("TEMPLATE").Range("E4:F100").Formula
End With
' Clear current sheet data
oSh.Range(oSh.Cells(4, 1), oSh.Cells(GetLastRow(oSh, 2), 7)).Clear
' Compile data from last six months and add to and display on "Overall 6 mo" sheet
For shIndex = Worksheets.Count - 5 To Worksheets.Count
'Worksheets(shIndex).Activate
myLastRow = GetLastRow(oSh, 2)
shLastRow = GetLastRow(Worksheets(shIndex), 1)
oSh.Cells(myLastRow + 1, 1).Value = MonthName(Month(CDate(Worksheets(shIndex).Name)), False)
Worksheets(shIndex).Range("A4:D" & shLastRow).Copy oSh.Cells(myLastRow + 1, 2)
Next shIndex
' Call UpdateChart to clear and re-add Quality and Cost charts to wks
Call UpdateCharts(oSh.Index)
oSh.Activate
Set oSh = Nothing
Application.ScreenUpdating = True
End Sub
Private Function GetLastRow(oSheet As Worksheet, lngColumn As Long) As Long
GetLastRow = oSheet.Cells(oSheet.UsedRange.SpecialCells(xlLastCell).Row + 1, lngColumn).End(xlUp).Row
End Function
Is "TEMPLATE" in the same workbook with Index 1 (or less than Worksheets.Count - 5)? I have comment out Worksheets(shIndex).Activate as seems no need to run this sub every time in the For loop.
RowHeight applies to whole rows, not parts of rows.
So use
.Range("A1:A100").EntireRow.RowHeight = 15
or
.Range("1:100").RowHeight = 15

Can't read the value of a CheckBox

I'm having a weird problem.
I have a form that looks like the one in the image
Those checkboxes are generated with a peace of code like this one (there is a loop that increments the i and the l ...everything is fine there because the other components are generated through the same piece of code and I have no problem getting their values):
Public Sub AddCboxs(form, masina, nrmasini, replicare, nrcboxs)
Dim i, k, l As Integer
i = 0
l = 1
Do While i < nrmasini
Do While l < nrcboxs + 1
Set cControl = form.Controls("iooly" & i).Add("Forms.CheckBox.1", "sc" & l & "oly" & i, True)
With cControl
.Width = 15
.Height = 16
.Top = 200 + k
.Left = 205
End With
k = k + 35
l = l + 1
Loop
l = 1
k = 0
i = i + 1
Loop
End Sub
Now... I want to do the following thing. If the SC checkbox is checked I want to do some stuff that you'll see in the following piece of code ... without checking the value of the checkbox the code works just fine and does what I want it to do... but the problem is that I need to do it just when the checkbox is checked.
Public Sub CalcOly()
Dim i, j, k As Integer
Dim Rand, ContorVal, ContorTotal As Long
Dim ws As Worksheet
Set ws = Worksheets("Config")
Dim cControl As Control
i = 0
j = 1
ContorVal = 0
Do While i < 5
Do While j < 3
Rand = 30
If raport.Controls("sc" & j & "oly" & i).Value = True Then
Do While ws.Cells(Rand, 1).Value <> "" And Rand < 65536
If ws.Cells(Rand, 1).Value = raport.Controls("combo" & j & "oly" & i).Value Then
Set cControl = raport.Controls("iooly" & i).Add("Forms.Label.1", "valoare" & j & "oly" & i, True)
With cControl
.Caption = Int(ws.Cells(Rand, 2).Value * raport.Controls("q" & j & "oly" & i).Value) & " RON"
.Width = 55
.Height = 14
.Top = 42 + k
.Left = 225
End With
ContorVal = ContorVal + Int(ws.Cells(Rand, 2).Value * raport.Controls("q" & j & "oly" & i).Value)
End If
Rand = Rand + 1
Loop
End If
j = j + 1
k = k + 35
Loop
Set cControl = raport.Controls("iooly" & i).Add("Forms.Label.1", "totalval" & "oly" & i, True)
With cControl
.Caption = ContorVal & " RON"
.Width = 55
.Height = 14
.Top = 350
.Left = 225
End With
k = 0
j = 1
i = i + 1
ContorVal = 0
Loop
End Sub
Now here's the weird thing... if I click on CALCUL VALOARE (which calls the CalcOly procedure) it executes the code but no matter if the SC checkbox is checked or no it shows no value. If I go on page Olympia 4 or Olympia 5 it does what it needs to do but again... ignoring if the SC checkboxes are checked or not.
I tried to get the value of the checkbox in a separate caption and I observed that it doesn't get it... I really don't know why!
Thanks a lot for your help!
Later: http://www.youtube.com/watch?v=mPb617JxgtI I've uploaded a video to see how strange the app acts. I don't get it... if I remove the If that checks if the checkbox is True or False it works fine
You should show us the piece of code that generates the first checkboxes, especially if your other checkboxes work properly.
That said, several things:
In VBA, you can't declare variables this way:
Dim i, j, k As Integer
Dim Rand, ContorVal, ContorTotal As Long
You have to do:
Dim i As Integer, j As Integer, k As Integer
Dim Rand As Long, ContorVal As Long, ContorTotal As Long
See here
Debug
What did you see when debuging your userform. Do the raport.Controls("sc" & j & "oly" & i) exist ?
By the same way, you should have a look at raport.Controls() collection to see what are the elements and their properties.
That would tell you if your Controls were really created the way you wanted.
This seems a bit obscure but from the help for the Add method of the Controls property:
If you add a control at run time, you must use the exclamation syntax
to reference properties of that control. For example, to return the
Text property of a control added at run time, use the following
syntax:
userform1!thebox.text
You could try changing:
If raport.Controls("sc" & j & "oly" & i).Value = True Then
to:
If raport!("sc" & j & "oly" & i).Value = True Then
edit: I'd originally used a separate variable to build up the string but that wouldn't work as the program would look for a control with the same name as the variable rather than the value of the variable
edit2: if that doesn't work you could try:
If raport!"sc" & j & "oly" & i.Value = True Then
but it would depend on the precedence of ! and . relative to &
Just use these for your button click
if checkboxname.value = true then
invoke your function
end if