+ Reply to Thread
Results 1 to 4 of 4

Error 1004 using Column variable in Sorting

  1. #1
    Registered User
    Join Date
    12-01-2009
    Location
    Iowa, United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Error 1004 using Column variable in Sorting

    I get error 1004: Method Range of Object Global Failed or other associated messages when I try to use an array of column numbers as variables in sorting or otherwise selecting a range. I sort on two criteria, the first is a constant column. The total number of rows of data is held in the variable SnapShot. Here's my code...help?

    'create arrays to iterate through sorting columns ascending or descending
    Dim AscArray As Variant
    AscArray = Array(7, 8, 12, 13, 18, 19, 22)
    Dim DescArray As Variant
    DescArray = Array(6, 9, 10, 11, 14, 15, 16, 17, 20, 21, 23, 24)
    Dim ColumnNumber As Variant

    'iterate through Ascending array

    For Each ColumnNumber In AscArray

    'sort by column
    ActiveWorkbook.Worksheets("Rankings").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Rankings").Sort.SortFields.Add Key:=Range("A2:A" & SnapShot _
    ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Rankings").Sort.SortFields.Add Key:=Range(Cells(ColumnNumber, 2), Cells(ColumnNumber, SnapShot) _
    ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Rankings").Sort
    .SetRange Range("A1:AC" & SnapShot)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    'highlight best value and worst value green and red
    Range(Cells(ColumnNumber, 2)).Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 5296274
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With

    Range(Cells(ColumnNumber, SnapShot)).Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With

    Next

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Error 1004 using Column variable in Sorting...help!!!

    Hi Kristal_Miller,

    please put your code between code tags like this: [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code]. That will make the code easier to read and copy. HTML otherwise strips out indentation and consecutive spaces.

    While you're waiting for a reply, you may want to check the forum rules to find out how to post properly and get brilliant results!

    cheers

  3. #3
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Error 1004 using Column variable in Sorting...help!!!

    Hi Kristal,

    As Teylyn mentioned, please take a moment to read the forum rules and then edit your post to wrap your code using code tags (the "#" symbol in the post 'Advanced' editor).

    Until that is done, other users on the forum won't respond.

    Thanks!

  4. #4
    Registered User
    Join Date
    12-01-2009
    Location
    Iowa, United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Error 1004 using Column variable in Sorting

    Thanks for the response and I'll be sure to do that next time.

    I figured this one out, but thanks anyway. Here's the code that worked. (I had set a variable names ws as the active worksheet and forgot to replace it with the full workbook.worksheet reference, but it worked...go figure!)

    Please Login or Register  to view this content.
    K
    Last edited by Kristal_Miller; 12-02-2009 at 12:40 AM. Reason: Forgot to include the solution

+ 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