form will not show when VBE is modified

bigt95nt

New Member
Joined
Apr 11, 2011
Messages
26
When the following code is executed, the form that the controls (CheckBoxes 1 to n) are created on fails to "Show" on the sheet. However the form appears in the project window with the correct number CheckBox Controls and if "RUN FORM" is selected the form apears on the sheet and each control works properly when each CheckBox is selected.

If on the other hand, if the code is commented out, the form displays on the sheet with the correct number of CheckBox controls, but no functionality
NOTE: the code shown is in a FOR NEXT loop and is not shown in its entirity.

Any comment would be greatly appreciated!

With TempForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub Checkbox" & i & "_Change()"
.InsertLines Line + 2, " if ActiveChart.SeriesCollection(" & i & ").Format.Line.visible = True then"
.InsertLines Line + 3, ""
.InsertLines Line + 4, "ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= False"
.InsertLines Line + 5, "exit sub"
.InsertLines Line + 6, "else ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= True"
.InsertLines Line + 7, ""
.InsertLines Line + 8, "exit sub"
.InsertLines Line + 9, "end if"
.InsertLines Line + 10, "End Sub"
End With
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The 1st thing I would suggest is to compare the names of the checkboxes to the Names of the _Click event procedures. I can't tell from the code you posted, but the checkboxes that you create have to be named Checkbox" & i so that the _Click event procedures you create match the same Names of the checkboxes you create.
 
Upvote 0
I have included the code in its entirety

For i = iCount To 1 Step -1
Set NewCheckBox = TempForm.Designer.Controls _
.Add("forms.CheckBox.1")
With oChart.TrendLegend

NewCheckBox.Caption = i & " " & .GetText(i)
NewCheckBox.Left = 1
NewCheckBox.Top = WidthIndex
NewCheckBox.Width = .GetWidth(i)
NewCheckBox.Height = .GetLength(i) + 5
WidthIndex = WidthIndex + .GetLength(i)
NewCheckBox.Font = .GetFont(i)
NewCheckBox.ForeColor = .GetColor(i)
NewCheckBox.Value = True
NewCheckBox.Caption = "CheckBox" & i
End With
On Error Resume Next
With TempForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub Checkbox" & i & "_Change()"
.InsertLines Line + 2, " if ActiveChart.SeriesCollection(" & i & ").Format.Line.visible = True then"
.InsertLines Line + 3, ""
.InsertLines Line + 4, "ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= False"
.InsertLines Line + 5, "exit sub"
.InsertLines Line + 6, "else ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= True"
.InsertLines Line + 7, ""
.InsertLines Line + 8, "exit sub"
.InsertLines Line + 9, "end if"
.InsertLines Line + 10, "End Sub"
End With
Next i
VBA.UserForms.Add(TempForm.Name).Show
'
 
Upvote 0
Remove or comment out the On Error Resume Next which may be hiding an error.

Explicitly name each Checkbox added (in red) so they are the exact same names as you write procedures for.

Code:
For i = iCount To 1 Step -1
Set NewCheckBox = TempForm.Designer.Controls _
.Add("forms.CheckBox.1")
With oChart.TrendLegend
[COLOR="Red"]NewCheckBox.Name = "CheckBox" & i[/COLOR]
NewCheckBox.Caption = i & " " & .GetText(i)
NewCheckBox.Left = 1
NewCheckBox.Top = WidthIndex
NewCheckBox.Width = .GetWidth(i)
NewCheckBox.Height = .GetLength(i) + 5
WidthIndex = WidthIndex + .GetLength(i)
NewCheckBox.Font = .GetFont(i)
NewCheckBox.ForeColor = .GetColor(i)
NewCheckBox.Value = True
NewCheckBox.Caption = "CheckBox" & i
End With
[COLOR="Green"]' On Error Resume Next[/COLOR]
With TempForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub Checkbox" & i & "_Change()"
.InsertLines Line + 2, " if ActiveChart.SeriesCollection(" & i & ").Format.Line.visible = True then"
.InsertLines Line + 3, ""
.InsertLines Line + 4, "ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= False"
.InsertLines Line + 5, "exit sub"
.InsertLines Line + 6, "else ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= True"
.InsertLines Line + 7, ""
.InsertLines Line + 8, "exit sub"
.InsertLines Line + 9, "end if"
.InsertLines Line + 10, "End Sub"
End With
Next i
VBA.UserForms.Add(TempForm.Name).Show
'

Does the code create the _Change procedures and if yes, are those procedures triggered when you change the checkboxes?

Forum Tip:
It would be best if you surround your VBA code with code tags e.g.; [CODE]your VBA code here[/CODE]
It makes reading your VBA code much easier. When you're in the forum editor, highlight your pasted VBA code and then click on the icon with the pound or number sign #.
 
Upvote 0
I added the line of code "NewCheckBox.Name = "CheckBox" & i" , but it has no effect. when program runns form appears for a second then disapears, but the form can be found in the project window.
 
Upvote 0
Based on your description, it seems the code is working at least up to the point where the new form is displayed, and then something happens after it is displayed.

Can you post all your code that is after...
VBA.UserForms.Add(TempForm.Name).Show

'
 
Upvote 0
VBA.UserForms.Add(TempForm.Name).Show is the last line of code in the sub Like I mentioned that if you comment out the code modification, the form appears on the sheet with the correct number of CheckBox Controls, but with no functionality!
 
Upvote 0
Observation 1: When the Code posted below is NOT commented out the UserForm will NOT show on the Sheet; however, it does show up in the project explorer window under "Forms" and if you select "RUN USER FORM"
the Userform appears on the Sheet with all CheckBox Controls each of which function correctly.

Observation 2: When the Code posted below IS commented out, the form DOES appear on the Sheet compleet with all CheckBox Controls, but controls do not function.

I would assume that there is some problem with the code below, but do not know what

I hope I have done a better job of stating the problem

TempForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, "Sub Checkbox" & i & "_Change()"
.InsertLines Line + 2, " if ActiveChart.SeriesCollection(" & i & ").Format.Line.visible = True then"
.InsertLines Line + 3, "" .InsertLines Line + 4, "ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= False"
.InsertLines Line + 5, "exit sub"
.InsertLines Line + 6, "else ActiveChart.SeriesCollection(" & i & ").Format.Line.visible= True""
.InsertLines Line + 7, " .InsertLines Line + 8, "exit sub"
.InsertLines Line + 9, "end if"
.InsertLines Line + 10, "End Sub"
End With
 
Upvote 0
Give this a try...

Code:
' On Error Resume Next
With TempForm.CodeModule
Line = .CountOfLines
.InsertLines Line + 1, ""
.InsertLines Line + 2, "Sub Checkbox" & i & "_Change()"
.InsertLines Line + 3, "    With ActiveChart.SeriesCollection(" & i & ").Format.Line"
.InsertLines Line + 4, "        IIf .Visible = msoCTrue, .Visible = msoFalse, .Visible = msoCTrue"
.InsertLines Line + 5, "    End With"
.InsertLines Line + 6, "End Sub"
End With
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top