Results 1 to 5 of 5

Thread: selection.Sort error

  1. #1

    selection.Sort error

    Hello,
    Can someone tell me as to why microsoft VB seems to have a problem running the line "Selection.Sort Key1:=Range("h1"), ..." in the program below. However, the line of code runs stand alone. Please advise. Thank you.

    yours,
    Ed





    [vba]
    Sub Daily_Click()



    'copyAndPasteColumns
    '------------------------------------------------------
    '------------------------------------------------------
    Dim inData As Integer

    Data = Array(1, 3, 5, 7, 9, 11, 13, 24, 32, 33, 34, 35, 36, 37, 38, 39, 40, 44, 47, 51, 52, 53, 54, 0)
    inData = 0

    Sheets(1).Select
    ActiveSheet.Range("a1").Select
    Sheets(2).Select
    ActiveSheet.Range("a1").Select
    Do Until Data(inData) = 0
    ActiveSheet.Columns(Data(inData)).Copy
    Sheets(1).Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, 1).Select
    Sheets(2).Select
    'MsgBox Data(inData)
    inData = inData + 1
    Loop
    '------------------------------------------------------
    '------------------------------------------------------


    Sheets(1).Select
    ActiveSheet.Range("h1").Select
    Selection.Sort Key1:=Range("h1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom


    Sheets(1).Select
    ActiveSheet.Range("a1").Select
    ActiveCell.EntireRow.Insert
    Sheets(4).Select
    ActiveSheet.Range("a1").Select
    ActiveSheet.Rows(1).Copy
    Sheets(1).Select
    ActiveSheet.Range("a1").Select
    ActiveCell.EntireRow.Insert
    'ActiveSheet.Paste


    ActiveSheet.Columns("P:Q").Select
    Selection.Style = "percent"
    Selection.NumberFormat = "0.00%"
    ActiveWindow.DisplayGridlines = False
    ActiveSheet.Columns.Select
    ActiveSheet.Columns.AutoFit
    ActiveCell.Range("a1").Select
    ActiveWorkbook.Save


    End Sub[/vba]

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Ed

    What range do you actually want to sort?

    What's actually selected when the code errors?

    Where are you copying/pasting from/to?
    [vba]
    Option Explicit
    Sub Daily_Click()
    Dim inData As Long
    Dim arrData


    'copyAndPasteColumns
    '------------------------------------------------------
    '------------------------------------------------------

    arrData = Array(1, 3, 5, 7, 9, 11, 13, 24, 32, 33, 34, 35, _
    36, 37, 38, 39, 40, 44, 47, 51, 52, 53, 54)

    For inData = LBound(arrData) To UBound(arrData)
    Sheets(2).Columns(arrData(inData)).Copy Sheets(1).Range("A1").Offset(inData)
    Next

    With Sheets(1)
    .UsedRange.Sort Key1:=.Range("h1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    .Range("a1").EntireRow.Insert
    Sheets(4).Rows(1).Copy .Range("A1")
    End With

    ActiveCell.EntireRow.Insert
    With ActiveSheet ' not sure which sheet to use here, change ActiveSheet to Sheets(1) or whatever

    .Columns("P:Q").Style = "percent"
    .Columns("P:Q").NumberFormat = "0.00%"
    ActiveWindow.DisplayGridlines = False
    .Columns.Columns.AutoFit
    .Range("a1").Select
    End With

    ActiveWorkbook.Save

    End Sub[/vba]

  3. #3
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    not sure what your doing here either.....maybe I'm missing something:
    [VBA]
    Sheets(1).Select
    ActiveSheet.Range("a1").Select
    Sheets(2).Select
    ActiveSheet.Range("a1").Select

    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    thank you for the prompt replies.
    Steve:; I was only setting activecell to cell A1, its easier for me to know what i am do since i am such a beginner.
    Norie: the error occures at

    [vba]Selection.Sort Key1:=Range("h1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom [/VBA]

    HOwever, I will have a go at your codes and get back to you.

    Many thanks guys

    yours,
    Ed

  5. #5
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    somewhere in this loop(I think)you are removing the data from H1 in sheet1 so it doesn't have anything to act on.....

    [vba]
    Do Until Data(inData) = 0
    ActiveSheet.Columns(Data(inData)).Copy
    Sheets(1).Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, 1).Select
    Sheets(2).Select
    'MsgBox Data(inData)
    inData = inData + 1
    Loop
    [/vba]
    it does run stand alone if there is data in h1. When I ran your code from above that gives you an error...after the error there was no data in h1 sheet1 after the loop

    if you try to run the sort routine with no data in h1 sheet1 you will get the same error
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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