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!
microsoft-excel vba csv
add a comment |
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!
microsoft-excel vba csv
What have you tried so far?
– cybernetic.nomad
Nov 20 at 18:39
You can useApplication.WorksheetFunction.Index
andApplication.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
add a comment |
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!
microsoft-excel vba csv
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
microsoft-excel vba csv
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 useApplication.WorksheetFunction.Index
andApplication.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
add a comment |
What have you tried so far?
– cybernetic.nomad
Nov 20 at 18:39
You can useApplication.WorksheetFunction.Index
andApplication.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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 20 at 23:26
Brian
212
212
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What have you tried so far?
– cybernetic.nomad
Nov 20 at 18:39
You can use
Application.WorksheetFunction.Index
andApplication.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