【发布时间】:2020-10-13 01:10:26
【问题描述】:
我有一个包含多个字符串的数据集,我想要一个唯一的出现次数,以便我可以查看和优化我的数据集。我一直无法使用公式做到这一点,所以转而使用 VBA,但由于我是业余爱好者,所以遇到了障碍。
我的数据看起来像这样......
我希望它返回这个...
我尝试将文本解析为列,但在大型数据集中,我的字符串中有 60 列,其中有 100 次点击。因此,转置它然后尝试获取唯一性计数将是令人生畏的。
因此,我希望 VBA 会有所帮助,但我似乎只能获得一个函数,而不是使用 Sub 和 Function 来转置然后计数。像下面这样...
Sub Main()
Dim filename As String
Dim WorksheetName As String
Dim CellRange As String
Sheets.Add.Name = "ParsedOutput"
'==============================================================
' CHANGE THESE VALUES FOR YOUR SHEET
WorksheetName =
CellRange =
'==============================================================
' Get range
Dim Range
Set Range = ThisWorkbook.Worksheets(WorksheetName).Range(CellRange)
' Copy range to avoid overwrite
Range.Copy _
Destination:=ThisWorkbook.Worksheets("ParsedOutput").Range("A1")
' Get copied exclusions
Dim Copy
Set Copy = ThisWorkbook.Worksheets("ParsedOutput").Range("A:A")
' Parse and overwrite
Copy.TextToColumns _
Destination:=Range("A:A"), _
DataType:=xlDelimited, _
ConsecutiveDelimiter:=True, _
Comma:=True
End Sub
Option Explicit
Public Function Counter(InputRange As Range) As String
Dim CellValue As Variant, UniqueValues As New Collection
Application.Volatile
'For error Handling On Error Resume Next
'Looping through all the cell in the defined range For Each CellValue In InputRange
UniqueValues.Add CellValue, CStr(CellValue) ' add the unique item Next
'Returning the count of number of unique values CountUniqueValues = UniqueValues.Count
End Function
【问题讨论】:
标签: excel vba powerquery