2017-05-26 67 views
0

新的VBA的种类,所以感谢任何帮助,我试图将数据从一个工作表中的行复制到另一个,删除空行和排序列中的数据“V”从最大到最小。复制和粘贴很好,但是当我排序时,它将“Winning”工作表顶部的空行和底部的已排序数据留空。复制部分行从一个表到另一个,然后排序结果

排序前:

enter image description here

Sub CreateListOfTeams() 

'Copy the data 
Sheets("Team").Range("AB4:AW301").Copy 
'Activate the destination worksheet 
Sheets("Winning").Activate 
'Select the target range 
Range("A2").Select 
'Paste in the target destination 
ActiveSheet.PasteSpecial xlPasteValuesAndNumberFormats 

Application.CutCopyMode = False 

Dim lastrow As Long 
lastrow = Cells(Rows.Count, 2).End(xlUp).Row 
Range("A1:V" & lastrow).Sort key1:=Range("V1:V" & lastrow), _ 
order1:=xlDescending, Header:=xlYes 

End Sub 

排序与上面的代码之后:

enter image description here

+1

在你的代码中你删除了空白行吗?因为你不是,所以在过滤时,你会得到顶部的空白行 –

+0

,我期待这种排序将空白行放在排序的最底部,而将数据留在最上面。 –

回答

0

注意:如果您使用Activate表望而却步(它的更好“获胜”)和后来的Select r安格。

Option Explicit 

Sub CreateListOfTeams() 

'Copy data 
Sheets("Team").Range("AB4:AW301").Copy 

With Sheets("Winning") 
    .Range("A2").PasteSpecial xlPasteValuesAndNumberFormats 

    Application.CutCopyMode = False 

    Dim LastRow As Long 
    Dim Rng As Range, i As Long 

    LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row 
    Application.ScreenUpdating = False 
    ' clear all blank rows 
    For i = LastRow To 2 Step -1 
     If WorksheetFunction.CountA(.Range("A" & i & ":V" & i)) = 0 Then .Rows(i).Delete 
    Next i 
    Application.ScreenUpdating = True 

    LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row ' find last row after removal of blank rows 
    Set Rng = .Range("A1:V" & LastRow) ' set the filter range 
    Rng.Sort key1:=.Range("V1:V" & LastRow), _ 
      order1:=xlDescending, Header:=xlYes 
End With 

End Sub  
+0

谢谢Shai,数据是团队是通过公式创建的,它正在查看一个以3人队为队伍的列表,然后在每个洞上取3分中的最高2分,并给出团队总数有一个完整数据在这里。 https://i.stack.imgur.com/Y2VPa.png当我尝试你提供的代码时说“找不到单元格” –

+0

@LHarnett尝试修改后的编辑代码 –

+0

谢谢@ShaiRado –

相关问题