2015-10-15 62 views
0

Excel VBA:我想动态获取最后一列字符并将其作为最后一列传递,同时选择排序范围。但它似乎并没有在这里工作是代码动态获取最后一列字符并将其传递

Sub Sort_THAT_IS_NOT_CALLED_SORT_BECAUSE_THAT_IS_A_RESEVED_WORD() 
    Dim lastrowcheck As Long, n1 As Long, LastRowcheck1 As Long, n2 As Long 
    Dim lcol As Integer, colletter As String 

    With Worksheets("MergeSheet") 
     lastrowcheck = .Range("A" & .Rows.Count).End(xlUp).Row 
     For n1 = 2 To lastrowcheck 
      If .Cells(n1, 1).Value = "FUND" Then 
       .Rows(n1).Delete 
      End If 
     Next n1 

     ActiveWorkbook.Worksheets("MergeSheet").Sort.SortFields.Clear 
     ActiveWorkbook.Worksheets("MergeSheet").Sort.SortFields.Add Key:=Range(_ 
      "A2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ 
      xlSortNormal 
     With ActiveWorkbook.Worksheets("MergeSheet").Sort 
      lcol = Cells(1, Columns.Count).End(xlToLeft).Column 
      colletter = ConvertToLetter(lcol) 
      .SetRange Range("A2:colletter" & lastrowcheck) 
      .Header = xlNo 
      .MatchCase = False 
      .Orientation = xlTopToBottom 
      .SortMethod = xlPinYin 
      .Apply 
     End With 
    End With 
End Sub 
+0

您的代码表明您没有标题行,但您从第2行开始。您能否确认没有标题行? – Jeeped

回答

0

记录Range.Sort method是非常详细的。您通常可以将记录的代码削减为录制内容的一小部分,最终得到实际所需的内容。

Sub Sort_THAT_IS_NOT_CALLED_SORT_BECAUSE_THAT_IS_A_RESEVED_WORD2() 
    With Worksheets("MergeSheet") 
     With .Cells(2, 1).CurrentRegion 
      With .Resize(.Rows.Count + (.Rows(1).Row = 1), .Columns.Count).Offset(Abs(.Rows(1).Row = 1), 0) 
       .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _ 
          Orientation:=xlTopToBottom, Header:=xlNo, _ 
          DataOption:=xlSortNormal, MatchCase:=False 
     End With 
    End With 
End Sub 
相关问题