1

I have a column in an Excel file that contain 100 rows. I'm trying to import this column into a ListBox using a button.

The problem is that only 48 rows are importing from the Excel column.

Why aren't all the rows inside the column imported?

Here is my code (vb.net form):

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    Dim oExcel As Object = CreateObject("Excel.Application")
    Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\User\Desktop\1.xlsx")
    Dim oSheet As Object = oBook.Worksheets(1)
    Dim i As Integer
    Dim cell As String
    For i = 0 To AscW(ListBox1.Items.Count.ToString()(i = i + 1)) - 1
        'set cell name, e.g. A1, A2, etc
        cell = "B" & Convert.ToString(i + 1)
        ' get cell data from Excel
        cell = oSheet.Range(cell).Value
        If cell = "" Then
            Exit For
        Else
            ListBox5.Items.Add(cell)
        End If
    Next
    oExcel.Quit()
End Sub
4
  • 1
    Is there an empty cell??? If cell = "" Then Exit For Commented Dec 16, 2015 at 9:54
  • The issue is most probably coming from AscW(ListBox1.Items.Count.ToString()(i = i + 1)) - 1 or the Exit For, why are you taking the Asc value of your listbox's count? Commented Dec 16, 2015 at 9:58
  • there is no empty cell, I'm using Asc to take all the items not a specific size Commented Dec 16, 2015 at 10:00
  • is there any alternative way to implement it ? @R3uK Commented Dec 16, 2015 at 10:14

1 Answer 1

2

I changed your AscW(... to oSheet.Range("B" & oSheet.Rows.Count).End(xlUp).Row,
so that you'll add all the column B to your ListBox

(still, be careful because of your Exit For you canNOT have an empty cell in the middle!)

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim oExcel As Object = CreateObject("Excel.Application")
    Dim oBook As Object = oExcel.Workbooks.Open("C:\Users\User\Desktop\1.xlsx")
    Dim oSheet As Object = oBook.Worksheets(1)
    Dim i As Integer
    Dim cell As String
For i = 0 To oSheet.Range("B" & oSheet.Rows.Count).End(xlUp).Row
    'set cell name, e.g. A1, A2, etc
    cell = "B" & Convert.ToString(i + 1)
    ' get cell data from Excel
    cell = oSheet.Range(cell).Value
    If cell = "" Then
        Exit For
    Else
        ListBox5.Items.Add (cell)
    End If
Next i
oExcel.Quit()
End Sub
Sign up to request clarification or add additional context in comments.

10 Comments

Very much appreciate your answer . But a small error occur ( Error 'xlup' is not declared. It may be inaccessible due to its protection level)
Ok, so you can replace it by its actual value : Const xlUp = -4162 (&HFFFFEFBE), let me know if it is enough! ;)
Can you be more specific where exactly to insert the actual value?
You could either define a variable with that name and set it to the good value. Or you can just replace it directly in the line For i = ... , change ).End(xlUp). to ).End(-4162).
Ofcourse i would love to help you .. but i can't downvote now until the answer is edited
|

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.