+ Reply to Thread
Results 1 to 10 of 10

VBA Code Trouble - Need to return value after defined text from a text file

  1. #1
    Registered User
    Join Date
    08-18-2017
    Location
    England
    MS-Off Ver
    2010
    Posts
    7

    VBA Code Trouble - Need to return value after defined text from a text file

    I have some code below:

    Sub SystemInfoInput()
    Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
    myFile = "C:\Temp\systeminfo.txt"
    Open myFile For Input As #1
    Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
    Loop
    Close #1
    TotMem = InStr(text, "Total Physical Memory")
    SysMod = InStr(text, "System Model")
    Range("D4").Value = Mid(text, TotMem + 27, 8)
    Range("D5").Value = Mid(text, SysMod + 27, 19)
    End Sub

    I want it to return the value after Total Physical Memory: (currently have it above by using the characters but as this will be run on different machines these values will change and the character length not always correct).

    At the moment i am trying to get it to return a few lines but i will want to return more than i currently have got.

    Here is a snippet of the file it is opening:

    OS Name: Microsoft Windows 7 Enterprise
    OS Version: 6.1.7601 Service Pack 1 Build 7601
    OS Manufacturer: Microsoft Corporation
    OS Configuration: Member Workstation
    OS Build Type: Multiprocessor Free
    Registered Owner: Computer User
    Original Install Date: 12/07/2016, 15:28:21
    System Boot Time: 25/08/2017, 10:43:46
    System Manufacturer: HP
    System Model: HP EliteBook 850
    System Type: X86-based PC
    Processor(s): 1 Processor(s) Installed.
    [01]: x64 Family 6 Model 78 Stepping 3 GenuineIntel ~2277 Mhz
    BIOS Version: HP N75 Ver. 01.16, 08/06/2017
    Windows Directory: C:\WINDOWS
    System Directory: C:\WINDOWS\system32
    Boot Device: \Device\HarddiskVolume1
    System Locale: en-gb;English (United Kingdom)
    Input Locale: en-gb;English (United Kingdom)
    Time Zone: (UTC+00:00) Dublin, Edinburgh, Lisbon, London
    Total Physical Memory: 3,185 MB

    Any suggestions on how i can improve the code would be greatly appreciated!

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    By adding a line break (vbcrlf), or any other unique character for that matter, between lines from the text file, you can then find the start and end of the line with the info you want. Then we can also generalize the formula by using the length of the string you are searching, rather than hard coding the number of characters. So this approach will return the info you need, not caring what the actual length of the memory, or system model is. I also added the trim function to remove any leading/trailing spaces in the answer:

    Please Login or Register  to view this content.
    PS Please check out the forum rules, specifically #3 about using code tags.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    Perhaps this is better... by just making an array of the data strings you want to extract, you can then loop through the array:

    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    See if this is how you wanted.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-18-2017
    Location
    England
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    Thanks @Arkadi, that works a real treat!

    Thanks for the info about the code rules, apologies first post will ensure i follow that going forward

  6. #6
    Registered User
    Join Date
    08-18-2017
    Location
    England
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    Quote Originally Posted by Arkadi View Post
    Perhaps this is better... by just making an array of the data strings you want to extract, you can then loop through the array:

    Please Login or Register  to view this content.
    So would i be right in thinking i would need to just add my additional info needs into the myInfo array? Like this?

    Please Login or Register  to view this content.

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    Yes that's right... that ought to do it

  8. #8
    Registered User
    Join Date
    08-18-2017
    Location
    England
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    Quote Originally Posted by Arkadi View Post
    Yes that's right... that ought to do it
    I thought so, but Excel doesn't like

    I get Run time error 5 - Invalid procedure call or argument

    When i hit debug it goes to:

    Please Login or Register  to view this content.

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    We have two options... build in some error checking, or you need to make sure the string will be found... in your 2 new strings, you have "Os", but the text file has a capital S for those values ("OS") which I presume is causing your error. This modification should eliminate the error, but will not return values either unless there is a perfect match:


    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    08-18-2017
    Location
    England
    MS-Off Ver
    2010
    Posts
    7

    Re: VBA Code Trouble - Need to return value after defined text from a text file

    Quote Originally Posted by Arkadi View Post
    We have two options... build in some error checking, or you need to make sure the string will be found... in your 2 new strings, you have "Os", but the text file has a capital S for those values ("OS") which I presume is causing your error. This modification should eliminate the error, but will not return values either unless there is a perfect match:


    Please Login or Register  to view this content.
    Genius, and can't believe i didn't notice the lower s

    Works just as needed!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] analyze cells of text-return defined text phrase if all cells have same text
    By vtwinsport in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-30-2014, 09:30 PM
  2. Powershell code to count data connections and return output to a text file
    By Jack C in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-29-2014, 04:05 AM
  3. Loop through Code that copies the content of a text file to another text file
    By michiel soede in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-31-2013, 01:34 PM
  4. [SOLVED] Trouble saving a websites source code to a text file.
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-12-2013, 09:11 AM
  5. Importing a data from a text file - user-defined file
    By DaveSev in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2013, 07:02 PM
  6. code to save selected(pre defined)text to text file
    By martindwilson in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-27-2008, 06:51 PM
  7. trouble with code to move text
    By JOUIOUI in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-21-2006, 08:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1