+ Reply to Thread
Results 1 to 5 of 5

error:Statement invalid outside type block

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2010
    Location
    NY,USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    error:Statement invalid outside type block

    I am trying to sort a number of columns in my worksheet based on the Cell Color. The columns have variable length of rows.
    I have two questions:
    1. When I run my current code in debug mode then I get the error: "Statement invalid outside type block". I am not sure which parantheses I should remove to get rid of the error.
    2.Ssecondly I think my current code is not going to do the sort that I would like it to do. Can anyone please suggest what changes I should make in my code to get it working.

    Below I have pasted a sample of my sheet as a link:
    http://picasaweb.google.com/manishab...14238835510658

    
    Sub Macro2()
    '
    ' Macro2 Macro
    '
    ' define the col range
    '
        
        For Each currentHeaderColumn In Worksheets("Sheet1").Range("D1", Selection.End(xlEnd))
            Debug.Print "Currently processing Column:" & vbTab & currentHeaderColumn.Column
            Dim r As Range
            Dim ca As String
             ca = Cells(2, currentHeaderColumn.Column).Address
            Set r = Range(ca).End(xlDown)
            Debug.Print "Total number of rows:" & vbTab & r.Row
            
            
            myRange As Range
            Set myRange = Worksheets("Sheet1").Range(ca, Cells(2, r.Row).Address)
            For Each currentCellInCurrentColRange In myRange
            ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range(myRange), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
            , 0)
                Debug.Print currentCellInCurrentColRange.Value
           Next currentCellInCurrentColRange
        Next currentHeaderColumn

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: error:Statement invalid outside type block

    1. Which line gives you the error?
    2. Why post a picture, rather than a sample workbook? It would also help if you explained why you think the sort won't do what you want, as well as what you actually do want.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    06-04-2010
    Location
    NY,USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: error:Statement invalid outside type block

    Quote Originally Posted by romperstomper View Post
    1. Which line gives you the error?
    2. Why post a picture, rather than a sample workbook? It would also help if you explained why you think the sort won't do what you want, as well as what you actually do want.

    Thanks romperstomper for replying.

    1. I was able to solve the first problem of the error: Statement invalid outside type block just a few minutes back. I had forgotten to add Dim infront of myRange

    2. I need help with the second part that is sort the rows in each column according to color such that the Red colored cells are on the top and the blue colored on the bottom for each column.

    I pasted a link of the picture of the worksheet to give an example of the format of the data I am dealing with. This time I attach the worksheet to this post for convinience of the readers.

    below is also the edited code that doesnot throw the Statement invalid outside type block error
    Sub Macro2()
    '
    ' Macro2 Macro
    '
    ' define the col range
    
        For Each currentHeaderColumn In Worksheets("Sheet1").Range("D1", Selection.End(xlEnd))
            Debug.Print "Currently processing Column:" & vbTab & currentHeaderColumn.Column
            Dim r As Range
            Dim ca As String
            ca = Cells(2, currentHeaderColumn.Column).Address
            Set r = Range(ca).End(xlDown)
            Debug.Print "Total number of rows:" & vbTab & r.Row
    '       my $ @ %
            Dim myRange As Range ' this was the line that gave the errror before
            Set myRange = Worksheets("Sheet1").Range(ca, Cells(2, r.Row).Address)
            For Each currentCellInCurrentColRange In myRange
           
            ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range(myRange), _
            xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
            , 0)
           
            
                Debug.Print currentCellInCurrentColRange.Value
           Next currentCellInCurrentColRange
        Next currentHeaderColumn
        
    
      
      End Sub
    Attached Files Attached Files

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: error:Statement invalid outside type block

    Something like this?
    
        Dim rng As Range
        Dim n As Long
        Dim wks As Worksheet
        Set wks = ActiveSheet
        For n = 1 To 6
            Set rng = Range(Cells(2, n), Cells(Rows.Count, n).End(xlUp))
            With wks.Sort
                With .SortFields
                    .Clear
                    .Add(rng, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
                    , 0)
                    .Add(rng, xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
                    , 240)
                End With
                .SetRange rng.Offset(-1).Resize(rng.Count + 1)
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        Next n

  5. #5
    Registered User
    Join Date
    06-04-2010
    Location
    NY,USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: error:Statement invalid outside type block

    thanks a lot!!!!! It worked fine. thanks for your help. My code was quite complicated.


    Quote Originally Posted by romperstomper View Post
    Something like this?
    
        Dim rng As Range
        Dim n As Long
        Dim wks As Worksheet
        Set wks = ActiveSheet
        For n = 1 To 6
            Set rng = Range(Cells(2, n), Cells(Rows.Count, n).End(xlUp))
            With wks.Sort
                With .SortFields
                    .Clear
                    .Add(rng, xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
                    , 0)
                    .Add(rng, xlSortOnCellColor, xlDescending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
                    , 240)
                End With
                .SetRange rng.Offset(-1).Resize(rng.Count + 1)
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        Next n

+ 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