0

I am currently creating a macro that needs to be able to open a .csv file from a specified path, copy all of the data, and paste it into an excel document. These documents have IDs that contain leading zeroes and so I need to be able to retain them. My current manual process includes:

  • opening a new excel workbook
  • setting all cells to Text data type
  • opening a .csv file in notepad++
  • c&p all into the workbook
  • use Text to Columns for formatting
  • delete the entire workbook contents
  • re-c&p the data from np++ into excel and QA for the green triangle that denotes a number is stored as text

I have tried other methods that utilize importing the data, however those methods do not seem to be retaining the leading zeroes. Thus, I am seeking a possible way to just select all from a .csv in notepad++ and c&p the contents into a workbook.

Sub cdx()

    Dim wb As Workbook
        Set wb = ThisWorkbook

    Workbooks.Add
    
    Dim data_range As Range
        'Selects all cells in the workbook
            Cells.Select
        'Sets all cells in workbook to a variable
            Set data_range = Selection
            data_range.NumberFormat = "@"

    'Declare variables
        'file_name: partial file name to be opened
            Dim file_name As Variant
        'file_path: directory file path to open the files
            Dim file_path As String
        'csv: object that opens up based on application type
            Dim csv As Object
                'creates an object to open up .csv files in NotePad++
                    Set csv = CreateObject("shell.application")

        file_path = "C:\Users\User Name\Desktop\"
        file_name = Dir(file_path & "Inquiries*")

        If file_name <> "" Then
            csv.Open (file_path & file_name)
        End If
        
        Dim csv_data
            csv_data = file_name.readall
            wb.Sheets(1).Range("A1").Value = csv_data

End Sub
5
  • 1
    Very curious why you're using Notepad++ at all. Excel can work with CSV's directly, including preserving leading zeros Commented Nov 3, 2021 at 17:08
  • As I mentioned, I used other methods that I found from similar questions importing directly from a .csv, however those methods did not retain the leading zeroes, which is why I am turning to the approach of c&p from a notepad type application directly. If you have a thread that works directly with CSVs as you mention, while retaining the leading zeroes, I'll gladly close out this post. Commented Nov 3, 2021 at 17:13
  • 2
    stackoverflow.com/questions/67894852/… Commented Nov 3, 2021 at 17:14
  • You say you're using some text/column formatting in Excel and then saving back to csv. Couldn't you maybe do it all directly in Notepad++ using clever search & replace, or maybe using the CSV Lint plug-in? github.com/BdR76/CSVLint Commented Nov 3, 2021 at 21:56
  • Hi @BdR, I believe I was unclear then, I am not resaving to .csv, I want to be able to convert my manual process into an automated one which includes: copy the contents of a .csv file (opened in np++) and paste the data into a new excel workbook (that has already been set to text for all cells), Text to Columns to check off the comma, semicolon delimiter, and delete all of the workbook contents then repaste from np++ because sometimes after Text to Columns, the leading 0s aren't retained so repasting always does the trick. Commented Nov 4, 2021 at 20:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.