Solved

Visual Basic Listview / Excel

Posted on 2011-09-23
20
277 Views
Last Modified: 2012-05-12
I am trying to use this code once a .xlsx file is open to load all items starting from A2 - A1000 to load into the listview1 nambed compliantskus. Thanks for the help, and look forward to get this to work.


 Dim i As Integer
        Do Until i = 1000
            For incnumber As Integer = 1 To 1000
                Label4.Text = i
                compliantskus.Items.Add(oSheet.Range("A" & incnumber).Value)
                i = i + 1
            Next
        Loop

Open in new window

0
Comment
Question by:Jack_Jones
  • 13
  • 3
  • 3
  • +1
20 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 36590264
So you want to have a listview object (located in the Excel worksheet) load using the data found in cells A2-A1000, correct?  Where does VB.Net come into play?  Does your question only deal with Excel?  Please correct me if I am wrong.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36590288
I have a listview in visual basic that I want to generate data into from an excel sheet starting with A2 - A1000.
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 36590313
You are using VB.Net in Visual Studio?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36590321
it's Visual Studio 2010 code in VB.
0
 
LVL 13

Expert Comment

by:StormITSolutions
ID: 36590509
objExcel.Range("A2").Select 'Select Cell to start from 
Do Until objExcel.ActiveCell.Value = "A1000" 'Add items until row 1000
RowActive = objExcel.ActiveCell.Row 'Get current row
Set itmX = ListView1.ListItems.Add 
itmX = objExcel.Cells(RowActive, 1).Value 'Adds Value from Current Row and Column 1
itmX.SubItems(1) = objExcel.Cells(RowActive, 2).Value 'Adds value from Current Row and Column 2

objExcel.ActiveCell.Offset(1, 0).Select
Loop

Open in new window


This is how to do it in VB, but im confused with your statement Visual Studio 2010 in VB

VB is Visual Basic
VS 2010 is VB.net if im not mistaken?

Hope this helps.

Storm
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36590523
No luck with that Storm, it gives me lots of red errors under objExcel, RowActive, itmX. Here is what I fully am working with.

Dim oExcel, oWorkbook, oSheet, oNewSheet

        oExcel = CreateObject("Excel.Application")
        oWorkbook = oExcel.Workbooks.Open("C:\Inventory\database.xlsx")
        oSheet = oWorkbook.Sheets("PCDB")

        Dim i As Integer
        Do Until i = 1000
            For incnumber As Integer = 1 To 1000
                compliantskus.Items.Add(oSheet.Range("A" & incnumber).Value)
                i = i + 1
            Next
        Loop

        oWorkbook.Save()
        oExcel.Quit()
        oNewSheet = Nothing
        oSheet = Nothing
        oWorkbook = Nothing
        oExcel = Nothing

Open in new window

0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36590538
Yes I use VS 2010
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 36590642
Got to go...will help more later if you need me.
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36590647
sl8rz thanks
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36592327
What's wrong with your code? Do you have all the other code and references added so that oSheet refers to an actual Excel sheet?


I would rewrite your code as below
For incnumber As Integer = 2 To 1000
    compliantskus.Items.Add(oSheet.Range("A" & incnumber).Value)
Next

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:Jack_Jones
ID: 36592463
Sure do ;)
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36592466
Howdy CodeCruiser, good to see ya here!

Dim oExcel, oWorkbook, oSheet, oNewSheet

        oExcel = CreateObject("Excel.Application")
        oWorkbook = oExcel.Workbooks.Open("C:\Inventory\database.xlsx")
        oSheet = oWorkbook.Sheets("PCDB")

        Dim i As Integer
        Do Until i = 1000
            For incnumber As Integer = 1 To 1000
                compliantskus.Items.Add(oSheet.Range("A" & incnumber).Value)
                i = i + 1
            Next
        Loop

        oWorkbook.Save()
        oExcel.Quit()
        oNewSheet = Nothing
        oSheet = Nothing
        oWorkbook = Nothing
        oExcel = Nothing 

Open in new window

0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36592778
Here this might help;


System.Reflection.AmbiguousMatchException was unhandled
 
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36593779
Did you try replacing your loop code with mine? You are running two loops in your code which is not needed. Also, which line generates the error?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36593868
Yep still same error
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36593880
Which line?
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36593977
 compliantskus.Items.Add(oSheet.Range("A" & incnumber).Value
0
 
LVL 1

Author Comment

by:Jack_Jones
ID: 36593980
The first executed add item in the loop.

 For i As Integer = 0 To 35
            ListView1.Items.Add(oSheet.Range("A" & i + 3).Value)  <--- Flags Error
            ListView1.Items(i).SubItems.Add(oSheet.Range("B" & i + 3).Value)
            ListView1.Items(i).SubItems.Add(oSheet.Range("C" & i + 3).Value)
        Next

Open in new window

0
 
LVL 1

Assisted Solution

by:Jack_Jones
Jack_Jones earned 0 total points
ID: 36594264
Ok got it fixed after working on it all day,

For i As Integer = 0 To 34 <- Adjusted to how many rows are in the xls file.
            ListView1.Items.Add(oSheet.Range("A" & i + 1).Value) <- Changed to + 1
            ListView1.Items(i).SubItems.Add(oSheet.Range("B" & i + 1).Value)
            ListView1.Items(i).SubItems.Add(oSheet.Range("C" & i + 1).Value)
        Next

Open in new window

0
 
LVL 1

Author Closing Comment

by:Jack_Jones
ID: 36813451
Fixed Loop to rows, and changed it to + 1, error went bye bye.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now