Consulting

Results 1 to 5 of 5

Thread: Run-time error 1004

  1. #1

    Run-time error 1004

    Hi,

    This is a super basic error but I am trying to sort a column from A to Z from top to bottom.

    Here is the code below:

    Worksheets("information Sort sheet").Range("A:A").Sort key1:=Range("A:A"), order1:=xlAscending

    Is there something of which I am missing here?

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Specify a single cell as the Key
    Worksheets("information Sort sheet").Range("A:A").Sort key1:=Range("A1"), order1:=xlAscending
    Then try this
    Worksheets("information Sort sheet").Range("A:A").Sort Order1:=xlAscending
    Also check the spelling and capitalizationof the Sheet name. Look for leading and trailing spaces.

    I often run this Macro when developing a Workbook Project
    Sub TrimSheetNames()
    Dim Sht As Object
    
    For Each Sht In ActiveWorkbook.Sheets
      Sht.Name = Trim(Sht.Name)
    Next
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Worksheets("information Sort sheet").Range("A:A").Sort key1:=Cells(1, 1), order1:=xlAscending

    I am still getting the same area with the code above.

  4. #4
    Apologies. I mistyped, I meant to type "error" as opposed to "area". Thanks

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Lots of times I'll record a macro that does what I want, and then clean it up and generalize it


    Option Explicit
    Sub Macro1_AsRecorded()
        Range("A1").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A23") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A1:A23")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    
    
    Sub Macro1_Generalized()
        Dim r As Range, r1 As Range
        
        With ActiveWorkbook.Worksheets("Sheet1")
            Set r = .Cells(1, 1).CurrentRegion.Columns(1)
            Set r1 = r.Cells(2, 1).Resize(r.Rows.Count - 1, r.Columns.Count)
            
            With .Sort
                .SortFields.Clear
                .SortFields.Add Key:=r1, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                .SetRange r
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •