PDA

View Full Version : Imported array and vlookup named range



mikke3141
03-19-2010, 02:11 PM
Hello,

How can I move an imported csv file that I have transferred to an array, to be used as a named range for vlookup.


Sub Give_it()

file_name = file_name & "D:\test.csv"

fnum = FreeFile
whole_file = Input$(LOF(fnum), #fnum)
Close fnum

lines = Split(whole_file, vbCrLf)

num_rows = UBound(lines)
one_line = Split(lines(0), ",")
num_cols = UBound(one_line)
ReDim the_array(num_rows, num_cols)

For R = 0 To num_rows
one_line = Split(lines(R), ",")
For C = 0 To num_cols
the_array(R, C) = one_line(C)
Next C
Next R

End Sub

Above the import to array code.

mdmackillop
03-19-2010, 04:20 PM
Sub SetCSVtoRange()
Dim InputString As String, FileNum As Integer
Dim TextLine As String, MyDate As Date, IntegerValue As Integer
Dim Rng As Range, Pth As String, RName As String

'Pth = "D:\test.csv"
Pth = "C:\pds\data.csv"
RName = "MyData"
FileNum = FreeFile
Open Pth For Input As #FileNum
While Not EOF(FileNum)
i = i + 1
Line Input #FileNum, InputString
Cells(i, 1) = InputString
Wend
Close FileNum
Cells(1, 1).CurrentRegion.TextToColumns Destination:=Range("A1"), _
DataType:=xlDelimited, Comma:=True
Set Rng = Cells(1, 1).CurrentRegion
ActiveWorkbook.Names.Add Name:=RName, RefersTo:="=" & Rng.Address

End Sub

mikke3141
03-20-2010, 11:30 AM
Thank you. It worked wonders. :clap: