VBA, Loop troubleshooting and Compile Else If error

chee321

New Member
Joined
Mar 18, 2018
Messages
2
Hi guys,

This is my code as followed,

Code:
Sub Macro3()
' Macro3 Macro


    Sheets("MV filter").Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("MV-70%").Select
    Range("A4").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
        
    Sheets("MV-70%").Select
    Range("C4").Select
    Do Until IsEmpty(ActiveCell)
    If Range("C4") <> "" Then
         Sheets("MV-70%").Select
          Range("A4:C4").Select
            Range(Selection, Selection.End(xlDown)).Select
            Application.CutCopyMode = False
            ActiveWorkbook.Worksheets("MV-70%").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("MV-70%").Sort.SortFields.Add Key:=Range("C5:C2674" _
                ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                With ActiveWorkbook.Worksheets("MV-70%").Sort
                .SetRange Range("A4:C2674")
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
         Sheets("MV-70%").Select
         Range("A4:C4").Select
         Range(Selection, Selection.End(xlDown)).Select
         Selection.Copy
         Sheets("Sheet3").Select
         Range("A1").Select
         Selection.End(xlDown).Select
         ActiveCell.Offset(1, 0).Range("A1").Select
         Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
         False, Transpose:=True
         Sheets("MV-70%").Select
         Columns("C:C").Select
         Selection.Delete Shift:=xlToLeft
         Range("C4").Select
     Else
            Sheets("MV-70%").Select
            Range("A4").Select
            
        End With
      Application.ScreenUpdating = False
End Sub

There is a compile Else If error when I try to loop it. Not sure how to debug.
 
Last edited by a moderator:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi & welcome to MrExcel.
You have the End With in the wrong place & are missing an End If & Loop.
Try
Code:
Sub Macro3()
' Macro3 Macro

Application.ScreenUpdating = False
   Sheets("MV filter").Select
   Range("A1").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.Copy
   Sheets("MV-70%").Select
   Range("A4").Select
   Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
      False, Transpose:=True
   
   Sheets("MV-70%").Select
   Range("C4").Select
   Do Until IsEmpty(activecell)
      If Range("C4") <> "" Then
         Sheets("MV-70%").Select
         Range("A4:C4").Select
         Range(Selection, Selection.End(xlDown)).Select
         Application.CutCopyMode = False
         ActiveWorkbook.Worksheets("MV-70%").Sort.SortFields.Clear
         ActiveWorkbook.Worksheets("MV-70%").Sort.SortFields.Add Key:=Range("C5:C2674" _
            ), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
         With ActiveWorkbook.Worksheets("MV-70%").Sort
            .SetRange Range("A4:C2674")
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        [COLOR=#ff0000] End With[/COLOR]
         Sheets("MV-70%").Select
         Range("A4:C4").Select
         Range(Selection, Selection.End(xlDown)).Select
         Selection.Copy
         Sheets("Sheet3").Select
         Range("A1").Select
         Selection.End(xlDown).Select
         activecell.Offset(1, 0).Range("A1").Select
         Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=True
         Sheets("MV-70%").Select
         Columns("C:C").Select
         Selection.Delete shift:=xlToLeft
         Range("C4").Select
      Else
         Sheets("MV-70%").Select
         Range("A4").Select
      [COLOR=#ff0000]End If[/COLOR]
   [COLOR=#ff0000]Loop[/COLOR]
   Application.ScreenUpdating = True
   End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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