+ Reply to Thread
Results 1 to 4 of 4

Complie error: End If without If

Hybrid View

Seler Naciowy Complie error: End If without... 11-28-2008, 01:26 PM
VBA Noob This should get you started ... 11-28-2008, 01:31 PM
solnajeff Compile error 11-28-2008, 01:35 PM
Seler Naciowy Thanks! 12-01-2008, 06:49 AM
  1. #1
    Registered User
    Join Date
    11-27-2008
    Location
    Everywhere
    Posts
    11

    Complie error: End If without If

    Hi,

    I'm writing a bit of a code that will re-format all sheets in spreadsheet except the three named. I keep on getting compile errors whatever I do. It's driven me mad.

    Could you please have a look at this and tell me what the heck is wrong?

    Sub formatsandsortall()
     Dim ws As Worksheet
     
     For Each ws In Worksheets
        If ActiveSheet.Name <> "name1" And ActiveSheet.Name <> "name2" And ActiveSheet.Name <> "name3" Then _
        Worksheets("ws").Select
        ActiveSheet.Cells.Select
        Selection.ClearFormats
        Selection.NumberFormat = "@"
        Columns("A:K").Select
        ActiveSheet.Sort.SortFields.clear
        ActiveSheet.Sort.SortFields.Add Key:=Range( _
            "A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        With ActiveSheet.Sort
            .SetRange Range("A:K")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        End If
       Next ws
    
     End Sub
    Thanks a million!
    Last edited by Seler Naciowy; 12-01-2008 at 07:54 AM.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    This should get you started

    Need to change activesheet to ws

    For Each ws In Worksheets
        If ws.Name <> "name1" And ws.Name <> "name2" And ws.Name <> "name3" Then _
        With ws
            .UsedRange.ClearFormats
            .UsedRange.NumberFormat = "@"
        End With
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Forum Contributor
    Join Date
    04-21-2007
    Location
    Lima, Peru
    MS-Off Ver
    2000, 2007, 2010
    Posts
    674

    Compile error

    Hi

    The error is in the following line

    If ActiveSheet.Name <> "name1" And ActiveSheet.Name <> "name2" And ActiveSheet.Name <> "name3" Then
            ws.Select
    There should not be an underscore as the select statement should be on the next line, also should be just ws.select.

    Regards

    Jeff

  4. #4
    Registered User
    Join Date
    11-27-2008
    Location
    Everywhere
    Posts
    11

    Thanks!

    Thanks, this was very helpful.
    The solution was the combination of the two answers.
    Sub formatsandsortall()
     Dim ws As Worksheet
    
    For Each ws In Worksheets
        If ws.Name <> "name1" And ws.Name <> "name2" And ws.Name <> "name3" Then
        ws.Activate
        ActiveSheet.Cells.Select 'for some reason wouldn't accept ws.Cells.Select...
        Selection.ClearFormats
        Selection.NumberFormat = "@"
        Columns("A:K").Select
        ws.Sort.SortFields.clear
        ws.Sort.SortFields.Add Key:=Range( _
            "A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        With ws.Sort
            .SetRange Range("A:K")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        End If
       Next ws
    
     End Sub
    Thanks a million for your help!

    Seler
    Last edited by Seler Naciowy; 12-01-2008 at 07:01 AM. Reason: skipped a row of code

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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