+ Reply to Thread
Results 1 to 1 of 1

Thread: "Statement invalid outside Type block" error

  1. #1
    Registered User
    Join Date
    11-14-2011
    Location
    fairfax, virginia
    MS-Off Ver
    Excel 2007
    Posts
    6

    "Statement invalid outside Type block" error

    hi all,
    I'm working on a macro that generates, sorts, and subtitles a small list using data from a larger database. the purpose is to help with the creation of plant lists for clients without having to copy or paste or delete. My boss isn't very tech saavy, and up until now has been deleting the data she doesn't use each time she creates a new list. i've managed to piece together this macro with what little knowledge i possess but it's been showing errors from time to time.

    the goal:
    • copy all rows on "sheet 1" (after row 2) that contain data in column "B" and paste them into "sheet 2" starting at row 2.
    • sort the newly copied data by type listed in golumn "G" using custom sort (Trees, Shrubs, Grass/Sedge, Perennial, Vine, Bulb)
    • Locate the first instance of each type (tree, shrub, ect..) listed in column "G", and insert 2 rows above each one, with the row directly above subtitling the plant type in column "C"
    • finally, now that each item in column G has a header, i want to delete all of the data in column G with the exception of cell G1 which i want to say "Spacing and Notes"

    Here's the code that i have so far, but some of it keeps getting an error message.

    Sub GeneratePlantlist()
        
        Application.ScreenUpdating = False
        Selection.AutoFilter
        
        Sheets("Sheet2").Select
        Range("A2:G714").Select
        Selection.ClearContents
        
        With Sheet1
            .Range("B:B").AutoFilter Field:=1, Criteria1:="<>"
            .UsedRange.Offset(2).SpecialCells(xlCellTypeVisible).EntireRow.Copy Worksheets("Sheet2").Cells(2, 1)
            If .AutoFilterMode Then .AutoFilterMode = False
     
            organize
            
        End With
        Columns("G:G").Select
        Selection.ClearContents
        Range("G1").Select
        ActiveCell.FormulaR1C1 = "Spacing/Notes"
        Sheets("Sheet1").Select
        Selection.AutoFilter
        Sheets("Sheet2").Select
        Application.ScreenUpdating = True
    
        MsgBox "All matching data has been copied."
        
    End Sub
    Sub Italics()
    
        Columns("C:C").EntireColumn.Select
        Selection.Font.Italic = True
    End Sub
    Sub organize()
    
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.clear
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("G2:G4"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            "Tree,Shrub,Grass/Sedge,Perennial,Vines,Bulb", DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("C2:C4"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet2").Sort
            .SetRange Range("A1:G4")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        trythis
    End Sub
    Sub trythis()
        Dim myRow As Long
        Dim myCount As Long
        Dim myCol As Integer
    
        myCol = 3
        myCount = ActiveSheet.UsedRange.Rows.Count
    
        For myRow = myCount - 1 To 1 Step -1
        If Cells(myRow, myCol).Value <> Cells(myRow + 1, myCol).Value Then
        Range(Cells(myRow + 1, myCol), Cells(myRow + 2, myCol)).EntireRow.Insert
        Range(Cells(myRow, myCol), Cells(myRow, myCol)) As Range.Select.Copy
        Range(Cells(myRow + 1, myCol - 3), Cells(myRow + 1, myCol)) As Range.Select.Paste
        End If
    Next myRow
    End Sub
    any thoughts or suggestions? thank you in advance

    Elo
    Last edited by theinexplicablefuzz; 11-15-2011 at 04:14 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0