Excel VBA index match a csv import











up vote
0
down vote

favorite












How can I use an index match function in VBA to find values in a CSV file then import those values to the matched index in a cell in Excel? Say I have a CSV file which contains the following comma deliminated text:



lookup1, 2



lookup2, 3



And the following data in Sheet2 in Excel:



B2 = lookup1



C2 = 0



B3 = lookup2



C3 = 0



How do I use an index match to find the value 2 in the second column for "lookup1" in the CSV file and then change the value to 2 in cell C2 in Excel?



I've tried the code below but it doesn't work and I don't think it's looking in the Excel sheet to match where the value needs to go



Sub Tester()

Dim arr1

arr1 = CsvToArray(Application.GetOpenFilename)

Debug.Print TestLookup(arr1, "lookup1", 1, 2)

End Sub

Function TestLookup(arr, val, lookincol As Integer, returnfromcol As Integer)
Dim r
r = Application.Match(val, Application.Index(arr, 0, lookincol), 0)
If Not IsError(r) Then
TestLookup = arr(r, returnfromcol)
Else
TestLookup = "Not found" 'or some other "error" value
End If
End Function

Function CsvToArray(filepath As String) As Variant
Dim wb As Workbook
Application.ScreenUpdating = False
Set wb = Application.ActiveWorkbook
CsvToArray = wb.Sheets("Sheet2").Range("C2").CurrentRegion.Value
End Function


Thanks!










share|improve this question
























  • What have you tried so far?
    – cybernetic.nomad
    Nov 20 at 18:39










  • You can use Application.WorksheetFunction.Index and Application.WorksheetFunction.Match in VBA, but I doubt it will be useful in this case.
    – Christofer Weber
    Nov 20 at 18:51










  • @cybernetic.nomad i made changes to the post to include the code I've used so far, but it's not quite what i need
    – Tyll25
    Nov 20 at 19:12















up vote
0
down vote

favorite












How can I use an index match function in VBA to find values in a CSV file then import those values to the matched index in a cell in Excel? Say I have a CSV file which contains the following comma deliminated text:



lookup1, 2



lookup2, 3



And the following data in Sheet2 in Excel:



B2 = lookup1



C2 = 0



B3 = lookup2



C3 = 0



How do I use an index match to find the value 2 in the second column for "lookup1" in the CSV file and then change the value to 2 in cell C2 in Excel?



I've tried the code below but it doesn't work and I don't think it's looking in the Excel sheet to match where the value needs to go



Sub Tester()

Dim arr1

arr1 = CsvToArray(Application.GetOpenFilename)

Debug.Print TestLookup(arr1, "lookup1", 1, 2)

End Sub

Function TestLookup(arr, val, lookincol As Integer, returnfromcol As Integer)
Dim r
r = Application.Match(val, Application.Index(arr, 0, lookincol), 0)
If Not IsError(r) Then
TestLookup = arr(r, returnfromcol)
Else
TestLookup = "Not found" 'or some other "error" value
End If
End Function

Function CsvToArray(filepath As String) As Variant
Dim wb As Workbook
Application.ScreenUpdating = False
Set wb = Application.ActiveWorkbook
CsvToArray = wb.Sheets("Sheet2").Range("C2").CurrentRegion.Value
End Function


Thanks!










share|improve this question
























  • What have you tried so far?
    – cybernetic.nomad
    Nov 20 at 18:39










  • You can use Application.WorksheetFunction.Index and Application.WorksheetFunction.Match in VBA, but I doubt it will be useful in this case.
    – Christofer Weber
    Nov 20 at 18:51










  • @cybernetic.nomad i made changes to the post to include the code I've used so far, but it's not quite what i need
    – Tyll25
    Nov 20 at 19:12













up vote
0
down vote

favorite









up vote
0
down vote

favorite











How can I use an index match function in VBA to find values in a CSV file then import those values to the matched index in a cell in Excel? Say I have a CSV file which contains the following comma deliminated text:



lookup1, 2



lookup2, 3



And the following data in Sheet2 in Excel:



B2 = lookup1



C2 = 0



B3 = lookup2



C3 = 0



How do I use an index match to find the value 2 in the second column for "lookup1" in the CSV file and then change the value to 2 in cell C2 in Excel?



I've tried the code below but it doesn't work and I don't think it's looking in the Excel sheet to match where the value needs to go



Sub Tester()

Dim arr1

arr1 = CsvToArray(Application.GetOpenFilename)

Debug.Print TestLookup(arr1, "lookup1", 1, 2)

End Sub

Function TestLookup(arr, val, lookincol As Integer, returnfromcol As Integer)
Dim r
r = Application.Match(val, Application.Index(arr, 0, lookincol), 0)
If Not IsError(r) Then
TestLookup = arr(r, returnfromcol)
Else
TestLookup = "Not found" 'or some other "error" value
End If
End Function

Function CsvToArray(filepath As String) As Variant
Dim wb As Workbook
Application.ScreenUpdating = False
Set wb = Application.ActiveWorkbook
CsvToArray = wb.Sheets("Sheet2").Range("C2").CurrentRegion.Value
End Function


Thanks!










share|improve this question















How can I use an index match function in VBA to find values in a CSV file then import those values to the matched index in a cell in Excel? Say I have a CSV file which contains the following comma deliminated text:



lookup1, 2



lookup2, 3



And the following data in Sheet2 in Excel:



B2 = lookup1



C2 = 0



B3 = lookup2



C3 = 0



How do I use an index match to find the value 2 in the second column for "lookup1" in the CSV file and then change the value to 2 in cell C2 in Excel?



I've tried the code below but it doesn't work and I don't think it's looking in the Excel sheet to match where the value needs to go



Sub Tester()

Dim arr1

arr1 = CsvToArray(Application.GetOpenFilename)

Debug.Print TestLookup(arr1, "lookup1", 1, 2)

End Sub

Function TestLookup(arr, val, lookincol As Integer, returnfromcol As Integer)
Dim r
r = Application.Match(val, Application.Index(arr, 0, lookincol), 0)
If Not IsError(r) Then
TestLookup = arr(r, returnfromcol)
Else
TestLookup = "Not found" 'or some other "error" value
End If
End Function

Function CsvToArray(filepath As String) As Variant
Dim wb As Workbook
Application.ScreenUpdating = False
Set wb = Application.ActiveWorkbook
CsvToArray = wb.Sheets("Sheet2").Range("C2").CurrentRegion.Value
End Function


Thanks!







microsoft-excel vba csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 19:29









Rey Juna

5239




5239










asked Nov 20 at 18:33









Tyll25

12




12












  • What have you tried so far?
    – cybernetic.nomad
    Nov 20 at 18:39










  • You can use Application.WorksheetFunction.Index and Application.WorksheetFunction.Match in VBA, but I doubt it will be useful in this case.
    – Christofer Weber
    Nov 20 at 18:51










  • @cybernetic.nomad i made changes to the post to include the code I've used so far, but it's not quite what i need
    – Tyll25
    Nov 20 at 19:12


















  • What have you tried so far?
    – cybernetic.nomad
    Nov 20 at 18:39










  • You can use Application.WorksheetFunction.Index and Application.WorksheetFunction.Match in VBA, but I doubt it will be useful in this case.
    – Christofer Weber
    Nov 20 at 18:51










  • @cybernetic.nomad i made changes to the post to include the code I've used so far, but it's not quite what i need
    – Tyll25
    Nov 20 at 19:12
















What have you tried so far?
– cybernetic.nomad
Nov 20 at 18:39




What have you tried so far?
– cybernetic.nomad
Nov 20 at 18:39












You can use Application.WorksheetFunction.Index and Application.WorksheetFunction.Match in VBA, but I doubt it will be useful in this case.
– Christofer Weber
Nov 20 at 18:51




You can use Application.WorksheetFunction.Index and Application.WorksheetFunction.Match in VBA, but I doubt it will be useful in this case.
– Christofer Weber
Nov 20 at 18:51












@cybernetic.nomad i made changes to the post to include the code I've used so far, but it's not quite what i need
– Tyll25
Nov 20 at 19:12




@cybernetic.nomad i made changes to the post to include the code I've used so far, but it's not quite what i need
– Tyll25
Nov 20 at 19:12










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Your CSV file needs to be formatted with a proper header.



In plain text it would look like:



lookup1,lookup2
2,3


Would comment but not ranked high enough yet.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1377061%2fexcel-vba-index-match-a-csv-import%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Your CSV file needs to be formatted with a proper header.



    In plain text it would look like:



    lookup1,lookup2
    2,3


    Would comment but not ranked high enough yet.






    share|improve this answer

























      up vote
      0
      down vote













      Your CSV file needs to be formatted with a proper header.



      In plain text it would look like:



      lookup1,lookup2
      2,3


      Would comment but not ranked high enough yet.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Your CSV file needs to be formatted with a proper header.



        In plain text it would look like:



        lookup1,lookup2
        2,3


        Would comment but not ranked high enough yet.






        share|improve this answer












        Your CSV file needs to be formatted with a proper header.



        In plain text it would look like:



        lookup1,lookup2
        2,3


        Would comment but not ranked high enough yet.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 23:26









        Brian

        212




        212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1377061%2fexcel-vba-index-match-a-csv-import%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            AnyDesk - Fatal Program Failure

            How to calibrate 16:9 built-in touch-screen to a 4:3 resolution?

            QoS: MAC-Priority for clients behind a repeater