Edit CSV file adding double quote via macro

MrRed_07

New Member
Joined
Jul 21, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello, i have created an excel file where there is a button and when the user presses it, it prompts a window for the user to select a CSV file to be edited.
After the file is selected i make some changes and save it to a new csv file that works fine, but i struggle to add double quotes (") to the K column. The data in the K column are numbers of this format : 0000012345.
I have tried using this :

VBA Code:
 Dim k As Range
 For Each k In Range("K2", Range("K" & Rows.Count).End(xlUp))
        k.Value = Chr(34) & k.Value & Chr(34)
 Next k

but the result when i open the new csv file are in this format : """0000012345""". I want to be like this : "0000012345".

Any idea how may i do it ?
Thanks in advance.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Something like this perhaps
VBA Code:
    Dim k As Range
    Dim S As String

    For Each k In Range("K2", Range("K" & Rows.Count).End(xlUp))
        S = Trim(Replace(CStr(k.Value), Chr(34), ""))
        k.Value = Chr(34) & S & Chr(34)
    Next k
 
Upvote 0
Something like this perhaps
VBA Code:
    Dim k As Range
    Dim S As String

    For Each k In Range("K2", Range("K" & Rows.Count).End(xlUp))
        S = Trim(Replace(CStr(k.Value), Chr(34), ""))
        k.Value = Chr(34) & S & Chr(34)
    Next k
still tha same outcome
 
Upvote 0
The content of the CSV file that opens to be edited looks like this :
VBA Code:
CITY NAME;5278 90** **** 4303;0021417951;1;975;18/07/2023;48,00;-0,29;47,71;19/07/2023;0000062439
After the edit i want to look like this :
Code:
CITY NAME;5278 90** **** 4303;0021417951;1;975;18/07/2023;48,00;-0,29;47,71;19/07/2023;"0000062439"
 
Upvote 0
but the result when i open the new csv file are in this format : """0000012345""". I want to be like this : "0000012345".

I think I misunderstood what you wanted. Excel is going to save to csv as either 0000012345 or """0000012345""", depending on cell contents and format. Unless someone comes along with an easier suggestion, to get "0000012345" in a CSV file you may have to write code to build your ownsave routine to read each row and write a delimited line to a text file.

Are you sure you need to go to all that trouble? For example, if you open the file with excel, it will import both of these lines the same way

CITY NAME;5278 90** **** 4303;0021417951;1;975;18/07/2023;48,00;-0,29;47,71;19/07/2023;0000062439
CITY NAME;5278 90** **** 4303;0021417951;1;975;18/07/2023;48,00;-0,29;47,71;19/07/2023;"0000062439"

1689953262074.png
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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