2015-10-15 52 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 
相關問題