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
Bookmarks