ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do I call a dynamically created String in a VBA Sub (https://www.excelbanter.com/excel-programming/380007-how-do-i-call-dynamically-created-string-vba-sub.html)

[email protected]

How do I call a dynamically created String in a VBA Sub
 
Hi,

I have 50 Check Boxes and I'm trying to create a Sub that accepts an
Integer parameter (1-50) and then changes the value of the check box to
true.

This is the code that I'm trying to do

Sub chngVal(x)
Dim thestring As String
thestring = "CheckBox" & x & ".Value = True"
thestring
End Sub

So if I called chngVal(25), it would call CheckBox25.Value = True.

This code doesn't work but is there a way to create a String or some
form of Object and then call the line?

Thanks!

Ilan


Martin Fishlock

How do I call a dynamically created String in a VBA Sub
 
Try using me.controls("CheckBox" & x ).Value = True

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


" wrote:

Hi,

I have 50 Check Boxes and I'm trying to create a Sub that accepts an
Integer parameter (1-50) and then changes the value of the check box to
true.

This is the code that I'm trying to do

Sub chngVal(x)
Dim thestring As String
thestring = "CheckBox" & x & ".Value = True"
thestring
End Sub

So if I called chngVal(25), it would call CheckBox25.Value = True.

This code doesn't work but is there a way to create a String or some
form of Object and then call the line?

Thanks!

Ilan



[email protected]

How do I call a dynamically created String in a VBA Sub
 
Hi Martin,

Thanks for your help.

Unfortunately the code didn't seem to work.

I'm getting an error with the Controls part saying "Compile Error -
Method or Data member not Found"

Is there any other way in which to call the string? This would now also
be helpful as for some stages I need to make all 50 Check Boxes not
visible and so would like to use a while or for statement instead of
hand coding all 50 statements of CheckBoxxx.Visible = False.

Thanks for your help again.

Ilan

Martin Fishlock wrote:
Try using me.controls("CheckBox" & x ).Value = True

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


" wrote:

Hi,

I have 50 Check Boxes and I'm trying to create a Sub that accepts an
Integer parameter (1-50) and then changes the value of the check box to
true.

This is the code that I'm trying to do

Sub chngVal(x)
Dim thestring As String
thestring = "CheckBox" & x & ".Value = True"
thestring
End Sub

So if I called chngVal(25), it would call CheckBox25.Value = True.

This code doesn't work but is there a way to create a String or some
form of Object and then call the line?

Thanks!

Ilan




Tom Ogilvy

How do I call a dynamically created String in a VBA Sub
 
Good chance that with 50 checkboxes, these are on a worksheet rather than a
userform. In that case, you would use

Sub chngVal(x as Long)
Worksheets("Sheet1").OleObjects("CheckBox" & x).Object.Value = True
end sub

Assumes checkboxes from the control toolbox toolbar located on a worksheet
named Sheet1 with names like CheckBox1 to CheckBox50

--
Regards,
Tom Ogilvy


"Martin Fishlock" wrote in message
...
Try using me.controls("CheckBox" & x ).Value = True

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


" wrote:

Hi,

I have 50 Check Boxes and I'm trying to create a Sub that accepts an
Integer parameter (1-50) and then changes the value of the check box to
true.

This is the code that I'm trying to do

Sub chngVal(x)
Dim thestring As String
thestring = "CheckBox" & x & ".Value = True"
thestring
End Sub

So if I called chngVal(25), it would call CheckBox25.Value = True.

This code doesn't work but is there a way to create a String or some
form of Object and then call the line?

Thanks!

Ilan





[email protected]

How do I call a dynamically created String in a VBA Sub
 
Thanks Tom.

You're a superstar. It worked.

Tom Ogilvy wrote:
Good chance that with 50 checkboxes, these are on a worksheet rather than a
userform. In that case, you would use

Sub chngVal(x as Long)
Worksheets("Sheet1").OleObjects("CheckBox" & x).Object.Value = True
end sub

Assumes checkboxes from the control toolbox toolbar located on a worksheet
named Sheet1 with names like CheckBox1 to CheckBox50

--
Regards,
Tom Ogilvy


"Martin Fishlock" wrote in message
...
Try using me.controls("CheckBox" & x ).Value = True

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


" wrote:

Hi,

I have 50 Check Boxes and I'm trying to create a Sub that accepts an
Integer parameter (1-50) and then changes the value of the check box to
true.

This is the code that I'm trying to do

Sub chngVal(x)
Dim thestring As String
thestring = "CheckBox" & x & ".Value = True"
thestring
End Sub

So if I called chngVal(25), it would call CheckBox25.Value = True.

This code doesn't work but is there a way to create a String or some
form of Object and then call the line?

Thanks!

Ilan





All times are GMT +1. The time now is 01:20 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com