Consulting

Results 1 to 3 of 3

Thread: Imported array and vlookup named range

  1. #1
    VBAX Regular mikke3141's Avatar
    Joined
    Jun 2007
    Location
    Klaukkala
    Posts
    53
    Location

    Imported array and vlookup named range

    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.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    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
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular mikke3141's Avatar
    Joined
    Jun 2007
    Location
    Klaukkala
    Posts
    53
    Location
    Thank you. It worked wonders.

Posting Permissions

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