+ Reply to Thread
Results 1 to 9 of 9

End If without block If

  1. #1
    Registered User
    Join Date
    05-08-2013
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2011 mac
    Posts
    43

    End If without block If

    Part of a macro is below. There is an alternative block for 'PN. To get over the problem do I have to put "End if" after " ActiveSheet.Paste" then begin another "If" block? I'd prefer a method of putting both paragraphs within the 1 "IF", just for convenience/ brevity.

    'EN
    If Range("aaj1") = 1 Then
    Range("aai3:aai182").Select
    Selection.Copy
    Range("k3:aag182").Select
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    ActiveSheet.Paste

    Range("g3:aal182").Select
    ActiveWorkbook.Worksheets("NewData").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("NewData").Sort.SortFields.Add Key:=Range( _
    "aaj3:aaj182"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("NewData").Sort
    .SetRange Range("g3:AAL182")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply

    Range("aaj3:aaj32").Select
    Selection.Copy
    Range("k3:aag32").Select
    ActiveSheet.Paste
    End If
    Last edited by tays01s; 05-12-2013 at 02:13 AM.

  2. #2
    Registered User
    Join Date
    09-22-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003 & Excel 2007
    Posts
    36

    Re: End If without block If

    Next time I'd put the code in code-brackets, makes it a lot easier to read (besides that it is a forum rule).


    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Above is effectively the same, if you can put what follows after the if-statement is on the same line you can skip the closing of the if-statement. If you need more room you can apply a closing statement.
    'Ignoramus et ignorabimus'

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: End If without block If

    The problem is actually a missing End With that should go after .Apply.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    09-22-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2003 & Excel 2007
    Posts
    36

    Re: End If without block If

    I noticed that but that wasnt the question

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: End If without block If

    It is the answer to the problem though.

    If the OP really wants help in structuring the code I think we would need to see more of it.

  6. #6
    Registered User
    Join Date
    05-08-2013
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2011 mac
    Posts
    43

    Re: End If without block If

    I've not yet understood the answer. However, here's both alternatives of the 'If' together with their 'do stuff' component:

    'EN
    If Range("aaj1") = 1 Then
    Range("aai3:aai182").Select
    Selection.Copy
    Range("k3:aag182").Select
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    ActiveSheet.Paste

    Range("g3:aal182").Select
    ActiveWorkbook.Worksheets("NewData").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("NewData").Sort.SortFields.Add Key:=Range( _
    "aaj3:aaj182"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("NewData").Sort
    .SetRange Range("g3:AAL182")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply

    Range("aaj3:aaj32").Select
    Selection.Copy
    Range("k3:aag32").Select
    ActiveSheet.Paste
    End If

    'PN
    If Range("aaj1") = 0 Then
    Range("aak3:aak182").Select
    Selection.Copy
    Range("k3:aag182").Select
    Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    ActiveSheet.Paste

    Range("g3:aal182").Select
    ActiveWorkbook.Worksheets("NewData").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("NewData").Sort.SortFields.Add Key:=Range( _
    "aal3:aal182"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("NewData").Sort
    .SetRange Range("g3:AAL182")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply

    Range("aal3:aal32").Select
    Selection.Copy
    Range("k3:aag32").Select
    ActiveSheet.Paste
    End If

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    The problem is missing End Withs.

    For example, you open a With here but there's no End With later in the code to close it.

    The End With should be immediately after .Apply.
    Please Login or Register  to view this content.
    PS Please add code tags, it makes the code a lot easier to read and it's kind of a forum rule.

  8. #8
    Registered User
    Join Date
    05-08-2013
    Location
    Bristol, UK
    MS-Off Ver
    Excel 2011 mac
    Posts
    43

    Re: End If without block If

    So for the above it should have ended with...


    ActiveSheet.Paste
    End With
    End If

    [Correct?]

    - Apologies for ignorance; what's a code tag?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: End If without block If

    No like this.
    Please Login or Register  to view this content.

+ 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