1
APC NM000038
APC NM000038
APC NM000038
APC NM000038
APC NM001127510
APC NM001127510
APC NM001127510

我试图在excel中计算重复数据。我找不到相关的答案,所以我只是在这里问。并感谢您提前回答。

我正在尝试使用两列信息来计算。如果A列中的值=某些标准,我想计算一个值出现在B列中的次数(不要计算B列中的相同值,只计算不同的值)-并非A列中的所有值都对应b 列值。因此,如果我想查看有多少女性获得了“5”,我需要查看 A 列中有多少“女性”在 B 列中有相应的“5”值。(并非所有女性都有“5”,值可以是 1 到 5)

在上面的例子中,我期望

NM001127510  2
NM000038     2
4

1 回答 1

3

Assuming that you data is in cell A1 to B1000, use this formula

=SUMPRODUCT((A1:A1000="APC")*(B1:B1000="NM000038"))

similarly

=SUMPRODUCT((A1:A1000="APC")*(B1:B1000="NM001127510"))

FOLLOWUP

I did that, but the data sets are huge, several hundred MB excel file. I m not sure whether i need to use vba to do that, cuz i dont know a lot about vba – Maggie Mi 7 hours ago

If you Excel Files are >= 100 mb then opening it and then running the VBA code or using formulas will take a lot of time. If you are just concerned with the results then try this :)

Open a new Excel File. Ensure your other file is closed. In a module paste this code. When you run this, the output will be pasted in Sheet1.

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim adodb As Object
    Dim result

    Set ws = Sheets("Sheet1")

    Set adodb = CreateObject("ADODB.Connection")

    adodb.CursorLocation = 3

    adodb.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
                  "C:\book1.xlsx" & ";Extended Properties=""Excel 12.0 Xml;IMEX=1" & _
                  "HDR=NO;" & """"

    Set result = adodb.Execute("Select F1, F2, Count(*) from [Sheet1$] Group by F1,F2")

    With ws
        .Range(.Cells(1, 1), .Cells(result.RecordCount, result.Fields.Count)) _
        = Application.Transpose(result.GetRows)
    End With

    '~~> Cleanup
    result.Close
    adodb.Close
    Set adodb = Nothing
    Set result = Nothing
End Sub

MY ASSUMPTIONS (Change the above code - My code is based on below assumptions)

1) You are working with Excel 2007/2010 files. If not, then you will have to change the connection string. Please see this link for an appropriate connection string.

http://connectionstrings.com/excel

2) The name of your Excel file which has the data is called Book1.xlsx and the data is in Sheet1. Also it resides in C:\

SNAPSHOT

enter image description here

于 2012-05-05T08:53:58.183 回答