按值排序Excel列表

时间:2015-05-22 04:21:25

标签: excel vba excel-vba

我正在使用一个包含10列的文件,每列都有不同的数据。只有一列具有(相当)一致的数据。我将附上该文件,以便它有助​​于我正在尝试做的事情。

我希望将唯一值复制到另一个工作表。有多个ACD选项,但我只想复制ACD,如果它上面的单元格包含值RING。另外,我想复制第一个TIME列,如果可能的话,第二个。如果有必要,我将有一个宏将名称更改为第二列的长度。

Data File

我知道可以使用高级过滤器,但我还没有弄清楚如何正确设置它。

任何提示都将不胜感激!

1 个答案:

答案 0 :(得分:0)

这是你需要的吗?我对VBA很新,但它确实有效。

Sub CopyData()
Dim lastrow As Long, LocY As Long, lastcopy As Long
Dim source As Worksheet, dest As Worksheet

Set source = Worksheets("Sheet1")
Set dest = Worksheets("Sheet2")

lastrow = source.Cells(Rows.Count, 1).End(xlUp).Row 'Checks for the last row that contains data in the source
lastcopy = dest.Cells(Rows.Count, 1).End(xlUp).Row + 1 'Checks for the last row with data in the destination and move down by one to prime for data entry

For LocY = 2 To lastrow 'LocY referring to current location
    If source.Cells(LocY, 7).Value = "RING" Then 'Check if current locations at column 7 in source sheet contains the value RING
        source.Cells(LocY, 2).Copy 
        dest.Cells(lastcopy, 1).PasteSpecial xlPasteValues 'pastes the value into the last row of destination sheet. 
        source.Cells(LocY, 7).Copy
        dest.Cells(lastcopy, 2).PasteSpecial xlPasteValues
        source.Cells(LocY, 10).Copy
        dest.Cells(lastcopy, 3).PasteSpecial xlPasteValues
        lastcopy = lastcopy + 1 'moves the target row in the destination down by one to prime for data entry.
    End If
Next
End Sub