Solved

Visual Basic Listview / Excel

Posted on 2011-09-23
20
291 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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