CSV import overwriting the next column after the last one in the CSV file

Tanquen

Board Regular
Joined
Dec 14, 2017
Messages
78
Office Version
  1. 365
Platform
  1. Windows
The CSV file only has columns A through F but it is overwriting column G so G is blank after import.

If I open the CSV it stops at column F and I don't see anything in column G.

VBA Code:
Sub Import_Azure_CSV_OLD()
    Dim ws As Worksheet
    Dim csvFilePath As Variant
    Dim lastRow As Long
    
    ' Set the worksheet where you want to paste the data
    Set ws = ThisWorkbook.Sheets("From Azure DB")
    
    ' Prompt the user to select the CSV file
    csvFilePath = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv), *.csv", Title:="Select CSV File")
    
    ' Check if the user selected a file or canceled the dialog
    If csvFilePath = False Then
        MsgBox "No file selected. Import canceled.", vbExclamation
        Exit Sub
    End If
    
    ' Clear existing data in the destination range starting from A9
    ws.Range("A9").CurrentRegion.Clear
    
    ' Import the CSV file starting at row 9
    With ws.QueryTables.Add(Connection:="TEXT;" & csvFilePath, Destination:=ws.Range("A9"))
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFilePlatform = xlWindows
        .Refresh
    End With
    
    ' Make row 9 bold
    ws.Rows(9).Font.Bold = True
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
What is in the worksheet before you import the CSV file?

If there are data in A9 through to Gxx, then this line:
VBA Code:
ws.Range("A9").CurrentRegion.Clear
will wipe it all.

CurrentRegion includes any cells containing data out to bounding rows and columns that are completely blank. So in the following example, ws.Range("A9").CurrentRegion.Clear will clear the entire shaded area. Test it in your sheet by selecting A9 and pressing Ctrl+Shift+8.
1723093511353.png


If that's not the issue then I would open your CSV file in a text editor (like Notepad) and see if the lines have a trailing comma on the end.
 

Attachments

  • 1723093461694.png
    1723093461694.png
    15.9 KB · Views: 1
Upvote 0
Not sure I follow? I'm trying to stop it from deleting or overwriting anything other than the columns it actually has in the CSV.

I have data in column G that I need to keep that it is removing.

Opening the CSV in Excel there is nothing in column G and looking at it in notepad there is no extra comma at the end.


Code:
"System","Sub-System","Equipment","Point","Value","Timestamp"
"CP100","DataHall110","PDU7-02B-1","Output Active Energy Delivered","7282.056","2024-08-07T16:41:48.928Z"

I'm trying to get it to just overwrite columns A-F starting at row 9. Not sure why it deletes what is in column G.
1723129226171.png
 
Last edited:
Upvote 0
I added the X in column H and I and now it deletes everything. It must be the Destination range of A9. I've tried changing it to A9:F9 but it still deletes everything.
 
Upvote 0
I mean that the line
VBA Code:
ws.Range("A9").CurrentRegion.Clear
Is probably the problem.
Try changing it to
VBA Code:
ws.Range(“A9:F100”).Clear
and see what happens.
If that works we can worry about fixing up the 100 to get the last row instead.
 
Upvote 1
Solution
I mean that the line
VBA Code:
ws.Range("A9").CurrentRegion.Clear
Is probably the problem.
Try changing it to
VBA Code:
ws.Range(“A9:F100”).Clear
and see what happens.
If that works we can worry about fixing up the 100 to get the last row instead.
Sorry, I was focused on the Destination range. This seems to work.

VBA Code:
ws.Range("A9:F" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row).Clear

This gives an error:
VBA Code:
ws.Range(“A9:F100”).Clear
 
Upvote 0

Forum statistics

Threads
1,223,847
Messages
6,174,992
Members
452,598
Latest member
jeffreyp

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top