+ Reply to Thread
Results 1 to 29 of 29

How to link separate Excel files to auto-populate cells

Hybrid View

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

    Re: How to link separate Excel files to auto-populate cells

    Quote Originally Posted by chrlyd View Post
    My question regarding the formula is that I don't know how to apply this to my actual data. What formula do I enter and where to achieve these results with my data and as a I create new data sheets.
    Do you mean actual header names and/or position of header in csv are different from your files?

    If so, this code will find the position of each header in csv from the header in row(1) of the worksheet.
    Try replace "test" sub with the below, keeping other procedure/function remain intact.
    Sub test()
        Dim fn As String, a, b, s, x, y, temp
        Dim i As Long, ii As Long, dic(1) As Object
        For i = 0 To 1
            Set dic(i) = CreateObject("Scripting.Dictionary")
            dic(i).CompareMode = 1
        Next
        a = Sheets("sheet1").[a1].CurrentRegion.Value
        b = Application.Index(a, 1, 0)
        For i = 2 To UBound(a, 1)
            If a(i, 1) <> "" Then dic(0)(a(i, 1)) = Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4))
            If a(i, 2) <> "" Then dic(1)(a(i, 2)) = Array(a(i, 1), a(i, 2), a(i, 3), a(i, 4))
        Next
        fn = ThisWorkbook.Path & "\example_datasheet.csv"
        x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbNewLine)
        y = Split(CleanCSV(x(0), Chr(2), Chr(3)), ",")
        For ii = 1 To UBound(b)
            b(ii) = Application.Match(Replace(b(ii), ",", Chr(2)), y, 0)
            If IsError(b(ii)) Then MsgBox "Field " & a(1, ii) & " is missing", vbCritical: Exit Sub
            b(ii) = b(ii) - 1
        Next
        For i = 1 To UBound(x)
            If x(i) <> "" Then
                y = Split(CleanCSV(x(i), Chr(2), Chr(3)), ",")
                For ii = 0 To 1
                    s = Replace(y(b(ii + 1)), Chr(2), ",")
                    If dic(ii).exists(s) Then temp = dic(ii)(s)
                Next
                If IsArray(temp) Then
                    For ii = 1 To UBound(b)
                        If temp(ii - 1) Like "*,*" Then temp(ii - 1) = Chr(34) & temp(ii - 1) & Chr(34)
                        y(b(ii)) = temp(ii - 1)
                    Next
                End If
                x(i) = Replace(Replace(Join(y, ","), Chr(2), ","), Chr(3), """")
            End If
        Next
        fn = Replace(fn, ".csv", "_Updated.csv")
        Open fn For Output As #1
            Print #1, Join(x, vbNewLine);
        Close #1
        ImportCSV fn
    End Sub
    Last edited by jindon; 06-26-2024 at 03:51 AM.

+ 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. Auto link from excel to folder's files
    By sherif114 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 11-06-2020, 09:46 AM
  2. Replies: 3
    Last Post: 06-25-2020, 10:43 PM
  3. [SOLVED] How to link two sheets in two separate Excel files
    By BNCOXUK in forum Excel General
    Replies: 3
    Last Post: 11-30-2012, 09:17 AM
  4. Auto populate data from multiple cells on separate worksheet
    By Talance in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2011, 07:19 PM
  5. Link and auto-populate cells in workbooks
    By Larry in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 06-22-2006, 11:00 AM
  6. [SOLVED] How can I create a link between cells in two separate Excel sheet
    By Dawnmarie in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-18-2006, 01:40 PM
  7. How do I link separate Excel files to one spreadsheet?
    By eklushin in forum Excel General
    Replies: 0
    Last Post: 01-06-2006, 04:40 PM

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