Solved

Visual Basic Listview / Excel

Posted on 2011-09-23
20
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

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

Expert Comment

by:Allan Nisbet
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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