Jump to content
  • Advertisement
Sign in to follow this  

Excel Macro Problem

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I decided to finally try to learn a bit about excel macros since I have a problem that I am working on that involves a huge amount of manual editing if I don't. I am having some trouble getting my code to work since I am unfamiliar with the language.
Sub DeleteRows()
    I = 0
    For c = 2 To 2343
        If I = 1 Then I = 0
        Else
            If Cells(c, 8) = 1 Then I = 1
            Else: Cells(c).EntireRow.Delete
            End If
        End If
    Next c
End Sub
The problem it runs into is that the bolded else is without an if. I am not sure exactly how to fix this, and I found some Visual Basic tutorials to start working through until I have a handle on the basics, but I was hoping somebody could point out the problem to me so I can finish up what I am working on first.

Share this post


Link to post
Share on other sites
Advertisement
Split the If...Then statement into multiple lines.

Sub DeleteRows()
I = 0
For c = 2 To 2343
If I = 1 Then
I = 0
Else
If Cells(c, 8) = 1 Then
I = 1
Else
Cells(c).EntireRow.Delete
End If
End If
Next c
End Sub

Share this post


Link to post
Share on other sites
If-Then-Else statements can only take one of these forms:

If A Then B Else C

or:

If A Then
B
Else
C
End If

You cannot mix and match between them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!