Results 1 to 29 of 29

Thread: parsing data into excel

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    parsing data into excel

    I've used VB slightly, but never gotten familiar with VBA. Is it possible to create a form that a user can input a string of data into, then parse that the integers out of that string into the next blank cell?

    Example: ID: #15149, Budget: 2 cr, agriculture: 0, mining: 0, processing: 0, production: 0, physics: 0, chemics: 0, medical: 0, weapons: 1, drives: 0, construction: 0

    Parsing this would return 15149, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 into row n(next blank row), columns a-l.

    Or is there some way to set agriculture, mining, etc as constants and return the integer following them to the next blank cell?

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

    Re: parsing data into excel

    Welcome to the Forums.

    Yes this can be done. Their may be a better way though. What about having
    a separate textbox for each cell entry?
    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
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Sadly, that would defeat the whole purpose of the macro, which is to make it so a large number of variants of the above posted string can be quickly sorted. I started trying to do this in a stand-alone VB application using regular expressions, and was able to sort the string, but had no idea how to import the data into excel.

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

    Re: parsing data into excel

    This short Excel Tutorial should help.

    Wouldnt using a userform also defeat the purpose too. Entering data directly into Excel be faster or are you
    reading data in from a textfile 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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    I think the tutorial might help me hack something together.

    The string I'm pasting in will be coming from my browser window, and there will be a large number (100-1000s) of variations of it. I'm trying to create a way I just copy, paste, then click and have it sort out the string without me having to bother with it.

    Sorry if this isn't so clear. I have the sneaking suspicion that I'm just going to have to sit down and learn VB(A) in order to do this, instead of just hacking it all together =)

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

    Re: parsing data into excel

    Well that may be true, but we are here to help you with any questions when they come up. The thing to
    do is to break it down into smaller manageable sections. First you need to finish you userform so it populates
    Excel correctly. Then you need to handle all the variations of the data.

    VB and VBA are very similar. You can even automate Excel from VB, read the data from the web page and directly
    enter it into Excel without copying and pasting. It just depends on how much work you want to do.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Appreciate the help, I'll probably get back to once I'm done with the next 1k pages of my Mastering VB .NET book

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

    Re: parsing data into excel

    You dont have to read all 1000 before you start but it does help. What I do is learn as the need arises.
    Like with this one you will want to learn how to inetract with the userform and the excel grid. Then, learn some
    of the parsing and validation techniques and that should get your main issue solved.

    Search the Forums for everything will be here that you need, if not let me know and I will try to help.
    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Alright.. one more go at this. Maybe this is in the wrong forum, but you seem to know your stuff.. so here goes

    VB Code:
    1. mports System.IO
    2. Imports System.Net
    3. Imports System.Text.RegularExpressions
    4.  
    5. Public Class Form1
    6.     Sub AppendResults(ByVal msg As String)
    7.         txtRes.AppendText(msg & ControlChars.CrLf)
    8.     End Sub
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _      As System.EventArgs) Handles Button1.Click
    10.         Dim r As Regex
    11.         Dim m As Match
    12.         r = New Regex("\d+")
    13.         Dim mc As MatchCollection = r.Matches(txtFor.Text)
    14.         For Each m In mc
    15.             AppendResults(m.Value)
    16.  
    17.         Next
    18.  
    19.  
    20.  
    21.     End Sub

    This is returning all of the integer values in this user (txtFor textbox) inputted string:

    ID: #15207, Budget: 2 cr, agriculture: 0, mining: 0, processing: 0, production: 0, physics: 0, chemics: 1, medical: 0, weapons: 0, drives: 3, construction: 0

    to my txtRes textbox in the following format:
    15207
    2
    0
    0
    0
    0
    0
    1
    0
    0
    3
    0

    How would I populate the next blank row with these values? Am I even on the right track here? Thanks for your patience and your help.

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

    Re: parsing data into excel

    This should get you started.
    VB Code:
    1. Imports System.IO
    2. Imports System.Net
    3. Imports System.Text.RegularExpressions
    4. Imports Microsoft.Office.Interop
    5.  
    6. Public Class Form1
    7.     Inherits System.Windows.Forms.Form
    8.  
    9. [color=dimgray]Region " Windows Form Designer generated code "[/color]
    10.  
    11.     Sub AppendResults(ByVal msg As String)
    12.         txtRes.AppendText(msg & ControlChars.CrLf)
    13.     End Sub
    14.  
    15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16.         Dim r As Regex
    17.         Dim m As Match
    18.         r = New Regex("\d+")
    19.         Dim mc As MatchCollection = r.Matches(txtFor.Text)
    20.         For Each m In mc
    21.             AppendResults(m.Value)
    22.         Next
    23.  
    24.         Dim oApp As Excel.Application
    25.         Dim oWB As Excel.Workbook
    26.         Dim rLastRow As Excel.Range
    27.         Const xlCellTypeLastCell = 11
    28.         Dim i As Integer
    29.         Dim iRow As Integer
    30.         Dim iCol As Integer
    31.         Dim bOpen As Boolean
    32.  
    33.         Try
    34.             oApp = GetObject(, "Excel.Application")
    35.             If oApp Is Nothing Then oApp = New Excel.Application
    36.             For Each oWB In oApp.Workbooks
    37.                 If oWB.Name = "MyExcelDemo" Then
    38.                     bOpen = True
    39.                     Exit For
    40.                 Else
    41.                     bOpen = False
    42.                 End If
    43.             Next
    44.             If bOpen = True Then
    45.                 'Already open
    46.                 oWB = oApp.Workbooks("MyExcelDemo")
    47.             Else
    48.                 'Open your wb
    49.                 oWB = oApp.Workbooks.Open("C:\MyExcelDemo.xls")
    50.             End If
    51.             rLastRow = oWB.Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell)
    52.             iRow = rLastRow.Row
    53.             For i = 1 To 12
    54.                 oWB.Sheets(1).cells(i, iRow) = mc(i - 1).Value
    55.             Next
    56.         Catch ex As Exception
    57.             Throw New System.Exception("Excel Error!")
    58.         End Try
    59.  
    60.     End Sub
    61.  
    62. End Class
    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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Thanks! Where is iCol used at.. and Const xlCellTypeLastCell = 11 isn't declared?

    Silly me.. got the constant declared.. but iCol isn't used anywhere?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Alright.. got iCol taken care of.. now.. how to make it continue to write to my spreadsheet without trying to open a new one everytime?

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

    Re: parsing data into excel

    I put iCol in there because I though that maybe we would need dynamic used column range too?
    The GetObject should be attaching to any existing excel instance.

    This line is where you need to place a breakpoint and test if its working correctly.

    VB Code:
    1. If oApp Is Nothing Then oApp = New Excel.Application
    I didnt test the code so there may be some issues still.
    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

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

    Re: parsing data into excel

    Ok, I didnt place the oApp object in the correct scope.
    VB Code:
    1. Imports System.IO
    2. Imports System.Net
    3. Imports System.Text.RegularExpressions
    4. Imports Microsoft.Office.Interop
    5.  
    6. Public Class Form1
    7.     Inherits System.Windows.Forms.Form
    8.  
    9.     [b]Private oApp As Excel.Application[/b]
    10.  
    11. [color=DimGray]Region " Windows Form Designer generated code "[/color]
    12.  
    13.     Sub AppendResults(ByVal msg As String)
    14.         txtRes.AppendText(msg & ControlChars.CrLf)
    15.     End Sub
    16.  
    17.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    18.         Dim r As Regex
    19.         Dim m As Match
    20.         r = New Regex("\d+")
    21.         Dim mc As MatchCollection = r.Matches(txtFor.Text)
    22.         For Each m In mc
    23.             AppendResults(m.Value)
    24.         Next
    25.  
    26.         Dim oWB As Excel.Workbook
    27.         Dim rLastRow As Excel.Range
    28.         Const xlCellTypeLastCell = 11
    29.         Dim i As Integer
    30.         Dim iRow As Integer
    31.         'Dim iCol As Integer
    32.         Dim bOpen As Boolean
    33.  
    34.         Try
    35.             oApp = GetObject(, "Excel.Application")
    36.             If oApp Is Nothing Then oApp = New Excel.Application
    37.             For Each oWB In oApp.Workbooks
    38.                 If oWB.Name = "MyExcelDemo" Then
    39.                     bOpen = True
    40.                     Exit For
    41.                 Else
    42.                     bOpen = False
    43.                 End If
    44.             Next
    45.             If bOpen = True Then
    46.                 'Already open
    47.                 oWB = oApp.Workbooks("MyExcelDemo")
    48.             Else
    49.                 'Open your wb
    50.                 oWB = oApp.Workbooks.Open("C:\MyExcelDemo.xls")
    51.             End If
    52.             rLastRow = oWB.Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell)
    53.             iRow = rLastRow.Row
    54.             For i = 1 To 12
    55.                 oWB.Sheets(1).cells(i, iRow) = mc(i - 1).Value
    56.             Next
    57.         Catch ex As Exception
    58.             Throw New System.Exception("Excel Error!")
    59.         End Try
    60.  
    61.     End Sub
    62.  
    63. End Class
    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
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    I really appreciate all the help you've given me and your inhuman patience with my ignorance. Everything seems to be working accept I keep getting an unhandled exception for this peice of code:

    VB Code:
    1. Catch ex As Exception
    2.             Throw New System.Exception("Excel Error!")
    3.         End Try

    Which seems to be referring back to:

    VB Code:
    1. oApp = GetObject(", Excel.Application")

    When I ignore this error, and compile it anyways, the program works for the first string I enter, but for every additional string it tries to open/reopen my spreadsheet. Would it be easier to write all the data from txtFor directly to excel instead of it passing through txtRes? I originally did this to test whether or not the code I wrote was sorting the string properly, and it is really no longer necessary.

    Again, thank you for all your help. I know you are a busy person, and if this problem has taken too much of your time, and you don't feel like fiddling with it anymore, I'll understand completely.

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

    Re: parsing data into excel

    Looks like you may have had an issue pasting the code in. It should be...
    VB Code:
    1. oApp = GetObject(, "Excel.Application")
    Your double quote was to the left of the comma. It needs to be just around the Excel.Application 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

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Fixed that, but still getting the unhandled exception at:

    Throw New System.Exception("Excel Error!")

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

    Re: parsing data into excel

    I think you also need the System namespace.
    VB Code:
    1. Imports System
    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
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Still no go. Appreciate all your help, but I think I'm going to put this project on hold until I get a better idea of what I'm doing.. my guess/check/beg for help method is starting to get frustrating hehe

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

    Re: parsing data into excel

    Noooo, dont give up yet. So close. Change it to this.
    VB Code:
    1. Catch ex As Exception
    2.     MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
    3. End Try
    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
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Arg.. now I get cannot create active x component..?
    Err.. if I have excel open.. it will put the string where I want it, but when I do another string it tries to reopen it? How about we'll just assume that I always have excel and the workbook open before using this program so we don't have to worry about that?

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

    Re: parsing data into excel

    Ok, I have created a test and got the GetObject working so it doesnt open another instance.
    I will post it in a minute, but I want to get the regex working.

    I pasted in: "ID: #15149, Budget: 2 cr, agriculture: 0, mining: 0, processing: 0, production: 0, physics: 0, chemics: 0, medical: 0, weapons: 1, drives: 0, construction: 0"
    and its not parsing correctly. I havent used regex too much so if you could assist me on that I can test if
    everything is working correctly.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: parsing data into excel

    Nevermind, I got the Regex working and the entire code.
    "ID: #15149, Budget: 2 cr, agriculture: 0, mining: 0, processing: 0, production: 0, physics: 0, chemics: 0, medical: 0, weapons: 1, drives: 0, construction: 0"

    VB Code:
    1. Imports System.IO
    2. Imports System.Net
    3. Imports System.Text.RegularExpressions
    4. Imports Microsoft.Office.Interop
    5.  
    6. Public Class Form1
    7.     Inherits System.Windows.Forms.Form
    8.  
    9.     Private oApp As Excel.Application
    10.  
    11. #Region " Windows Form Designer generated code "
    12.  
    13.     Public Sub New()
    14.         MyBase.New()
    15.  
    16.         'This call is required by the Windows Form Designer.
    17.         InitializeComponent()
    18.  
    19.         'Add any initialization after the InitializeComponent() call
    20.  
    21.     End Sub
    22.  
    23.     'Form overrides dispose to clean up the component list.
    24.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    25.         If disposing Then
    26.             If Not (components Is Nothing) Then
    27.                 components.Dispose()
    28.             End If
    29.         End If
    30.         MyBase.Dispose(disposing)
    31.     End Sub
    32.  
    33.     'Required by the Windows Form Designer
    34.     Private components As System.ComponentModel.IContainer
    35.  
    36.     'NOTE: The following procedure is required by the Windows Form Designer
    37.     'It can be modified using the Windows Form Designer.  
    38.     'Do not modify it using the code editor.
    39.     Friend WithEvents txtRes As System.Windows.Forms.TextBox
    40.     Friend WithEvents Button1 As System.Windows.Forms.Button
    41.     Friend WithEvents txtFor As System.Windows.Forms.TextBox
    42.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    43.         Me.txtRes = New System.Windows.Forms.TextBox
    44.         Me.txtFor = New System.Windows.Forms.TextBox
    45.         Me.Button1 = New System.Windows.Forms.Button
    46.         Me.SuspendLayout()
    47.         '
    48.         'txtRes
    49.         '
    50.         Me.txtRes.Location = New System.Drawing.Point(136, 136)
    51.         Me.txtRes.Name = "txtRes"
    52.         Me.txtRes.TabIndex = 0
    53.         Me.txtRes.Text = ""
    54.         '
    55.         'txtFor
    56.         '
    57.         Me.txtFor.Location = New System.Drawing.Point(72, 56)
    58.         Me.txtFor.Name = "txtFor"
    59.         Me.txtFor.TabIndex = 1
    60.         Me.txtFor.Text = ""
    61.         '
    62.         'Button1
    63.         '
    64.         Me.Button1.Location = New System.Drawing.Point(176, 216)
    65.         Me.Button1.Name = "Button1"
    66.         Me.Button1.TabIndex = 2
    67.         Me.Button1.Text = "Button1"
    68.         '
    69.         'Form1
    70.         '
    71.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    72.         Me.ClientSize = New System.Drawing.Size(292, 266)
    73.         Me.Controls.Add(Me.Button1)
    74.         Me.Controls.Add(Me.txtFor)
    75.         Me.Controls.Add(Me.txtRes)
    76.         Me.Name = "Form1"
    77.         Me.Text = "Form1"
    78.         Me.ResumeLayout(False)
    79.  
    80.     End Sub
    81.  
    82. #End Region
    83.  
    84.     Sub AppendResults(ByVal msg As String)
    85.         txtRes.AppendText(msg & ControlChars.CrLf)
    86.     End Sub
    87.  
    88.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    89.         Dim r As Regex
    90.         Dim m As Match
    91.         r = New Regex("\d+")
    92.         Dim mc As MatchCollection = r.Matches(txtFor.Text)
    93.         For Each m In mc
    94.             AppendResults(m.Value)
    95.         Next
    96.  
    97.         Dim oWB As Excel.Workbook
    98.         Dim rLastRow As Excel.Range
    99.         Dim rLastCol As Excel.Range
    100.         Const xlCellTypeLastCell = 11
    101.         Dim i As Integer
    102.         Dim iRow As Integer
    103.         Dim iCol As Integer
    104.         Dim bOpen As Boolean
    105.  
    106.         Try
    107.             oApp = GetObject(, "Excel.Application")
    108.             If oApp Is Nothing Then oApp = New Excel.Application
    109.             For Each oWB In oApp.Workbooks
    110.                 If oWB.Name = "MyExcelDemo.xls" Then
    111.                     bOpen = True
    112.                     Exit For
    113.                 Else
    114.                     bOpen = False
    115.                 End If
    116.             Next
    117.             If bOpen = True Then
    118.                 'Already open
    119.                 oWB = oApp.Workbooks("MyExcelDemo.xls")
    120.             Else
    121.                 'Open your wb
    122.                 oWB = oApp.Workbooks.Open("C:\MyExcelDemo.xls")
    123.             End If
    124.             oApp.Visible = True
    125.             rLastRow = oWB.Sheets(1).UsedRange.SpecialCells(xlCellTypeLastCell)
    126.             iRow = rLastRow.Row + 1
    127.             rLastCol = oWB.Sheets(1).usedrange.specialcells(xlCellTypeLastCell)
    128.             iCol = rLastCol.Column
    129.             For i = 1 To mc.Count
    130.                 oWB.Sheets(1).Cells(iRow, i).Value = mc(i - 1).Value
    131.             Next
    132.             oWB.Save()
    133.         Catch exp As Exception
    134.             MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
    135.         End Try
    136.  
    137.     End Sub
    138.  
    139. End Class
    Last edited by RobDog888; Apr 11th, 2005 at 09:40 PM.
    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

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

    Re: parsing data into excel

    You can make your program compatible with Excel 2002 as described in this quote from MSDN:
    Quote Originally Posted by MSDN
    Using the Office XP PIAs and the Office 2003 PIAs
    PIAs are version specific, so you should use the same version of the PIAs as the application version. For example, if you
    want to migrate your Microsoft Office XP managed code solution to Microsoft Office 2003, you should recompile the
    Office XP managed code solution with Office 2003 PIAs.

    You should also have two different setup packages—one for Office XP and another for Office 2003—if you intend for
    the same solution to work in both Office XP and Office 2003. Deployment of PIAs for Office XP is very different from that
    for Office 2003. For further information about these differences, see the ReadMe file included in the Office XP PIAs
    download
    . As emphasized in Avoiding Redistribution of the Office 2003 PIAs, no solution deployment scenario should try
    to redeploy the Office 2003 PIAs. If your solution relies on the Office 2003 PIAs, it should use Office 2003 to deliver the PIAs.
    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
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Wow.. I just opened up a whole new can of worms. I think my friends can just go out and get Office 2003.

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Hrm.. for some reason it won't let me access the microsoft excel 10 object library, only the 11. Any suggestions?

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

    Re: parsing data into excel

    What do you have installed on that system? Both? Did you install the PIA? If you installed the PIAs then it will only be
    one version allowed at a time.
    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

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Apr 2005
    Posts
    20

    Re: parsing data into excel

    Yeah.. my friends can just go blow the extra money on Office 2003

    Thanks again though.

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

    Re: parsing data into excel

    Have you tried Late Binding ?
    VB Code:
    1. Dim oApp as Object
    2. oApp = CreateObject("Excel.Application")
    3. 'Blah, blah, blah...
    Dont use any references to Office or 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

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