在excel或VBA中只计算一次重复值,当我添加新房间时,将自动计算我的房间类型总数
这是我的房间记录。我需要输入公式或VBA代码来计算我的房间类型在excel或VBA中只计算一次重复值,当我添加新房间时,将自动计算我的房间类型总数,excel,vba,excel-formula,Excel,Vba,Excel Formula,这是我的房间记录。我需要输入公式或VBA代码来计算我的房间类型 BedNum RoomNum RoomType RoomType totalcount 1001#1 1001 Studio Studio 3 1001#2 1001 Studio 2br1cr 2 1001#3 1001 Studio 3br1cr 1 1002#
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.
我们可以使用countifs()和countif(),如下所示:
编辑:
如果您希望自动生成一个列表,您可以查看以下内容,但要对列表进行排序,我留给您:
正如我在提示中所说,您可以使用“数据库”技术来实现这一点。我使用下面的代码来实现以下结果 我从以下布局开始 工作表shResult是空的。我相应地重命名了代码名 PS1我刚刚看到您更改了帖子,但您只需使用正确的字段名修改代码,即可获得以下结果 修改代码
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:可以将上述两个查询合并为一个查询,另一个查询将保存额外的工作表
query = "Select RoomType, count(RoomNum) From (Select RoomNum, RoomType From [shData$] " & _
"GROUP BY Roomtype,RoomNum) Group by RoomType"
最后的代码是这样的
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
请参阅第C列和第D列中使用公式提取列表中的唯一值,您可以使用countif。如果列表数据从A2开始,则C2
=IFERROR(索引($B$2:$B$12,匹配(0,COUNTIF($C$1:C1,$B$2:$B$12),0)),“”)中的数组(CSE)公式作为提示:您可以通过查询在Access中执行此操作。您描述的是一个枢轴-table@JvdV你是指我的评论吗?不管怎么说,你会用数据透视表数数吗?@JvdV:是的,你说得对。这也可以通过透视表来完成。老实说,我从未使用过向数据模型添加数据的选项无法创建唯一列表,因为我的数据id将增加并更新frequently@Hakunamatata那么你应该写一个更好的问题。执行该过程并将其与您的数据相匹配。@Hakumanatata请参阅编辑,以了解如何开始创建自动唯一列表…您好,感谢我尝试了上面的代码,但是shDate显示运行时错误,sHDate不是有效的名称。请确保它不包含INVILID字符或标点符号,并且不太长。您需要修改工作表的代码名。名字是shData!好的,谢谢你,我刚刚结束,这是工作,非常感谢你为我节省时间。谢谢
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