How do I read named ranges from a text file into excel using VBA?

sarithgada86

New Member
Joined
Jul 25, 2011
Messages
4
Hey All,

I am trying to write some code to read values back into a named range from a .txt document. The code I used to write it out was:

Sub RangeToText()
Set fs = CreateObject("Scripting.FileSystemObject")
Set x = fs.CreateTextFile(Range("filepath") & Range("studentname") & " " & Range("Basegroup") & ".txt")

For a = 1 To Range("rangeformacro").Rows.Count 'a range of named ranges to loop through
b = Range("rangeformacro").Rows(a) 'find the named range I want to work through

x.writeline b 'state the named range

For c = 1 To Range(b).Rows.Count 'loop through to write out data in that range

x.writeline (Range(b).Rows(c))

Next c

Next a
End Sub

The text file this produces looks like this:

StudentName (Named range is studentname)
Joe Bloggs (Data in this named range etc...)
BaseGroup
7H
College
Holmes College


Unfortunately, I do not know where to start to write the code to read it back in!

Help please?!

Thanks!:)

Sarith
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You could use FileSystemObject methods OpenTextFile and ReadLine, or VBA Open and Line Input statements. In both cases you'll need to check for the end of the file before reading the next line.
 
Upvote 0
Thanks for the reply John. But what code do I need to write? The coding is my downfall, never done this type of programming before!

Thanks,

Sarith
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top