ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   stop loop if prev sheet does not exist (https://www.excelbanter.com/excel-programming/369817-stop-loop-if-prev-sheet-does-not-exist.html)

sverre

stop loop if prev sheet does not exist
 
Hi,
How do I stop following loop if prev sheet does not exist? It should still
make

Application.DisplayAlerts = true

??
T
----------------------
Application.DisplayAlerts = False
x = 6
Do While Cells(x, 2).Value < ""


Sheets("makron").Activate
ActiveSheet.Previous.Select
ActiveSheet.Delete
x = x + 1

Loop


NickHK

stop loop if prev sheet does not exist
 
Something like this:

Private Sub CommandButton1_Click()
Dim RowCount As Long
Dim i As Long

With Range("rngStart")
RowCount = .End(xlDown).Row - 1 - .Row
End With
With Worksheets("makron")
If RowCount .Index - 1 Then
MsgBox "Not enough sheets"
Exit Sub
End If
Application.DisplayAlerts = False
For i = 1 To RowCount
Worksheets(.Index - 1).Delete
Next
Application.DisplayAlerts = True
End With
End Sub

NickHK

"sverre" wrote in message
...
Hi,
How do I stop following loop if prev sheet does not exist? It should still
make

Application.DisplayAlerts = true

??
T
----------------------
Application.DisplayAlerts = False
x = 6
Do While Cells(x, 2).Value < ""


Sheets("makron").Activate
ActiveSheet.Previous.Select
ActiveSheet.Delete
x = x + 1

Loop





All times are GMT +1. The time now is 02:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com