Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Creating a Control Array

Hi,
I need to create an Excel with an unknown number of rows (can change)
and assign a checkbox and a command to each one of the rows.
The code of the control must know which row it is relating to.
Any ideas?

Thanks

Imry Linden
ClickSoftware Melbourne Australia

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Creating a Control Array

Imry,

This ought to get you started...

"-------------------------------
Sub SprinkleCheckboxes()
Dim lngRow As Long
Dim lngTotal As Long

lngTotal = 20
For lngRow = 2 To 20 Step 2
With ActiveSheet.Shapes.AddFormControl(xlCheckBox, _
Cells(lngRow, 2).Left, Cells(lngRow, 2).Top, _
Cells(lngRow, 2).Width, Cells(lngRow, 2).Height)
.Name = "Box" & lngRow
.OnAction = ThisWorkbook.Name & "!StartSomething"
End With
Next 'lngRow
End Sub
'---
Sub StartSomething()
Dim str As String
str = Application.Caller
MsgBox "Row number is " & Val(Mid$(str, 4))
End Sub
'---------------------------------

Regards,
Jim Cone
San Francisco, USA
"imrylin" wrote in message
ups.com...
Hi,
I need to create an Excel with an unknown number of rows (can change)
and assign a checkbox and a command to each one of the rows.
The code of the control must know which row it is relating to.
Any ideas?
Thanks
Imry Linden
ClickSoftware Melbourne Australia

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Creating a Control Array

Hi imrylin,

Here's another approach if you don't want to fill a sheet with controls
-- why not simulate checkboxes by using cells adjacent to the data?
Assuming that your data is in a sheet named "Data", and that the actual
data starts in B2, you could use column A as your checkboxes.

The following code would go in ThisWorkbook. It checks that you've
selected a cell in column 1 and within the data rows, then it colors
the cell and runs your code in another module.

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Range)

Dim lastrow As Long
' quit if not on correct sheet or selecting more than a single cell
If Sh.Name < "Data" Or Target.Cells.Count 1 Then Exit Sub

' get the last row containing data
lastrow = Sh.Cells(Sh.Rows.Count, 2).End(xlUp).Row
With Target
If .Column = 1 And .Row 1 And .Row <= lastrow Then
If .Interior.ColorIndex < 1 Then
.Interior.ColorIndex = 1
DoNextThing ' run your macro "DoNextThing"
End If
End If
End With
End Sub

You can see that this only runs once for a given cell. Remove the "If
..ColorIndex..." line and it will always fire for one of the valid
cells.

Hope this also gives you some ideas,

Gary

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Creating a Control Array

Thanks Gary, I think that I will use your idea.
Imry

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Creating a Control Array

I should add that since you need to know the row, the line:

DoNextThing ' run your macro "DoNextThing"

should pass the row:

DoNextThing .Row ' run your macro "DoNextThing" on the row

and your "DoNextThing" sub (or whatever its called) should take the row
number as an argument:

Sub DoNextThing(rownum as Long)

Also, I suggested just filling the cell with a color, but you could use
a character, or text, or some fancy formatting if you want to make the
cells look and behave more like some sort of checkbox.

Gary

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need help creating a stock control system Reza Excel Discussion (Misc queries) 1 February 5th 07 06:51 PM
Help with Control Array Sum Formula Jim May Excel Discussion (Misc queries) 12 October 26th 06 11:03 AM
Control array - does it exist in VBA as in VB? Örjan Leringe Excel Programming 2 August 9th 04 06:30 PM
Creating a worksheet with values based on a control cell Dan Eames Excel Programming 3 July 13th 04 01:45 PM
creating a checkbox control on a user form using VBA Tim Marsh[_2_] Excel Programming 3 December 8th 03 02:51 AM


All times are GMT +1. The time now is 04:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"