+ Reply to Thread
Results 1 to 6 of 6

Expected Do before loop (compile error)

Hybrid View

  1. #1
    Registered User
    Join Date
    07-14-2013
    Location
    Grand Rapids, Michigan
    MS-Off Ver
    Excel 2010
    Posts
    35

    Expected Do before loop (compile error)

    I am wanting the code below to find data in D2 and if it returns any of the states abbreviated in the variable "x," to grab the range of the active cell and 4 columns to the left of it (A & Value of row), cut it, and paste it onto the next first empty row on sheet 3. I'm trying to learn looping so that it will continue down column D until there's an empty cell. When I run the code as is, it gives me a compile error. I'm really new at the Visual Basic end of excel, but I love what I've seen and figuring out why this is failing will help me tremendously.

    I've noticed I don't have an else statement to tell it what to do if it isn't one of the values defined by "x," figure that might be part of the problem . . . and it keeps asking me for a Do before Loop. I looked at the syntax and played with it for a while, still struggling. Any help or insights you can provide are greatly appreciated:

    Private Sub CommandButton2_Click()
    
    'Sort by Becky States
    
    '
    
    '
    
    Dim x As String
    
    Dim found As Boolean
    
     
    
    Range("D2").Select
    
    x = "SC or AL Or AR Or MS Or AZ Or TN Or GA Or TX Or IL"
    
    found = False
    
     
    
     
    
    Do Until IsEmpty(ActiveCell)
    
    If ActiveCell.Value = x Then
    
    found = True
    
    Exit Do
    
    ActiveCell.Offset(1, 0).Select
    
    If found = True Then
    
    Range(ActiveCell & ":" & ActiveCell.Offset(0, -4)).Select
    
    Selection.Cut
    
    Sheets("sheet3").Activate
    
    Sheets("sheet3").Range(Rows.Count).End(xlUp).Offset(1).Select
    
    Selection.PasteSpecial xlPasteValues
    
    Else: If ActiveCell.Value <> x Then found = False
    
     
    
    End If
    
    Loop
    
     
    
    End Sub

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Expected Do before loop (compile error)

    First, the If statement isn't that clever to interpret that you mean to check for SC or AL or AR, etc.

    What this code does is really checking whether the active cell has this entire string content -> "SC or AL Or AR Or MS Or AZ Or TN Or GA Or TX Or IL"
    If ActiveCell.Value = x Then
    Maybe you can try this:
    Do Until IsEmpty(ActiveCell)
        
        Select Case ActiveCell.Value
            Case "SC", "AL", "AR", "MS", "AZ", "TN", "GA", "TX", "IL"
                ActiveCell.Offset(0, -4).Cut
                Sheets("sheet3").Range("A" & rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End Select
        
        ActiveCell.Offset(1, 0).Select
    Loop

  3. #3
    Registered User
    Join Date
    07-14-2013
    Location
    Grand Rapids, Michigan
    MS-Off Ver
    Excel 2010
    Posts
    35

    Re: Expected Do before loop (compile error)

    I tried that string of code with a couple of modifications (see below), and it gave me a paste range error (runtime error 1004, paste special method of range class failed) . . . I tried a couple of different things, including activating sheet 3, and redefining range, etc., but could not get it to work. I have a work around that involves generating SQL code in another database storage program and then exporting the code to SAS (another SQL) program. But, SQL and the programs I'll be using are WAY more foreign to me and, although they remove some limitations, they make me nervous b/c my big bosses NEED the data to be correct (obviously) and I really don't trust myself considering my own personal limitations.

    Here's the code I tried, see if you think there's an easy fix. Either way, I appreciate you helping. I had a response to this string quite a bit sooner than I had suspected I would. With the boolean, I think I might just need an else / false statement (if returned result does not match the desired criteria then move to the next cell). I don't know, the more I think about it, the more lost I get. I didn't know if I needed the stuff I already had in the original equation to appear above your code, or if I would be able to omit it.

    Private Sub CommandButton2_Click()
    
    'Sort by Becky States
    
    '
    
    '
    
    Dim x As String
    
    Dim found As Boolean
    
     
    
    Range("D2").Select
    
    x = "SC or AL Or AR Or MS Or AZ Or TN Or GA Or TX Or IL"
    
    found = False
    
     
    
     
    
    Do Until IsEmpty(ActiveCell)
    
       
    
        Select Case ActiveCell.Value
    
            Case "SC", "AL", "AR", "MS", "AZ", "TN", "GA", "TX", "IL"
    
                ActiveCell.Offset(0, -3).Select
    
                Selection.Cut
    
                Sheets("sheet3").Activate
    
                Sheets("sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1).Select
    
                ActiveCell.PasteSpecial Paste:=xlPasteValues
    
          
    
        End Select
    
       
    
        ActiveCell.Offset(1, 0).Select
    
    Loop
    
     
    
     
    
    End Sub

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Expected Do before loop (compile error)

    If it's giving a paste error, maybe you need to tell us more what are you trying to cut and paste. I re-read your first post and it seems you wanted 4 columns of data including that active cell, initially I thought you only wanted that cell that is 4 columns to the left of the active cell.

    Try this:
    Do Until IsEmpty(ActiveCell)
        
        Select Case ActiveCell.Value
            Case "SC", "AL", "AR", "MS", "AZ", "TN", "GA", "TX", "IL"
                Range(ActiveCell.Offset(0, -4).address & ":" & ActiveCell.address).Copy Sheets("sheet3").Range("A" & rows.Count).End(xlUp).Offset(1)
                ActiveCell.EntireRow.Delete Shift:=xlUp
        End Select
        
        ActiveCell.Offset(1, 0).Select
    Loop

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Expected Do before loop (compile error)

    Hi, Rokn,

    maybe give this code a try (when deleting rows I start at the bottom an work up towards the top line):
    Private Sub CommandButton2_Click()
    
    'Sort by Becky States
    '
    '
    
    Dim lngCounter As Long
     
    For lngCounter = Range("D" & Rows.Count).End(xlUp).Row To 2 Step -1
      With Cells(lngCounter, "D")
        Select Case .Value
          Case "SC", "AL", "AR", "MS", "AZ", "TN", "GA", "TX", "IL"
            .Offset(0, -3).Resize(1, 4).Copy Sheets("sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1)
            .EntireRow.Delete Shift:=xlUp
        End Select
      End With
    Next lngCounter
    
    End Sub
    @millz:
    being in Column D an Offset of 4 columns should raise an error.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Registered User
    Join Date
    07-14-2013
    Location
    Grand Rapids, Michigan
    MS-Off Ver
    Excel 2010
    Posts
    35

    Re: Expected Do before loop (compile error)

    So, this one goes to Millz, although the code needed slight amending:

    Private Sub CommandButton1_Click()
    
    'Sort by Becky States
    
    '
    
    '
    
    Sheets("Sheet2").Range("D2").Select
    
    Do Until IsEmpty(ActiveCell)
    
        
    
        Select Case ActiveCell.Value
    
            Case "SC", "AL", "AR", "MS", "AZ", "TN", "GA", "TX", "IL"
    
                Range(ActiveCell.Offset(0, -3).Address & ":" & ActiveCell.Address).Copy Sheets("sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1)
    
                ActiveCell.EntireRow.Delete Shift:=xlUp
    
        End Select
    
        
    
        ActiveCell.Offset(1, 0).Select
    
    Loop
    
     
    
     
    
    End Sub
    This one is solved. You cats just saved me some serious time at work trying to figure this one out. Thanks again for all the assistance!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Compile error: Expected End Sub
    By MacroCoder in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-06-2013, 04:05 PM
  2. HELP Getting a Compile Error: Expected End With
    By turtles62 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-27-2012, 11:27 PM
  3. VBA Compile Error: Expected End With
    By MLS Packer Lover in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-24-2011, 02:58 AM
  4. Compile Error: Expected End Sub
    By Dcauth in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-31-2011, 01:56 PM
  5. Compile error: Expected End Sub
    By Leeboy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-31-2011, 09:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1