How to Create an Excel VBA Macro to Import CSV Data and Clear Ranges
Question details
The user needs a VBA macro that clears cells A2:L100 in the current workbook, opens a file picker restricted to CSV files, copies the matching range from the selected CSV, pastes it into the original workbook, and closes the source CSV without saving.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating the routine update of a specific spreadsheet range by pulling in new data from a user-selected CSV file.
- Observed behavior
- The user wants a streamlined, one-click automated solution where old data is removed and new data is imported seamlessly from an external file.
Ensure the Developer tab is enabled in your ribbon and that your main workbook is saved in a macro-enabled format (.xlsm) to preserve the VBA code.
Write a Custom VBA Macro for CSV Import
Create a VBA script that utilizes the FileDialog object to pick a CSV, clears the target range, and handles the copy-paste operation automatically.
Using VBA allows you to combine multiple steps into a single button click. The macro will first clear the old data, prompt you to pick a file, safely open it, copy the required range, paste it, and then close the CSV without triggering save prompts.
Press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.
Click 'Insert' in the top menu and select 'Module' to create a blank script window.
Write the command to clear the old data: ThisWorkbook.Sheets("Sheet1").Range("A2:L100").ClearContents.
Use Application.GetOpenFilename("CSV Files (*.csv), *.csv") to prompt the user to select the specific CSV file to import.
Add commands to open the selected CSV, execute Range("A2:L100").Copy, paste it to your main workbook, and finally use ActiveWorkbook.Close SaveChanges:=False to close the CSV file.
Use Power Query as a Reliable Alternative
For recurring CSV imports, Excel's built-in Power Query or text import tools are often more reliable than macros because they handle delimiters, encoding, and row variations natively.
Run VBA Macros and Import Data Easily with WPS Office
WPS Spreadsheet provides excellent support for VBA macros, allowing you to automate repetitive tasks like clearing ranges and importing CSV files exactly as you would in Microsoft Excel.
- 1. Enable Developer Tools: Open WPS Spreadsheet, go to the 'Tools' tab, and access the 'Developer' options.
- 2. Open the VBA Editor: Click on 'Macros' or press ALT + F11 to launch the built-in VBA code editor.
- 3. Insert the Script: Insert a new module and paste your CSV import and range-clearing VBA script.
- 4. Run the Automation: Run the macro to instantly launch the file picker, import your CSV data, and update your active sheet seamlessly.

Frequently Asked Questions
Why does my CSV data paste into a single column when using a VBA macro?
This usually happens when the CSV file isn't parsed correctly by the macro's open command. To fix this, use the Workbooks.OpenText method with the parameter Comma:=True to ensure the data is properly split into columns based on commas.
How do I suppress the clipboard warning when the macro closes the CSV file?
To prevent the 'Do you want to save changes to the clipboard?' prompt, insert the command Application.CutCopyMode = False immediately after your paste operation and before the ActiveWorkbook.Close command.
Can I modify the macro to copy dynamic ranges instead of exactly A2:L100?
Yes. Instead of hardcoding the range, you can dynamically find the last row containing data using a command like LastRow = Cells(Rows.Count, 1).End(xlUp).Row, and then define your copy range as Range("A2:L" & LastRow).
What happens if the user clicks 'Cancel' on the file picker dialog?
If the user cancels, the GetOpenFilename method returns 'False'. You should handle this in your code by adding a check: If Application.GetOpenFilename = False Then Exit Sub. This stops the macro safely without throwing a runtime error.




