Excel VBA executed instruction not in sequence

tgyew

New Member
Joined
Jan 8, 2019
Messages
6
I created a simple count down to delay the display of my math result. The strange thing happen was, one of my math input (Sub1) not get to display before the count down. Can anyone help me with what's wrong in the code below?

Code:
' Function: Making Number from 3 to 10, with count downSub MakingNumber()
Dim Main As Double
Dim Sub1 As Double
Dim Result As Double
Dim Text As String
Dim WShell As Object: Set WShell = CreateObject("WScript.Shell")
Dim strCnt As String


'Clear all value
Range("C2") = ""
Range("B4") = ""
Range("D4") = ""


' Random assign values and compute the results
Main = WorksheetFunction.RandBetween(3, 10)
Sub1 = WorksheetFunction.RandBetween(1, (Main - 1))
Result = Main - Sub1


' Display the values of the question
Range("C2") = Main
Range("B4") = Sub1


' Count Down
For i = 0 To 4
    strCnt = Main & " - " & Sub1 & " = ? " & vbCrLf & "Count Down " & (5 - i)
    WShell.Popup strCnt, 1, "CountDown", vbOKOnly
Next i


' Display the result
Range("D4") = Result
Text = Main & " - " & Sub1 & " = " & Result
MsgBox Text, , "Result"


End Sub

2N6b7mL.jpg
 

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.
As far as I can see the value of Sub1, which I assume is 4, is being displayed within the loop.
 
Upvote 0
As far as I can see the value of Sub1, which I assume is 4, is being displayed within the loop.

By design, Sub1 in the marco should display before the loop. Right now it is display after the loop. Not sure whats went wrong.

Code:
' Display the values of the question
Range("C2") = Main
Range("B4") = Sub1


' Count Down 
For i = 0 To 4
....[COLOR=#574123]
[/COLOR]
 
Upvote 0
Are you referring to the values on the sheet or in the popups/message box?
 
Upvote 0
tgyew,
Have you tried stepping through the code? Place a breakpoint at the first line afer the dim statements and then press f8 to step thrugh each subequent line. Does Cell B4 populate pperly then

Computerman
 
Upvote 0
tgyew,
Have you tried stepping through the code? Place a breakpoint at the first line afer the dim statements and then press f8 to step thrugh each subequent line. Does Cell B4 populate pperly then

Computerman

I tried out adding breakpoint, it able to populate Sub1 value in B4 properly. Once I removed breakpoint, it skipped thru the populating and direct go to count down.
 
Upvote 0
tgew,
Have you tried putting the line that displays B4 before the line that displays C2? See if the problem stays with B or stays with the second value displayed.

Computerman
 
Upvote 0
tgew,
Have you tried putting the line that displays B4 before the line that displays C2? See if the problem stays with B or stays with the second value displayed.

Computerman

I had tried display B4 before C2, and it show only B4 and no C2. Looks like a display timing issues in vba that pop up windows took the priority of display before the sheet get updated. I added one second delay and the problem solved.

Code:
[COLOR=#303336][FONT=inherit]Application[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Wait[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]Now [/FONT][/COLOR][COLOR=#303336][FONT=inherit]+[/FONT][/COLOR][COLOR=#303336][FONT=inherit] TimeValue[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"00:00:01"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]))[/FONT][/COLOR]
 
Upvote 0
Code:
' Display the values of the question
Range("C2") = Main
Range("B4") = Sub1



<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">[COLOR=#303336][FONT=inherit]Application[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Wait[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]Now [/FONT][/COLOR][COLOR=#303336][FONT=inherit]+[/FONT][/COLOR][COLOR=#303336][FONT=inherit] TimeValue[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"00:00:01"[/FONT][/COLOR][COLOR=#303336][FONT=inherit]))[/FONT][/COLOR]</code>' Count Down For i = 0 To 4....</pre>
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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