PDA

View Full Version : help with vba code



patel51
11-22-2008, 08:12 PM
Ok, for the following subprocedure, I am trying to figure out how many times line 7 will execute.


Line 1: Dim intCounter As Integer
Line 2: Dim intIndex As Integer
Line 3: Dim intTotal As Integer
Line 4:
Line 5: For intCounter = 7 To 10
Line 6: For intIndex = 3 To 5
Line 7: intTotal = intTotal + 1
Line 8: Next intIndex
Line 9: Next intCounter




Also, for the following code:


Dim mintNum As Integer


Private Sub cmdButton_Click()
mintNum = 0
Do While mintNum < 5
SubProcedure1
SubProcedure2
mintNum = mintNum + 1
Loop
MsgBox mintNum
End Sub


Private Sub SubProcedure1()
Dim intNumber As Integer
intNumber = 3
intNumber = MysteryFunc(intNumber)
mintNum = MysteryFunc(intNumber)
End Sub


Private Sub SubProcedure2()
Dim intNumber As Integer
mintNum = MysteryFunc(intNumber)
intNumber = MysteryFunc(mintNum)
End Sub


Function MysteryFunc(intArg As Integer) As Integer
MysteryFunc = intArg + mintNum
End Function

What number is displayed in the Message Box when the user clicks on cmdButton?

Edit Lucas: VBA tags added

lucas
11-22-2008, 08:24 PM
I am trying to figure out how many times line 7 will execute.
I think it would depend on how you use it.....I may not understand the question.


What number is displayed in the Message Box when the user clicks on cmdButton?
12

patel51
11-22-2008, 08:36 PM
ok thanks. does anyone else know about the first part?

GTO
11-22-2008, 08:41 PM
Greetings patel51,

Am I safe in believing that you are just starting in VBA? This will be a great site for you to have chosen, as I have "met" some terrific folks here, from those who are just starting out, to those who's knowledge and conceptual skills are most impressive. Welcome, and I sure hope you have some fun, as well as learn all you aim too.

Now as to your first try, copy and paste this code to a standard module.

Mark

PS - when you are pasting or typing code in to ask about, use the little green/white VBA button. This inserts tags, and the code should be between them.

Sub TryMe_01()
' Ok, for the following subprocedure, I am trying to figure out how
' many times line 7 will execute.
Dim intCounter As Integer
Dim intIndex As Integer
Dim intTotal As Integer

For intCounter = 7 To 10
For intIndex = 3 To 5
intTotal = intTotal + 1
Next intIndex
Next intCounter
MsgBox "I looped a total of: " & intTotal & " times."

End Sub

lucas
11-22-2008, 08:41 PM
I'm guessing on question one........

this nested for loop runs twice:

For intIndex = 3 To 5

for each of the 3 incriments of this parent for loop
For intCounter = 7 To 10

so it would be 6 times

If this is classwork you need to tell me because I may be lying....

GTO
11-22-2008, 08:47 PM
@Lucas:
Sorry Steve,

I didn't think of the classwork question. My bad...

Hope all is well,

Mark

patel51
11-22-2008, 08:54 PM
im trying to learn more about these vba loop codes cause we got a final coming up. this was one of the practice questions, but i cant seem to figure out what the right answer it.

lucas
11-22-2008, 08:56 PM
It's ok GTO but this looks a lot like a school assignment.


patel51, we would still be willing to help you but not willing to do it all for you. In other words it would be better to ask a question about how nested for loops work so you could understand them better and resolve the question yourself.

So this should say........

this nested for loop runs three times(3, 4 ,5):

For intIndex = 3 To 5

for each of the four(7,8,9,10) incriments of this parent for loop
For intCounter = 7 To 10

so it would be 12 times

lucas
11-22-2008, 09:04 PM
Just getting the right answer doesn't sound much like a practice question.....you need to understand how you get there.

I'm starting to wonder why you even posted the second piece of code in your first post. Are you able to run it in excel and get the messagbox?

patel51
11-22-2008, 09:09 PM
ok so how do steps work?

For sample = 8 To 2 Step -2
that would run a total of 4 times right/

lucas
11-22-2008, 09:15 PM
That is correct. To test it you can just use GTO's code and take out one loop:


Sub TryMe_01()
Dim intCounter As Integer
Dim intTotal As Integer
For intCounter = 8 To 2 Step -2
intTotal = intTotal + 1
Next intCounter
MsgBox "I looped a total of: " & intTotal & " times."
End Sub


by the way when you post code always select the code and hit the vba button and it will be formatted as above.

lucas
11-22-2008, 09:17 PM
It's running from 8 to 2 ........in increments of -2

patel51
11-22-2008, 09:23 PM
ok thanks GTO for the code. it will help me out a ton.

GTO
11-22-2008, 09:35 PM
Hi patel51,

You are of course welcome, and I hope you'll take heed to Lucas' advise. It's certainly a lot easier to understand any code once you know what its doing. To that end, one of the things that helps me when I'm not "getting" a certain procedure or loop or whatever, is to step-thru it. You can do this by placing the cursor anywhere in the procedure (sub or function) and pressing the F8 key. This will execute the code line-by-line, and you'll get to see what is going on better.

Hope that is of proper help,

Mark