View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
JJ[_10_] JJ[_10_] is offline
external usenet poster
 
Posts: 7
Default generate a form at runtime?

cheers all who replied .

Heres how i did it in the end

The code works well by assigning a checkbox for every sheet in workbook
(upto 100)

Then processing the the ones checked .


Private Sub UserForm_Initialize()

Dim wsSheet As Worksheet
Dim chkbx As Control
Dim currentsheet As String
Dim Chkbxtop As Long
Dim Chkbxleft As Long
Dim FormWidth As Long
Dim FormHeight As Long
Dim nChkBx As Byte


nChkBx = 0
Chkbxtop = 15
Chkbxleft = 6
FormWidth = 100
FormHeight = 255
ButtonPos = 1
CommandButton1.Top = 185
CommandButton2.Top = 211

For Each wsSheet In Worksheets
If nChkBx = 10 Then
FormHeight = 425
CommandButton1.Top = 354
CommandButton2.Top = 381
ElseIf nChkBx = 20 Then
Chkbxleft = 100
Chkbxtop = 15
FormWidth = 200
ElseIf nChkBx = 40 Then
Chkbxleft = 200
Chkbxtop = 15
FormWidth = 300
ElseIf nChkBx = 60 Then
Chkbxleft = 300
Chkbxtop = 15
FormWidth = 400
ElseIf nChkBx = 80 Then
Chkbxleft = 400
Chkbxtop = 15
FormWidth = 500
ElseIf nChkBx 100 Then
MsgBox "Error To many sheets"
Exit Sub
End If

Chkbxtop = Chkbxtop + 15
nChkBx = nChkBx + 1

Set chkbx = Select_Sheets.Controls.Add("Forms.checkbox.1",
"checkbox" & nChkBx, True)
With chkbx
.Caption = wsSheet.Name
.Accelerator = "N"
.Left = Chkbxleft
.Top = Chkbxtop
End With
Next wsSheet

Select_Sheets.Width = FormWidth
Select_Sheets.Height = FormHeight
CommandButton1.Left = FormWidth - 80
CommandButton2.Left = FormWidth - 80


End Sub


With this to process form
Private Sub CommandButton1_Click()
Dim wsSheet As Worksheet
Dim currentsheet As String
Dim chkbx As Control


currentsheet = ActiveSheet.Name

For Each chkbx In Me.Controls
If Left(chkbx.Name, 8) = "checkbox" Then
If chkbx.Value = True Then

Sheets(chkbx.Caption).Select
If PIntOrExt = "Int" Then
Run HF_Current(Pclientname, Pchargecode)
ElseIf PIntOrExt = "Ext" Then
Run EX_HF_Current(PCompany)
End If
End If
End If
Next

Sheets(currentsheet).Select
Unload Select_Sheets
End Sub