This is my first attempt at custom functions in excel. I have been having troubles getting my csv file to import into an existing workbook so I thought I'd use a custom function to pull the data I need so i attempted to implement that function as a vba module:
Test file: Potter.csv (only has 2 records)
The function call I used is
=ImportCSVData("Potter.csv",D2,F1)
and
=ImportCSVData("Potter.csv","D2","F1")
Both were met with the #VALUE! error and I'm unsure how to resolve it
I created the following:
Function ImportCSVData(csvFilePath As String, sourceCell As String, destinationCell As String)
Dim ws As Worksheet
Dim csvWb As Workbook
Dim csvWs As Worksheet
' Get the current worksheet
Set ws = Application.Caller.Worksheet
' Open the CSV file
Set csvWb = Workbooks.Open(csvFilePath)
Set csvWs = csvWb.Sheets(1)
' Get data from the source cell in CSV and put it in the destination cell
ws.Range(destinationCell).Value = csvWs.Range(sourceCell).Value
' Close the CSV file without saving changes
csvWb.Close False
' Release memory
Set csvWs = Nothing
Set csvWb = Nothing
Set ws = Nothing
End Function
Here is a screenshot of the test csv file:
I expected it to return "James Potter" in the cell I specified but instead both were met with the #VALUE! error and I'm unsure how to resolve it