+ Reply to Thread
Results 1 to 1 of 1

Saving csv file - help needed putting code together

  1. #1
    Registered User
    Join Date
    06-07-2017
    Location
    Nottingham, UK
    MS-Off Ver
    2013
    Posts
    15

    Saving csv file - help needed putting code together

    Hi All,
    I'm hoping you can help as I've got so far and hit a problem (as I'm new to this)

    I have some code that saves a range that it works out is active based on the number of rows and saves it to a specific location, however when testing I found that it wasn't putting " marks where needed and not all columns need " marks.

    Looking at all the notes in different forums, getting " to work correctly is painful, so to get around this I concatenate the relevant columns adding comma's and quotes as needed into a single column that I now want to save.

    I tried changing the original code and it puts in a double "" where I only want a "... it looks ok when you open the new csv file in excel, but when you open the file in notepad.. they they are.

    I've found some code that saves it nicely as a csv file, putting the correct number of " in it, but does not pick up the only elements I need from the original code i.e. Identify the range tthat is needed as rows that contain values within column Z OR to save the file with the right name format in the same folder as the active sheet.

    Not sure if it's better to adapt the new code or the original and I've been trying to stitch them together unsuccessfully- can anyone help?

    I also need to work out if I can concatenate in VBA rather than in columns in excel as it's a more robust solution
    Thanks


    ORIGINAL CODE
    'SAVING - Saves the file as csv (no validation check)
    Sub ORIGINAL_Save_CSV_6_Tabs_CalcRange()

    'Turn off screen updating for performance and prevent dizziness
    Application.ScreenUpdating = False

    Dim MyPath As String
    Dim MyFileName As String
    Dim WB1 As Workbook, WB2 As Workbook

    Set WB1 = ActiveWorkbook
    MyFileName = "ProdTabData-" & Format(Date, "yyyymmdd-") & Format(Time, "hhmmss")
    FullPath = WB1.Path & "\" & MyFileName

    'System detected output range, based on
    Application.ScreenUpdating = False

    Range("Z5:Z" & Cells(Rows.Count, 1).End(xlUp).Row).Copy
    Set WB2 = Application.Workbooks.Add(1)
    WB2.Sheets(1).Range("Z5").PasteSpecial xlPasteValues
    Application.DisplayAlerts = False

    'save file
    If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
    With WB2
    .SaveAs Filename:=FullPath, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
    End With
    Application.DisplayAlerts = False

    'Turn on screen updating
    Application.ScreenUpdating = True

    End Sub



    NEW SAVE CODE - that I want to add the dynamic range identification and save location into
    'http://www.excel-easy.com/vba/examples/write-data-to-text-file.html
    Sub test4()
    Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
    myFile = Application.DefaultFilePath & "\sales.csv"

    Set rng = Selection

    Open myFile For Output As #1
    For i = 1 To rng.Rows.Count
    For j = 1 To rng.Columns.Count
    cellValue = rng.Cells(i, 1).Value
    If j = rng.Columns.Count Then
    Write #1, cellValue
    Else
    Write #1, cellValue,
    End If
    Next j
    Next i
    Close #1

    End Sub



    3rd option - open notepad and save directly into there, but need to save location and dynamic selection adding


    I'm hoping the above are the ingredients... I just don't know what order to put them in

    Thanks
    AE

    NOT SOLVED, BUT APPROACHED IT A DIFFERENT WAY - created new question about to be raised separately
    Last edited by enthusiastic amateur; 06-12-2017 at 11:48 AM. Reason: added more detail about the benefit of the new save to csv code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 06-15-2016, 06:24 PM
  2. VBA code for saving a worksheet as UTF-8 CSV file
    By Air^Canada in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-14-2016, 10:36 AM
  3. Code Fails Saving File
    By breader in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2013, 03:06 AM
  4. Saving file as CSV corrupts when using code
    By OWA in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-14-2013, 12:53 PM
  5. Replies: 5
    Last Post: 03-24-2012, 03:41 AM
  6. Saving file without code, problems
    By bhofsetz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2008, 02:53 PM
  7. saving code to csv file
    By Scottmk in forum Excel General
    Replies: 2
    Last Post: 06-20-2007, 09:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1