首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EXCEL如何从范围内取消选择列?

EXCEL如何从范围内取消选择列?
EN

Stack Overflow用户
提问于 2017-04-13 10:39:07
回答 2查看 536关注 0票数 1

我编写了一个宏,它对我的表进行排序,并删除重复的行,如下所示:

代码语言:javascript
复制
Entitydocnum    Docstatus    Purchase-order   Created-date   Eyepeak
====================================================================== 
test1           pending          EL351-EE      27/03/2017       2
test2           pending          EL351-EE      06/04/2017       0
test1           pending          EL351-EE      30/03/2017       0
test4           pending          EL351-EE      25/03/2017       2

正如您所看到的,'test1‘行是重复的,因为the认为它因为日期不同而不同,这是不一样的。2017年3月30日有一行“test1”,另一行是2017年3月27日。

如何使宏忽略创建的列-date(只有此列)将test1 (27/03/2017)与test1 (30/03/2017)合并。其中的日期值较高..。?

此时此刻,我的宏是:

(我的桌子从“B3”开始)

代码语言:javascript
复制
Sub thepcshop_macrotest()

ActiveSheet.Range("B3").Sort _
        Key1:=ActiveSheet.Columns("B"), _
        Header:=xlGuess
Do While Not IsEmpty(ActiveCell)                ' Tant que la cellule active n'est pas vide, recommence
    If ActiveCell = ActiveCell.Offset(-1, 0) Then   ' Condition : si la cellule active est identique
        ActiveCell.EntireRow.Delete                 ' ˆ la cellule prŽcŽdente (mme colonne), supprime
    Else: ActiveCell.Offset(1, 0).Select        'toute la ligne. Sinon, passe ˆ la cellule suivante.
    End If
Loop
MsgBox "Done :)"

End Sub
EN

回答 2

Stack Overflow用户

发布于 2017-04-13 12:24:10

您可以将数据按降序排序,然后根据前三列删除重复项。

代码语言:javascript
复制
Sub thepcshop_macrotest()

    Dim rData As Range 'Whole data range
    Dim rDocNum As Range 'EntityDocNum range
    Dim rCreated As Range 'Created-date range


    With ThisWorkbook.Worksheets("Sheet1") 'Sheet name will need updating.
        'Reference required data ranges - many ways of doing this.
        'This method will work if there's nothing else on sheet.
        Set rData = .Range(.Cells(Rows.Count, 2).End(xlUp), .Cells(3, Columns.Count).End(xlToLeft))
        Set rDocNum = .Range(.Cells(4, 2), .Cells(Rows.Count, 2).End(xlUp))
        Set rCreated = .Range(.Cells(4, 5), .Cells(Rows.Count, 5).End(xlUp))

        'Sort by DocNum ascending and Created date descending.
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=rDocNum, _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=rCreated, _
                             SortOn:=xlSortOnValues, _
                             Order:=xlDescending, _
                             DataOption:=xlSortNormal
        With .Sort
            .SetRange rData
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

        'Remove duplicates based on EntityDocNum, DocStatus and Purchase-order.
        rData.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes

    End With
End Sub
票数 1
EN

Stack Overflow用户

发布于 2017-04-13 11:44:48

检查这段代码是否符合您的要求。

代码语言:javascript
复制
Sub thepcshop_macrotest()

    Dim Tmp As Variant

    ActiveSheet.Range("B3").Sort Key1:=ActiveSheet.Columns("B"), _
                                 Header:=xlGuess

    Do While Not IsEmpty(ActiveCell)                    ' Tant que la cellule active n'est pas vide, recommence
        With ActiveCell
            If .Value = .Offset(-1, 0).Value Then       ' Condition : si la cellule active est identique
                Tmp = .Offset(0, 3).Formula
                If Tmp <> .Offset(-1, 3).Value Then     ' i the previous is different
                    Tmp = Application.Max(Tmp, .Offset(-1, 3).Value)
                    ' replace the previous with the current if it is more recent
                    If Tmp < .Offset(-1, 3).Value Then .Offset(-1, 3).Value = Tmp
                End If
                .EntireRow.Delete                        ' ? la cellule pr?c?dente (mme colonne), supprime
            Else
                .Offset(1, 0).Select                    'toute la ligne. Sinon, passe ? la cellule suivante.
            End If
        End With
    Loop

    MsgBox "Done :)"
End Sub

使用简单的语言:如果选定的单元格与上面的单元格具有相同的值,它将检查D列中的日期。如果当前行中的日期是最近的,则将更改宝贵行中的日期。无论测试的结果如何,如果当前行在A列中的值与上面的行相同,则该行将被删除。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43390045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档