+ Reply to Thread
Results 1 to 9 of 9

Sort Problem

Hybrid View

  1. #1
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562

    Sort Problem

    I currently have the code listed below

    Windows("Call List.xls").activate
    Sheets("Call List").Select
    
    Columns("A:D").Select
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
    Thanks to the help of this list I am getting away from the activate and select statements, but I have been unsuccessful at getting this one rewritten. Using the set statement below how would I do the sort listed in the code above.

    Set wsCallList = Workbooks("Call List.xls").Sheets("Call List")
    Thanks for the help!

    Jeff

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Jeff

    Just merge the worksheet variable, column range & the sort commands all into 1 continous command

    Set wsCallList = Workbooks("Call List.xls").Sheets("Call List")
    
    wsCallList.Columns("A:D").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    mudraker,

    I have tried what you have listed, but it keeps throwing me into my error handler. I get Error #1004.

    Jeff

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Have you declared the wsCallList variable?
    Double check spelling of workbook & worksheet names

    Dim wsCallList As Worksheet
    If this does not fix the problem can you post a copy of your workbook or at least a full copy of the macro

  5. #5
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Every appears to be spelled correctly and I have declared wsCallList as a Worksheet. Below is the code I am using in the subroutine.

    Option Explicit
    Dim sAreaCode As String
    Dim sPhoneNumber As String
    Dim sFullNumber As String
    Dim sOldFullNumber As String
    Dim sTimeZone As String
    Dim sState As String
    Dim lDataRow As Long
    Dim lErrorRow As Long
    Dim lTargetRow As Long
    Dim lLastRow As Long
    Dim bWBOpen As Boolean
    Dim wsCallData As Worksheet
    Dim wsCallList As Worksheet
    Dim wsCallErrors As Worksheet
    Dim wsAreaCodes As Worksheet
    Dim rAreaCodes As Range
    
    Sub JoinNumbers()
    
    bWBOpen = WorkbookIsOpen("Area Code List.xls")
    If bWBOpen <> True Then
        Workbooks.Open Filename:="C:\John\Area Code List.xls"
    End If
    
    lDataRow = 2
    lErrorRow = 1
    lTargetRow = 2
    
    Set wsCallData = Workbooks("Call List.xls").Sheets("Data")
    Set wsCallList = Workbooks("Call List.xls").Sheets("Call List")
    Set wsCallErrors = Workbooks("Call List.xls").Sheets("Errors")
    Set wsAreaCodes = Workbooks("Area Code List.xls").Sheets("List")
    
    wsCallData.Columns("A:C").NumberFormat = "@"
    
    sAreaCode = wsCallData.Cells(lDataRow, 1)
    sPhoneNumber = wsCallData.Cells(lDataRow, 2)
    
    Application.ScreenUpdating = False
    Do While Len(sAreaCode) <> 0
        
        On Error GoTo ErrorHandler
        
        sFullNumber = sAreaCode + sPhoneNumber
    
        Windows("Area Code List.xls").Activate
        Sheets("List").Select
        Range("A1").Select
        
        Set rAreaCodes = wsAreaCodes.Cells.Find(What:=sAreaCode, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False)
    
        If Not rAreaCodes Is Nothing Then
            sState = rAreaCodes.Offset(0, 1).Value
            sTimeZone = rAreaCodes.Offset(0, 3).Value
        Else
            wsCallErrors.Cells(lErrorRow, 1) = sAreaCode
            wsCallErrors.Cells(lErrorRow, 2) = sFullNumber
            wsCallErrors.Cells(lErrorRow, 3) = "Area Code not found in list"
            lErrorRow = lErrorRow + 1
            GoTo 50
        End If
        
        wsCallList.Cells(lTargetRow, 1) = sFullNumber
        wsCallList.Cells(lTargetRow, 2) = sState
        wsCallList.Cells(lTargetRow, 3) = sTimeZone
        wsCallList.Cells(lTargetRow, 4) = lTargetRow
        lTargetRow = lTargetRow + 1
        
    50  lDataRow = lDataRow + 1
        
        sAreaCode = wsCallData.Cells(lDataRow, 1)
        sPhoneNumber = wsCallData.Cells(lDataRow, 2)
    
    Loop
    Application.ScreenUpdating = True
    
    Windows("Area Code List.xls").Close
    
    wsCallList.Columns("A:D").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers
    
    
    
    Set wsCallData = Nothing
    Set wsCallList = Nothing
    Set wsCallErrors = Nothing
    Set wsAreaCodes = Nothing
    
    Exit Sub        ' Exit to avoid handler.
    ErrorHandler:    ' Error-handling routine.
        
    End Sub
    Thanks for the help!

    Jeff

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    I think I got it this time

    In the previous code range("a2") refered to the activesheet which I beleive was not wsCallList.
    With the sortkey range being outside the sort range you will get an error.

    This should fix it
    wsCallList.Columns("A:D").Sort Key1:=wsCallList.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortTextAsNumbers

+ 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