Import CSV Columns Into Excel With VBA and a File Picker
Question details
Select a CSV file with a VBA file picker and import columns A and B from row 57 onward into the active workbook starting at A31.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- The user wants to choose a CSV file and import a specific range into the active worksheet.
- Observed behavior
- The desired result is for the CSV values to appear in columns A and B beginning at cell A31, through the final populated source row.
Open the destination workbook and select the worksheet that should receive the data. Save the workbook containing the macro as an Excel Macro-Enabled Workbook (.xlsm), and enable macros if prompted.
Use a VBA macro to select and import the CSV range
Use this macro to choose a CSV file, transfer the values from columns A and B, and close the source file without saving.
The macro captures the active worksheet before opening the CSV. It uses the last populated row in either column A or B and transfers values without source formatting.
In Excel, open the Developer tab and select Visual Basic, or press Alt+F11. In the VBA editor, select Insert > Module.
Paste the following code into the module: Sub ImportCSVColumns() Dim filePath As Variant Dim sourceBook As Workbook Dim destinationSheet As Worksheet Dim lastRow As Long Dim previousScreenUpdating As Boolean Dim errorMessage As String Set destinationSheet = ActiveSheet previousScreenUpdating = Application.ScreenUpdating On Error GoTo HandleError filePath = Application.GetOpenFilename( _ FileFilter:="CSV Files (*.csv),*.csv", _ Title:="Select a CSV file") If VarType(filePath) = vbBoolean Then GoTo CleanUp Application.ScreenUpdating = False Set sourceBook = Workbooks.Open(Filename:=CStr(filePath), Local:=True) With sourceBook.Worksheets(1) lastRow = Application.Max( _ .Cells(.Rows.Count, "A").End(xlUp).Row, _ .Cells(.Rows.Count, "B").End(xlUp).Row) If lastRow < 57 Then MsgBox "No data was found in columns A or B from row 57 onward." GoTo CleanUp End If destinationSheet.Range("A31").Resize(lastRow - 56, 2).Value = _ .Range("A57:B" & lastRow).Value End With CleanUp: On Error Resume Next If Not sourceBook Is Nothing Then sourceBook.Close SaveChanges:=False Application.ScreenUpdating = previousScreenUpdating On Error GoTo 0 If Len(errorMessage) > 0 Then MsgBox errorMessage, vbExclamation Exit Sub HandleError: errorMessage = "Import failed: " & Err.Description Resume CleanUp End Sub
Return to Excel and select the destination worksheet. Choose Developer > Macros, select ImportCSVColumns, and click Run. In the file picker, select the CSV file and click Open.
Inspect the destination worksheet starting at A31. The macro closes the CSV without saving changes and restores the previous ScreenUpdating setting.

Work with CSV spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open CSV files and work with Excel-format workbooks to make switching between office suites straightforward. Excel VBA macro behavior may differ and is not guaranteed by this alternative.

Frequently Asked Questions
Which worksheet receives the imported CSV data?
The macro uses the worksheet that is active when you run it. Select the destination sheet in Excel before choosing Developer > Macros.
What happens if I cancel the file picker?
The macro exits without opening a CSV or changing the destination worksheet.
How does the macro find the last row to import?
It checks the last populated row in both source columns A and B and uses the larger row number. If neither column has data at or below row 57, it displays a message.
Does the import preserve CSV formatting?
No. The macro assigns cell values to the destination range rather than copying source formatting. Excel's interpretation of CSV values when opening the file still applies.
How can I change where the imported data starts?
In the VBA editor, replace destinationSheet.Range("A31") with the desired starting cell, such as destinationSheet.Range("C10"), then save the macro.




