Solved

Visual Basic Listview / Excel

Posted on 2011-09-23
20
293 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 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