I want to know the number of rows in a excel but if the value is repeated in a "A" column it should not count .
A B C
aa 1 1
bb 2 2
aa 3 3
cc 4 4
bb 5 5
Total=3 Total=5 Total=5
I want to know the number of rows in a excel but if the value is repeated in a "A" column it should not count .
A B C
aa 1 1
bb 2 2
aa 3 3
cc 4 4
bb 5 5
Total=3 Total=5 Total=5
You need some way of counting distinct entities. A HashSet will work well for that:
The HashSet class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
All that needs to be done is look at each of the values in each of the columns and add it to a hashset if it isn't in there already, then count the number of entries in the hashset:
Option Infer On
Option Strict On
Imports Microsoft.Office.Interop
Imports System.Text
Public Class Form1
Sub ShowDistinctColumnValueCounts(xlFile As String)
Dim xl = New Excel.Application
Dim wb As Excel.Workbook = xl.Workbooks.Open(xlFile)
Dim ws As Excel.Worksheet = DirectCast(wb.Worksheets(1), Excel.Worksheet)
Dim nCols = ws.UsedRange.Columns.Count
Dim nRows = ws.UsedRange.Rows.Count
Dim vals = DirectCast(ws.Range(ws.Cells(1, 1), ws.Cells(nRows, nCols)).Value, Object(,))
wb.Close()
xl.Quit()
Dim sb As New StringBuilder
For col = 1 To nCols
Dim hs As New HashSet(Of Object)
For row = 1 To nRows
If Not hs.Contains(vals(row, col)) Then
hs.Add(vals(row, col))
End If
Next
sb.AppendLine($"Col {col} has {hs.Count} distinct entries")
Next
TextBox1.Text = sb.ToString()
End Sub
Sub DoStuff()
Dim src = "C:\temp\test.xlsx"
ShowDistinctColumnValueCounts(src)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DoStuff()
End Sub
End Class
With your sample data (I assumed that "A", "B", and "C" are the column names, not entries), that outputs:
Col 1 has 3 distinct entries
Col 2 has 5 distinct entries
Col 3 has 5 distinct entries
Additional Ref: The proper way to dispose Excel com object using VB.NET?