0

I have 2 Labels (lblTotal) and (lblYes) which I put on my form. I want to read an excel file and count how many people come and how many say "yes", like this (picture). So "lblTotal" will count all row with data (exclude table header) and "lblYes" will show the counting of Answer with "Yes" which updates in real time. So far I got the lblTotal to work:

Dim xlApp As Excel.Application = Nothing
Dim xlWorkBooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim xlWorkSheet As Excel.Worksheet = Nothing
Dim xlWorkSheets As Excel.Sheets = Nothing
Dim xlCells As Excel.Range = Nothing

Public Sub Total()
        xlApp = New Excel.Application
        xlApp.DisplayAlerts = False
        xlWorkBooks = xlApp.Workbooks
        xlWorkBook = xlWorkBooks.Open("D:\Question\Data.xlsx")
        xlApp.Visible = False
        xlWorkSheet = xlWorkBook.Worksheets("Sheet1")

Dim xx As Integer = xlWorkSheet.UsedRange.Rows.Count - 1
        lblTotal.Text = xx.ToString()
        xlApp.Quit()
End Sub

However, I have no idea how to count row that has the Yes answer. Also this doesn't allow real time update. Even though I use a timer to do the job (update on each tick) but it takes it too much to load. For example, in the code above, I used Button to refresh by calling the Total() function in the button click event.

I was thinking about the Button Save event that triggered this update as well. But my biggest question is how can I make it count "Yes" answer only?

3
  • Perhaps this will help. Commented Sep 14, 2018 at 4:35
  • Thank. That give me a way of new search but what should I start? xlworksheet.range("D1").autofilter Commented Sep 14, 2018 at 4:58
  • Looks like Gabriels' approach of simply running through the range and counting the "Yes" values works for you. It's probably a better solution than applying a filter. Commented Sep 14, 2018 at 19:22

1 Answer 1

1

I would do something like this, inside the Total() function:

    Dim WorkSheetPath As String = "D:\Question\Data.xlsx"
    Dim xlApp As Excel.Application
    Dim xlWorkbook As Excel.Workbook
    Dim xlWorksheet As Excel.Worksheet
    Dim Range As Excel.Range
    Dim rCnt As Integer
    Dim Obj As Object

    xlApp = New Excel.Application
    xlWorkbook = xlApp.Workbooks.Open(WorkSheetPath)
    xlWorksheet = xlWorkbook.Worksheets("sheet1")

    Range = xlWorksheet.UsedRange
    Dim count As Integer = 0
    For rCnt = 1 To Range.Rows.Count
        Obj = CType(Range.Cells(rCnt, 4), Excel.Range) 'if you change the index of the answer column in your excel file, change the index here as well (it is 4 right now)
        Dim answer As String = Obj.Value.ToString()
        If answer = "Yes" Then
            count += 1
        End If
    Next
    xlWorkbook.Close()
    xlApp.Quit()
    lblTotal.Text = count.ToString()

Hope this helps. ^^

Sign up to request clarification or add additional context in comments.

13 Comments

Dim workbook As New ExcelDocument error woth not defined.
Can you please explain it? I am not sure I understood your error
ExcelDocument is not defined.
Have you imported the Microsoft.Office.Interop.Excel reference where you have this piece of code?
isn't it suppose to be Excel.Application? I think
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.