PDA

View Full Version : [SOLVED:] Checkbox - linked cells



iWantToLearn
08-30-2014, 12:35 PM
Hi there,

I have problems with dinamycally adding and removing checkboxes.
I have a form and there is a button to add more rows to the table. Every row includes a checkbox too.
So first i add a row, and then add a checkbox to a specified cell in that row.There is a button for removing rows as well (witch should removes checkboxes as well).
My problem is when i ad the checkbox the linked cell property only works for the first one.When i add the second row the linked cell of the firstly added checkbox changes to the one in the new row and the new checkbox has no linked cell. I am adding the stuffs as follows:
---------------------------------------------------

Private Sub addBtn_Click()
Dim y As Integer
y = findFunc("end") // Y define where to insert the new row
Cells(y, 11).EntireRow.Insert
Cells(8, 11).Copy
Cells(y, 11).PasteSpecial xlPasteValidation
Application.CutCopyMode = False
addCb (y)
End Sub
-----------------

Sub addCb(y As String)
Dim shtActive As Worksheet
Set shtActive = ActiveSheet
With shtActive.CheckBoxes
.Add(Range("M" & y).Left, Range("M" & y).Top, Width:=15, Height:=15).Select
Selection.Value = xlOn
Selection.Characters.Text = "%"
.LinkedCell = Selection.TopLeftCell.Address // doesnt work with Address(False, False) neither
End With
End Sub
--------------------------------------------------------------------
I am new to VBA, and i dont even know if its a good way to add checkboxes dinamycally. Any idea why the linked cells move, or what i did wrong?
Thanks for any advices and help:)

br,
Joco

SamT
08-30-2014, 12:54 PM
Dunno if its a typo. Dunno if it is the problem, but, in Sub addCb(y As String) , you are using "x" internally.

You would have been notified of this if you had "Option Explicit" as the top line in the Code Module. Go to the ToolBar Menu, Tools >> Options >> Editor and check "Require Variable Declaration" to automate putting it at the top. I check every box in that "Code Settings" frame.

iWantToLearn
08-30-2014, 01:02 PM
When i wrote this post i changed some things in the code to make it all clear becouse i used different name for the internal variable in addCB(). and i forgot the change the one you mentioned. So its not a problem i corrected my post thanks.
By the way i dont get any errors by adding the rows and checkboxes, i just see there is something wrong with the linked cells, and i need them later on, and for removing the checkboxes.

SamT
08-30-2014, 01:26 PM
Try changing this
.LinkedCell To
Selection.LinkedCell Or even
Selection.ControlFormat.LinkedCell

iWantToLearn
08-30-2014, 01:32 PM
Well thank you so much :) it solved it
How can i mark this theard as solved? i am new to this forum:\ and i see some theards marked as solved
Anyways thanks for your help once again :)

SamT
08-30-2014, 01:34 PM
:thumb

snb
08-30-2014, 02:34 PM
Sub addCb(y)
With activesheet.CheckBoxes.Add(Columns(13).Left, Rows(y).Top, 15, 15)
.Value = xlOn
.Characters.Text = "%"
.LinkedCell = cells(y,13)
End With
End Sub

SamT
08-30-2014, 03:46 PM
Mark thread solved under "Thread Tools"