Results 1 to 5 of 5

Thread: [RESOLVED] Export from Listbox to Excel

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Resolved [RESOLVED] Export from Listbox to Excel

    I have an app which exports data to Excel. In excel the data is added, but with an square. This makes the calculations in Excel impossible.
    I've tried to use the replace in text changed, but with no luck.

    The code that export Listbox lines to Excel:

    Code:
      Dim trace As Integer
            For trace = 1 To ListBox1.Items.Count
                xlWorkSheet.Cells(trace + 18, 2).Value = ListBox1.Items(trace - 1)
            Next
    ListBox1_TextChanged:

    Code:
      ListBox1.Text = Replace(ListBox1.Text, Chr(8), String.Empty) 'backspace  
            ListBox1.Text = Replace(ListBox1.Text, Chr(9), String.Empty) ' tab
            ListBox1.Text = Replace(ListBox1.Text, Chr(10), String.Empty) 'LF
            ListBox1.Text = Replace(ListBox1.Text, Chr(11), String.Empty) 'vertical tab
            ListBox1.Text = Replace(ListBox1.Text, Chr(12), String.Empty) 'form feed, new page 
            ListBox1.Text = Replace(ListBox1.Text, Chr(13), String.Empty) 'CR
            ListBox1.Text = Replace(ListBox1.Text, Chr(32), String.Empty) 'Space
            ListBox1.Text = Replace(ListBox1.Text, Chr(35), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(127), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(129), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(141), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(143), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(144), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(157), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(249), String.Empty)
            ListBox1.Text = Replace(ListBox1.Text, Chr(254), String.Empty) 'Black box
            ListBox1.Text = Replace(ListBox1.Text, Chr(255), String.Empty) 'White Space

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Export from Listbox to Excel

    What happens if you do:
    Code:
      Dim trace As Integer
            For trace = 1 To ListBox1.Items.Count
                xlWorkSheet.Cells(trace + 18, 2).FormulaR1C1 = ListBox1.Items(trace - 1).ToString
            Next
    Also you don't need that code in ListBox1_TextChanged event.

    Can you attach a sample file?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: Export from Listbox to Excel

    Hi, thanks. The result was the same with your code. The text is pasted into a richtextbox, by the user. So I have no example, but it's just numbers.

    I also tried to export from a richtextbox, also with the same result.

    Export from textbox code:

    Code:
      Dim Text As String = String.Empty
            Dim i As Integer = 0
            Dim Lines As Integer = txtTraceSpeed.Lines.Length
    
            For i = 0 To Lines - 1
                Text = txtTraceSpeed.Lines(i).ToString()
                xlWorkSheet.Range("B" & i + 1).Value = Text
            Next
    The square seems to be a tab, but i'm not sure.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2007
    Location
    Sweden
    Posts
    359

    Re: Export from Listbox to Excel

    It could be resolved with this:

    Code:
    lstTraceSpeed.Text = Replace(lstTraceSpeed.Text, Chr(9), String.Empty)
    But only for the textbox, this didn't work for the listbox.

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] Export from Listbox to Excel

    xlWorkSheet.Cells(trace + 18, 2).Value = ListBox1.Items(trace - 1)
    Try triming the value before exporting it to excel...

    Edit: I had not refresh the page!!!! Damn
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width