using a variable to cycle through controls.

StevenD

Active Member
Joined
Nov 17, 2004
Messages
262
Hi,

I have 4 command buttons.

I would like to get what the caption is on each command button using a for next loop.

The command buttons are named Answer1, Answer2,Answer3,Answer4.

I was trying to use the below code but it won't work.

Code:
Dim Caption(1 to 4) As String
Dim Number As Integer

For Number = 1 to 4
      Caption(Number)=Userform1.Answer(Number).Caption
Next Number

Any ideas on how I can achieve this?
 
Erik

Why do you have the +1 within the parentheses?
when there are 4 buttons, 3 are not correct
let's assume button1 is correct
you will have
arrCaption(0) = answer2
arrCaption(1) = answer3
arrCaption(2) = answer4
Code:
valRnd = Int(Rnd * UBound(arrCaption) + 1)

int(rnd * 2 + 1) will generate 1 or 2
int(rnd * (2 + 1) will generate 0, 1 or 2
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Erik

I'm still checking out the code I posted.

It's actually still running, sorry about the 'white lie', but it does take a while for 20000*14 iterations.

But I have been checking in on it occasionally and all I'm getting in column B are 2s.

PS J is currently at 11250.
you are counting the enabled buttons
Code:
If ctl.Enabled Then X = X + 1
there are 2 buttons enabled :oops:
 
Upvote 0
Erik

The code I posted is meant to work with however many buttons there are, not just 4.

I've not been able to test your code as I'm still running that large loop.

Just looking at your code I thought that it would only work if it was always the 4th button that matched, but since I've been unable to test I may be wrong.

The original request from the OP was how to loop through controls on a userform, and I think I covered that with the first code I posted.

He then made another request regarding enabling/disabling buttons.

Now I'm still a little confused as to what the OP's goal is but I just had the feeling that it might more than 4 command buttons involved, so that's why I posted code that should work with all the command buttons on a userform.
 
Upvote 0
erik

Yes I'm counting the enabled buttons.

As far as I know what the OP wants to do is this.

1 Enable the button that has a caption that matches what's in A1.

2 Enable one other button, randomly, from the rest of the buttons that don't have a caption that matches A1.

Perhaps I'm missing something, but that's the way I've been interpreting things, but then again that's me.:eek:
 
Upvote 0
Hi, StevenD,

sorry if we are confusing you
sometimes we hijack a thread to discuss about the way to treat an issue
we are just showing you we can argue like little children which should already be in bed :cry:
You can take my or Nories last code (there could be still a next version :) )

EDIT: sentence deleted to avoid confusion
regards to all,
Erik
 
Upvote 0
Erik

That's incorrect as far as I can see.

As I posted I modified the code so the array had a base of 1 rather than the default 0.

The problem was not the last button it was the first one.
 
Upvote 0
what I pasted in my project is not the same as here, perhaps there was en edit after I copied or else I made a paste-error

You are correct about my last remark
added line to check
Code:
Cells(Rows.Count, 2).End(xlUp)(2) = Me.Controls(arrCaption(valRnd)).Name
 
Upvote 0
Thanks to the both of you for your time.

I will try out both and see which suits the application best. I will let you know.
 
Upvote 0

Forum statistics

Threads
1,215,892
Messages
6,127,610
Members
449,389
Latest member
ChessManNaill

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