Import text file with lines of variable length into Excel using VBA

I have a VBA enabled sheet with buttons that perform various tasks including dumping 4 lines of IP addresses into a text file. A sample text file is below:

192.168.44.12
192.168.44.123
192.168.33.111
192.168.44.1

At the end of my run, it reads the contents of the text file and places each line in specific fields (not in row vertically) in the sheet.

Right now I'm able to do this if I already know the length each line but it will be different each time.
wifinutAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
How are you reading the file, the length of a line shouldn't be a problem if you are just reading it as a plain text file.

~bp
wifinutAuthor Commented:
Thanks for your response. I'm including the entire code for this module as it stands right now:

Private Sub Commandbutton1_Click()


Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
myFile = "C:\test\myfile.txt"
'myFile = Application.GetOpenFilename()

Dim RetVal


RetVal = Shell("C:\test\(some.exe)", 1)


Application.Wait (Now + TimeValue("0:00:35"))


Open myFile For Input As #1
    Do Until EOF(1)
        Line Input #1, textline
        text = text & textline
    Loop
Close #1

posLat = InStr(text, "latitude")
posLong = InStr(text, "longitude")

Range("D43").Value = Mid(text, posLat + 1, 13)
Range("E43").Value = Mid(text, posLat + 14, 12)

Range("D44").Value = Mid(text, posLat + 28, 12)
Range("E44").Value = Mid(text, posLat + 37, 14)


MsgBox "Done."

End Sub

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Okay, good, I get that.  But that doesn't look like it's reading the IP list file you mentioned in the posting?

Can you post an example of the file this would read?

Once I see that we can parse it properly to be flexible.

~bp
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

wifinutAuthor Commented:
Sure. Here you are.
ips.txt
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, give this a try.  It reads each line, and puts the contents of that line in the corresponding cell.

Private Sub Commandbutton1_Click()

   Dim myFile As String, text As String

   Dim linenum As Integer
   Dim DestCells() As String
   DestCells = Split("D4,E43,D44,E44", ",")

   myFile = "C:\test\myfile.txt"
   'myFile = Application.GetOpenFilename()

   Dim RetVal
   RetVal = Shell("C:\test\(some.exe)", 1)

   Application.Wait (Now + TimeValue("0:00:35"))

   linenum = -1
   Open myFile For Input As #1
       Do Until EOF(1)
           Line Input #1, text
           linenum = linenum + 1
           Range(DestCell(linenum)).Value = text
       Loop
   Close #1

   MsgBox "Done."

End Sub

Open in new window

~bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wifinutAuthor Commented:
Thank you so much!!! Been working on this for 2 days now. You're a life saver!
Bill PrewIT / Software Engineering ConsultantCommented:
Great, glad that was useful.

~bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.