kschindl
kschindl

Reputation: 233

VBA - Descending Sort Works Only When Stepping Through Code

I'm trying to sort a column in descending order. The code works just fine when I step through it, but when the macro is run in full, the data is not sorted. I cannot figure out why this is happening. Here's the bit of code:

Dim temp_str as String
Dim lrow as Long
Dim DOX_Net_Lvl_Col_Loc as Long
Dim DOX_ws as WorkSheet
Set DOX_ws = ThisWorkbook.Sheets("DOX")

' Sort Netting Level in descending order
With DOX_ws
    .Activate
    temp_str = ConvertToLetter(DOX_Net_Lvl_Col_Loc)
    .Range(temp_str & "1:" & temp_str & lrow).Sort _
       Key1:=.Range(temp_str & "1"), Order1:=xlDescending
End With

Function ConvertToLetter(ColNo As Long) As String
    ConvertToLetter = Split(Cells(, ColNo).Address, "$")(1)
End Function

Obviously, this is just a snippet of the overall code. I've already confirmed that lrow and temp_str are correctly calculated. Also, when testing the code, I confirmed that .Range(temp_str & "1:" & temp_str & lrow).Select grabs the full range I want to sort. What could prevent the code from working during a full run of the macro?

To give a bit more context, the sub (let's call it "DOX") to populate the file/sort the column is only called from a different sub (let's call it "Master"). When I run the DOX sub by itself, the sort works perfectly. I'm using Excel 2010.

Upvotes: 1

Views: 340

Answers (1)

kschindl
kschindl

Reputation: 233

I figured out a solution. If I change the range to be .Range("A1:" & temp_str & lrow).Sort, then it works. I guess it needed the whole range of the data to perform the sort; just giving it the column wasn't enough, evidently.

Upvotes: 2

Related Questions