CSV Import with Data Types
Import CSV files with automatic data type detection and formatting
1767 views
Featured
Perfect For:
- Data migration
- External data import
- File processing
VBA Code
Sub ImportCSVWithTypes()
Dim filePath As String
Dim ws As Worksheet
Dim qt As QueryTable
Dim lastRow As Long, lastCol As Long
' Get CSV file path
filePath = Application.GetOpenFilename("CSV Files (*.csv), *.csv")
If filePath = "False" Then Exit Sub
' Create new worksheet for import
Set ws = Worksheets.Add
ws.Name = "Imported_" & Format(Now, "mmdd_hhmm")
' Import CSV with QueryTable for better control
Set qt = ws.QueryTables.Add( _
Connection:="TEXT;" & filePath, _
Destination:=ws.Range("A1"))
With qt
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.TextFileConsecutiveDelimiter = False
.RefreshStyle = xlInsertDeleteCells
.Refresh BackgroundQuery:=False
End With
' Auto-detect and apply data types
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
Dim col As Long
For col = 1 To lastCol
' Check if column contains dates
If IsDate(ws.Cells(2, col).Value) Then
ws.Columns(col).NumberFormat = "dd/mm/yyyy"
' Check if column contains numbers
ElseIf IsNumeric(ws.Cells(2, col).Value) And ws.Cells(2, col).Value <> "" Then
ws.Columns(col).NumberFormat = "0.00"
End If
Next col
' Auto-fit columns
ws.Columns.AutoFit
qt.Delete
MsgBox "CSV imported successfully with " & lastRow & " rows and " & lastCol & " columns!"
End Sub
Related Topics
csv
import
data types
formatting
Need Custom VBA Solutions?
Our AI-powered VBA generator can create custom code tailored to your specific requirements in seconds.
Free AI generations every month — top up with credit packs anytime
Related Templates
More VBA templates in the same category
Intermediate
Timesheet & Attendance Tracker
Track employee working hours, overtime, absences, and holidays with automatic calculations, weekl...
View Template
Advanced
Inventory Management System
Manage stock levels with automatic reorder alerts, stock movement logging, valuation reports, and...
View Template
Intermediate
Data Entry Form with Validation
Professional UserForm-based data entry system with field validation, dropdown lists, date pickers...
View Template