Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: Writing to Excel

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Writing to Excel

    Hi all
    I would like to write to excel, i know how to do the basics like the actual writing. But I do no want to specify a cell to write to I want it to go to the next row with no text in the cells can someone help me on this. I would also like it to be done behind the scenes, like when you use stream writer. But I suppose it would have to be opened to find the available row to write in. Help would be much appreciated.

    Sam

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    There is a built in function in Excel that will do that but then you would have to be using the Excel Object Model. How about if you use a SQL INSERT INTO statement to add a row? Will it place it at the end of your rows?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Could you show me an example where you click a button, it a number from a text box and puts it into to column A and the next available row.

    VB Code:
    1. Dim objExcel As New Excel.Application
    2.  
    3.         objExcel.Visible = True
    4.  
    5.         objExcel.Workbooks.Add()
    6.  
    7.         objExcel.Range("A1").Select()
    8.         objExcel.ActiveCell.FormulaR1C1 = "75"
    9.         objExcel.Range("B1").Select()
    10.         objExcel.ActiveCell.FormulaR1C1 = "125"
    11.         objExcel.Range("C1").Select()
    12.         objExcel.ActiveCell.FormulaR1C1 = "255"
    13.         objExcel.Range("D1").Select()
    14.         objExcel.ActiveCell.FormulaR1C1 = "295"
    15.  
    16.        
    17.         objExcel = Nothing

    I have leant ho to do this with a book I have, but what I want to do is open an existing excel file and add to it. It would be a great help if you can give me a very simple example.

    Sam

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    VB Code:
    1. Dim lRow As Long
    2.  
    3. lRow = objExcel.Sheets("Sheet1").Range("A1").SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row
    4. Messagebox.Show("Last Row: " & lRow.ToString, "VB Forums - Excel")
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Thanks Alot this work perfectly, just add one to the lRow so it writes to the next line.

    Sam

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    One more thing how would I close Excel and save what I have put in there.
    This is my code:
    VB Code:
    1. Dim objExcel As Microsoft.Office.Interop.Excel.Application
    2.         Dim lRow As Long
    3.         Dim wb As Microsoft.Office.Interop.Excel.Workbook
    4.  
    5.         Try
    6.  
    7.             objExcel = New Microsoft.Office.Interop.Excel.Application
    8.             wb = objExcel.Workbooks.Open("c:\test.xls")
    9.             objExcel.Visible = True
    10.             lRow = objExcel.Sheets("Sheet1").Range("A1").SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row
    11.             MessageBox.Show("Last Row: " & lRow.ToString, "VB Forums - Excel")
    12.  
    13.             lRow = lRow + 1
    14.             objExcel.Range("A" & lRow.ToString).Select()
    15.             objExcel.ActiveCell.FormulaR1C1 = "75"
    16.            
    17.         Catch ex As Runtime.InteropServices.COMException
    18.             MessageBox.Show("Error accessing Excel: " + ex.ToString())
    19.  
    20.         Catch ex As Exception
    21.             MessageBox.Show("Error: " + ex.ToString())
    22.  
    23.         End Try

    Sam

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Just call the Close method and pass the save argument and filename.
    VB Code:
    1. wb.Close SaveChanges:=True, FileName:="C:\MyBook.xls"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Hi Rob
    Ran in to slight problem with the Save how do I do the overwrite part.

    Sam

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Oh ok you have a previously savesd xls file. Then we can use the .SaveAs method to overwrite.
    VB Code:
    1. wb.SaveAs FileName:="C:\MyBook.xls", FileFormat:=xlWorkbookNormal
    2. wb.Saved = True
    3. wb.Close SaveChanges:=False
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Sorry to be such a pain, but there is a error that says Error 'xlWorkbookNormal' is not declared.?????

    Sam

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    I used this instead
    VB Code:
    1. wb.Save()
    2.         wb.Saved = True
    3.         wb.Close(SaveChanges:=False)
    4.         objExcel.Quit()

    This ok?

    How could I tell if when the file is opened whether its read only or not, the reason I ask this is because two people will be writing to this file. But if one has it open the other will not be able to save, I want this to come up with a msg saying already open cannot write until closed. Is this possible?

    Sam

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Its because I forgot the .NET fully qualified object path to the const.


    you can do it that way too if its not a new file. If the other user has the workbook open then is it a shared file or are you getting the "Read Only Notification" dialog?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    This file will be on a network, it is basically a quote log. So if when person has got it open it will be read only and the app would now be able to add the quote. So I want to be able to check if it is open so it can be closed. Understand?

    Sam

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Yes I understand but if the workbook is not designated as a Shared Workbook then the users and your code will encounter the message that it is opened Exclusively by another user and if they want to open the file as Read Only.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Yes it does open as Read-Only, if someone else has it open it opens as read only is there a way you can stop this and enable people to save when there are two open.

    Also how do you take the content of a cell and then use it, I also want to be able to split this as well eg say the content was EL11-12-RRR and I wanted to take the 11. Can you show me how to do this? Appreciate this help alot.

    Thanks

    Sam

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Excel 2003 and earlier: Go to Tools > Share Workbook. This will allow editing by multiple users at the same time and also merge workbooks too.

    Excel 2007: Go to Review ribben tab > Share Workbook.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    So just to clarify for me, when this is done. Two people will be able to edit the spreadsheet and save it at different times but it will have both sets of data in. True?

    Office 07 is not out yet in the UK dont think, will not be getting it for a while either because it is 70% more expensive here than in the US. Just a massive rip off

    Sam

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    I havent been in an environment to be able to test it but its supossed to allow both to make changes. Doesnt specify if it will get refreshed or if they have to reopen it or ?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Hi Rob

    Can you help me with this? I have got what you have told me working so far. But I now need to write to a cell from two listboxes, this is what I want to happen:

    In the cell I want:
    The first item in listbox 1 the , and the first item in listbox 2. Then one a new line I would like the second items in the same format.

    Help would be much appreciated

    Sam

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    VB Code:
    1. 'Write to sheet1, cell A1 with the value of the first item in the listbox
    2. wb.Sheets("Sheet1").Cells(1, 1).Value = Me.Listbox1.Items(0).ToString
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Hi Rob
    I have got this code, I want it to write first line of listbox1 the a , and then the first line of listbox2, then I want it to go to a new line. This is my code, but it overwrites each time obviously, but I didn't know how to do append the cell so wrote this so someone could modify:

    VB Code:
    1. objExcel.Range("F" & lRow.ToString).Select()
    2.             For index = 0 To lstproname.Items.Count - 1
    3.                 objExcel.ActiveCell.FormulaR1C1 = lstproname.Items(index)   & _    ", " & lstproprice.Items(index) &  vbNewLine
    4.                 index = index +1
    5.             Next

    Thanks

    Sam
    Last edited by samtaylor08; Jan 13th, 2007 at 07:23 AM.

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    I dont quite follow how you need it to be. Could you give an example?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Right I have 2 listboxes, on for product and one for the price of the product.

    So What I want is to list the contents of the listboxes into a cell.

    So for example I will write to A1 with how I want it:
    Product 1, Price 1
    Product 2 , Price 2
    Product 3, Price 3
    and so on till all of the items are listed in that cell.

    But what happens now is the last product and the last price are the only ones listed, because they overwrite the rest.

    Its quite hard to explain, hope this helps you a little more to understand what I want.

    Thanks

    Sam

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Oh ok. Its because you are not changing the cell that you are writting to. You are always writting to the ActiveCell which is not changing. Use an .Offset or just reference the Cells(x, 1) where x is the row number increasing in your loop.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    I want to write to the same cell eachtime so it stays on the same row. Can this be done I want each product and price one a different line thats why I put vbnewline but in the same cell. Can this be done?
    Sam

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Just reference the cell location as I suggested in a loop that will place each item on a new row. Make a secondary call to place another item on the same row but reference the next column by increasing the Y Cells value.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    I am not sure I understand this, if I put the cell bit in the loop, it will change the cell I am writing to and I want the whole listbox in 1 cell.

    Sam

  28. #28
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Ok how about an example of how you want it because I thought you wanted it in each cell and then each row as the record changes. Seems you are asking for everything in a single cell which is what you had in th beginning?[/color]
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Right I have uploaded a spreadsheet as an example, I had to do this is word then copy it across because when you try to press enter in excel it takes you to the cell underneath when I want a new line in that cell. Do you understand what I am after?

    Sam
    Attached Files Attached Files

  30. #30
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    I'll look at it in a minute, but did you know that if you press Alt+Enter you will enter a line break and remain editing that same cell instead of moving out of edit mode and mving to the next cell.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  31. #31
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    So you want everything in the same cell but 2 items per line in that cell.

    Instead of writting to the formula property of the cell in a loop, just write it all out to a string variable in your loop and then assign the cells value to that string variable after the loop is complete.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Can you give me an example not sure what you mean?

    But that is what I want.

    Sam

  33. #33
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Sure, heres a quick example
    VB Code:
    1. Dim str As String
    2. for loop goes here
    3.     str = str & lstproname.Items(index)   & ", " & lstproprice.Items(index) &  vbNewLine
    4. done with loop
    5. cells(1, 1).Value = str
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  34. #34

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    Thanks Alot understand now seems so simple when you know how.

    Sam

  35. #35

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    This works but there is a square at the end of each line is there a way to stop this I think it the vbnewline.

    Sam

  36. #36
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Ah, yes its the vbNewline or essentially the carriage return. I have seen this before but let me see if there was a workaround or not.

    It could be done by entering items in separate rows and then .Merge the rows together.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  37. #37

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    I am trying to loop though a spread sheet but I want to be able to use a 1 for the column A instead and so on for the rest.
    This is so I can use a variable instead of the actual letter.
    This is how I am doing it:
    VB Code:
    1. xl.Range(col & frow).Select()
    2.             file = xl.ActiveCell.Text

    But it comes up with a Comexception was not handled:
    System.Runtime.InteropServices.COMException was unhandled
    ErrorCode=-2146827284
    Message="Exception from HRESULT: 0x800A03EC"
    Source="Microsoft.Office.Interop.Excel"
    StackTrace:
    at Microsoft.Office.Interop.Excel.ApplicationClass.get_Range(Object Cell1, Object Cell2)
    at itext.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Sam Taylor\My Documents\Visual Studio 2005\Projects\itext\itext\Form1.vb:line 57
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(ApplicationContext context)
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    at itext.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
    at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    Can someone help me with this?

    Thanks

    Sam

  38. #38
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Well as shown in the error, you need to specify the two cells that will make up the range.

    Range(Object Cell1, Object Cell2)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  39. #39

    Thread Starter
    Addicted Member
    Join Date
    Dec 2006
    Posts
    208

    Re: Writing to Excel

    col is the column and frow is the row. The problem is I need them both to be numbers so I can use a loop, but it will not let me because col needs to be a letter I was wondering if there was away around it?

    Sam

  40. #40
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Writing to Excel

    Use the format I posted as you can use only numbers for both row and column.
    VB Code:
    1. oCell1 = [i]Sheet1[/i].Cells(1, 1)
    2. oCell2 = [i]Sheet1[/i].Cells(2, 1)
    3. [i]Sheet1.[/i]Range(oCell1, oCell2).Select()
    4. '...
    5. 'But you dont need to "Select" the cell as it slows down
    6. 'your code and also because you can just reference the cell.
    7.  
    8. file = [i]Sheet1.[/i]Range(oCell1, oCell2).Value
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Page 1 of 2 12 LastLast

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