Macro works alone, but not in a loop.

Joxon

Board Regular
Joined
Nov 3, 2008
Messages
56
Hi everyone!

I have a problem.
When I run Macro4() below it works perfect.
But when I try to run it in a loop in macro3() I get problems. It jumps to cells it should not and behave strange. Any guesses why?


Sub Macro3()
Dim i As Integer
For i = 1 To 20
Call Macro4
Next i
End Sub

Sub Macro4()
Call Macro1
Call Macro2
End Sub

Thank you in advance.

Regards
Joxon
 
Hmm ok, this is not my code but I have tested it and it seems to do autosum correctly so try this:

Sub DoAutoSum()
Dim x As CommandBarControl
Set x = CommandBars.FindControl(ID:=226)
If Val(Application.Version) > 9 Then _
Set x = x.Controls(1)
x.Execute 'AutoSum
If Selection.Cells.Count = 1 Then
x.Execute 'Again to exit edit mode
End If
End Sub



Than just simply replace
Call Macro2 with
Call DoAutoSum

Jesse
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Great! Thanks!
That Autosum macro worked a lot better.
Strange that the other autosum macro didnt work in a loop.
 
Upvote 0
Yeah I am unsure why yours did not work, but I am no VBA expert so who knows aha and I can't take credit for the VBA code I gave you, I found on another website, I will just take credit for spending my time looking aha. Cheers

Jesse
 
Upvote 0

Forum statistics

Threads
1,224,604
Messages
6,179,857
Members
452,948
Latest member
UsmanAli786

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