VBA Macro - Do While trouble

BestiaFPV

New Member
Joined
Dec 21, 2016
Messages
13
Hey guys, I have a macro that I want to keep doing over and over until I change a the value in A7

I have worked out the action of the Macro (rotating pages)

But I am stuck with the Do While

Here is my code, thanks in advance for the help

Code:
Sub Rotating()Application.OnTime Now + TimeValue("00:00:05"), "ShowNextSheet"
End Sub


Sub ShowNextSheet()
    Dim lastIndex As Integer, nextShtIndex As Integer
   
   Do While Range(A7) = "Yes"
    lastShtIndex = Worksheets.Count
    nextShtIndex = ActiveSheet.Index + 1


    If nextShtIndex <= lastShtIndex Then
        Worksheets(nextShtIndex).Select
        Rotating
    Else
        
    Sheets("Running Sheet").Activate
        
    End If
    Loop
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
It's not clear what you mean by "stuck", what is wrong with the DO WHILE LOOP? At a guess, it maybe be which A7 cell it refers to is not fixed to a specific worksheet; (untested) try:
Code:
Sub Rotating()

    Application.OnTime Now + TimeValue("00:00:05"), "ShowNextSheet"
    
End Sub

Sub ShowNextSheet()
    
    Dim x   As Long
    
    With Sheets("Running Sheet")
        Do While .Range("A7").Value = "Yes"
            x = ActiveSheet.Index + 1
            If x <= Worksheets.Count Then
                Worksheets(x).Select
                Rotating
            Else
                .Select
            End If
        Loop
    End With
    
End Sub
 
Last edited:
Upvote 0
My Issue is when I run my Macro I get

Run-time Error '1004'
Method 'Range' of object '_Global' failed


@JackDanIce - Thanks - but when I use yours, it doesnt use the 5 seconds

This is a working verison (with out the Checking of A7)

Code:
[/I][/COLOR]Sub Rotating()Application.OnTime Now + TimeValue("00:00:05"), "ShowNextSheet"
End Sub


Sub ShowNextSheet()
    Dim lastIndex As Integer, nextShtIndex As Integer
   
    lastShtIndex = Worksheets.Count
    nextShtIndex = ActiveSheet.Index + 1




    If nextShtIndex <= lastShtIndex Then
        Worksheets(nextShtIndex).Select
        Rotating
    Else
        
    Sheets("Running Sheet").Activate
        
    End If
   
End Sub


[COLOR=#574123][I]
 
Upvote 0
What happens when you put quotes around the cell reference in your original code?


Code:
 Do While Range([COLOR="#FF0000"]"[/COLOR]A7[COLOR="#FF0000"]"[/COLOR]) = "Yes"
 
Upvote 0
There seems to be no difference between your reply in #3 and what I posted in #2 other than Application.Ontime is on the same line as Sub Rotating() (which it shouldn't be I believe).

Have you tried Mark's suggestion (I'd auto included those in the suggested code as well)? I just ran the suggested code and I do not get the error you mention and does use the 5 seconds.
 
Upvote 0
@JacKDanIce - Well there is a big difference in the result

@Mark8585 - It doesnt give me the error so I must be on the right track.

I think either my placement with the loop is off, or I am not telling it to check the right cell (which I think it is)

I think instead of having a range of A7 - I need it to have a range Running Sheet - A7

So I will be looking up how to do that
 
Upvote 0
O.k so I found a solution

While kind of - it rotates just like I want, it stops when I want. I just need to work out how stop it from rotating the second workbook I have open when I click on that. ( A way to confine the Macro just to the work book called "Display Page")

Here is my current code

Code:
Sub Rotating()
Application.OnTime Now + TimeValue("00:00:05"), "ShowNextSheet"
End Sub




Sub ShowNextSheet()
    Dim lastIndex As Integer, nextShtIndex As Integer
   
 
    lastShtIndex = Worksheets.Count
    nextShtIndex = ActiveSheet.Index + 1




    If nextShtIndex <= lastShtIndex Then
        Worksheets(nextShtIndex).Select
        Rotating
    Else
        
    If Sheets("Running Sheet").Range("A7") = "Yes" Then
       Sheets("Running Sheet").Activate
        Rotating
        
        Else
       Sheets("Running Sheet").Activate
        
       End If
       
    End If
  
End Sub
 
Upvote 0
Here is what I am currently trying (and it is not working

Code:
Sub Rotating()Application.OnTime Now + TimeValue("00:00:30"), "ShowNextSheet"
End Sub




Sub ShowNextSheet()
    Dim lastIndex As Integer, nextShtIndex As Integer
   
    lastShtIndex = Worksheets.Count
    nextShtIndex = ActiveSheet.Index + 1


    If nextShtIndex <= lastShtIndex Then
        Workbooks("Display page.xlsm").Activate
        Workbooks("Display page.xlsm").Worksheets(nextShtIndex).Select
        Rotating
    Else
        
    If Sheets("Running Sheet").Range("A7") = "Yes" Then
       Workbooks("Display page.xlsm").Sheets("Running Sheet").Activate
        Rotating
        
        Else
        Workbooks("Display page.xlsm").Sheets("Running Sheet").Activate
        
       End If
       
    End If
  
End Sub


It currently gets stuck on - Workbooks("Display page.xlsm").Worksheets(nextShtIndex).Select - I get the error " Run-time error '9': Subscript out of range

And clues on where to look to fix that?
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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