?
Solved

Visual Basic Listview / Excel

Posted on 2011-09-23
20
Medium Priority
?
300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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 …
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

764 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