Code below tries to overwrite contents of first worksheet. Save copy of workbook before running to be safe. You will need to provide full path to CSV. I assume you have the same number of columns on each line of your CSV:
Option explicit
Sub CSVtoSheet()
Const FILEPATH as string = "C:\New Folder\test.csv" 'Replace with your own path; or consider changing to variable and assign dynamically.'
Const DELIMITER as string = ","
' Read into memory; assumes file will fit and is not too big.'
Dim FileContents as string
Open FILEPATH for binary access read as #1
Filecontents = space$(lof(1))
Get #1, 1, filecontents
Close #1
' Assign lines in file to 1-dimensional, 0-based array of strings'
Dim AllLines() as string
AllLines = split(filecontents,vbNewLine)
Dim NumberOfRows as long
Dim NumberOfColumns as long
NumberOfRows = ubound(alllines)+1'Watch out if last line of CSV is blank, as is sometimes the case. Rows which do not contain column delimiter can produce error/unwanted behaviour.'
' Assume number or columns is fixed throughout CSV and can be reliably deduced from first line alone'
NumberOfColumns = ubound(split(alllines(lbound(alllines)),delimiter))+1
Dim ArrayToWriteToSheet() as string 'Change to as variant if you need numeric values as numbers'
Redim ArrayToWriteToSheet(1 to NumberOfRows, 1 to NumberOfColumns)
' Iterate through each element in array'
Dim RowIndex as long, ColumnIndex as long
Dim TemporaryArray() as string
For RowIndex = lbound(arraytowritetosheet,1) to ubound(arraytowritetosheet,1)
If Len(alllines(rowindex-1)) > 0 then ' Skip any blank lines; sometimes final line in CSV is empty which can result in errors/unwanted behaviour.'
TemporaryArray = split(alllines(rowindex-1),delimiter)
For ColumnIndex = lbound(arraytowritetosheet,2) to ubound(arraytowritetosheet,2)
Arraytowritetosheet(RowIndex,ColumnIndex) = temporaryarray(columnindex-1)
Next columnindex
End if
Next rowindex
'Write to first sheet in workbook. Hopefully, Excel will not do any unwanted auto-conversion.'
Thisworkbook.worksheets(1).range("A1"). Resize(ubound(arraytowritetosheet,1),ubound(arraytowritetosheet,2)).value2 = arraytowritetosheet
End sub
Does it do what you want? In theory, once you have the values in memory, you can format/present however you want before writing back to sheet.
Untested, written on mobile.