Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

Counting duplicate value only once in excel or VBA , will auto calculate my total of room type when i adding new room

this is my Room record. i need to put formula or VBA code to count my type of room.

BedNum  RoomNum  RoomType         RoomType    totalcount
1001#1   1001     Studio           Studio         3
1001#2   1001     Studio           2br1cr         2
1001#3   1001     Studio           3br1cr         1
1002#1   1002     2br1cr
1002#2   1002     2br1cr
1003#1   1003     3br1cr
1003#2   1003     3br1cr
1004#1   1004     Studio
1004#2   1004     Studio
1005#1   1005     2br1cr
1005#2   1005     2br1cr
1006#1   1006     Studio
1006#2   1006     Studio
---------------------------------------------------------
above list is my Excel record , i wanna count in the rightside with formula or either VBA code ,
my RoomNumber will always increase and adding new room , what i want is total up the count of my roomtype.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As said in my hint you can use "database" technology to do this. I used the code below to achieve the following result

enter image description here

I started with the following layout

enter image description here

The sheet shResult is empty. I renamed the codenames accordingly!

Reading on codename

Reading on ADO

Sub ReadFromWorksheetADO()

    ' Goto Tools/Reference and
    ' add Microsoft ActiveX Data Objects
    Dim conn As New ADODB.Connection

    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
        & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES;"";"

    Dim query As String
    ' The name in the tab has to be shData
    query = "Select ID, Name From [shData$] GROUP BY Name,ID"

    Dim rs As New ADODB.Recordset
    rs.Open query, conn

    ' Here I use the codename of the second sheet shResult!!
    With shResult
        .Cells.ClearContents
        Dim i As Long
        For i = 0 To rs.Fields.Count - 1
            .Cells(1, i + 1).Value = rs.Fields(i).Name
        Next i
        .Range("A2").CopyFromRecordset rs
    End With
    rs.Close

    ' Now I take the result of the first query and count
    query = "Select Name, count(ID) as CountOf From [shResult$] GROUP BY Name"
    rs.Open query, conn

    ' Here I use the codename of the first sheet shData
    With shData
        .Range("D2").CopyFromRecordset rs
    End With

End Sub

PS1 I just saw that you changed your post but you just need to modify the code with the right fieldnames in order to get the following result

enter image description here

Modified code

Sub ReadFromWorksheetADO_B()

    ' Goto Tools/reference and
    ' add Microsoft ActiveX Dataa Objects
    Dim conn As New ADODB.Connection

    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
        & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES;"";"

    Dim query As String
    ' The name in the tab has to be shData
    query = "Select RoomNum, RoomType From [shData$] GROUP BY Roomtype,RoomNum"

    Dim rs As New ADODB.Recordset
    rs.Open query, conn

    ' Here I use the codename of the first sheet shResult!!
    With shResult
        .Cells.ClearContents
        Dim i As Long
        For i = 0 To rs.Fields.Count - 1
            .Cells(1, i + 1).Value = rs.Fields(i).Name
        Next i
        .Range("A2").CopyFromRecordset rs
    End With
    rs.Close

    ' Now I take the result of the first query and count
    query = "Select RoomType, count(RoomNum) as CountOf From [shResult$] GROUP BY RoomType"
    rs.Open query, conn

    ' Here I use the codename of the first sheet
    With shData
        .Range("E2").CopyFromRecordset rs
    End With

End Sub

PS2: One can combine the two queries above in one and one would save the extra sheet.

query = "Select RoomType, count(RoomNum) From (Select RoomNum, RoomType From [shData$] " & _
                "GROUP BY Roomtype,RoomNum) Group by RoomType"

And the final code looks like that then

Sub ReadFromWorksheetADO_C()

    ' Goto Tools/reference and
    ' add Microsoft ActiveX Dataa Objects
    Dim conn As New ADODB.Connection

    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
        & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES;"";"

    Dim query As String
    ' The name in the tab has to be shData
    query = "Select RoomType, count(RoomNum) From (Select RoomNum, RoomType From [shData$] " & _
                        "GROUP BY Roomtype,RoomNum) Group by RoomType"

    Dim rs As New ADODB.Recordset
    rs.Open query, conn

    ' Here I use the codename of the first sheet
    With shData
        .Range("E2").CopyFromRecordset rs
    End With

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...