Results 1 to 1 of 1

Output CSV file creates commas down blank rows

Threaded View

  1. #1
    Forum Contributor mcinnes01's Avatar
    Join Date
    05-25-2010
    Location
    Manchester
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    449

    Lightbulb Output CSV file creates commas down blank rows

    Hi,

    I have a spreadsheet I have been working on for a while, however I needed to add some extra functionality and few extra fields in. I did ths, however one function in particular that creates the csv file isn't working properly.

    The code in this module is almost identical to the original workbooks, other than the few extra fields I have added in, but these are to the exact same structure as the originals.

    If I run the macro in the original workbook, and open the output file in notepad it just contains the non blank rows that were in the output range. If I do it in the new workbook, It has the rows from the output range plus ,,,,,,,,,,,,,, all the way down to the bottom of the range.

    I don't understand why this is happening as the code is more or less identical...


    ORIGINAL:

    Option Explicit
    
    Sub CreateCSV()
    'Author:    Jerry Beaucaire, ExcelForum.com
    'Date:      10/8/2010
    'Summary:   Create a specifically formatted CSV file from activesheet
    Dim cell    As Long
    Dim NR      As Long
    Dim wsData  As Worksheet
    Dim wsCSV   As Worksheet
    Dim SaveStr As String
    Dim Deskstr As String
    
    Set wsData = Sheets("OUTPUT")
    Set wsCSV = Worksheets.Add(After:=Sheets(Sheets.Count))
    
    With wsData
    
        wsCSV.Range("A1") = "EMP_ID"
        .Range("A2:A" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("A2")
        
        wsCSV.Range("B1") = "TYPE_ID"
        .Range("TYRng:B" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("B2")
    
        wsCSV.Range("C1") = "ABSENT_ID"
        .Range("ABSRng:C" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("C2")
    
        wsCSV.Range("D1") = "START_DATE"
        .Range("SDRng:D" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("D2")
     
        wsCSV.Range("E1") = "START_TIME"
        .Range("STRng:E" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("E2")
    
        wsCSV.Range("F1") = "END_DATE"
        .Range("EDRng:F" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("F2")
    
        wsCSV.Range("G1") = "END_TIME"
        .Range("ETRng:G" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("G2")
        
        wsCSV.Range("H1") = "DURATION"
        .Range("DRng:H" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("H2")
        
        wsCSV.Range("I1") = "DUR_CAL_DAYS"
        .Range("CALRng:I" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("I2")
    
        wsCSV.Range("J1") = "ACTION_ID"
        .Range("ACTRng:J" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("J2")
        
        wsCSV.Range("K1") = "DESCRIPTION"
        .Range("ADRng:K" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("K2")
        
        wsCSV.Range("L1") = "CERTIFIED"
        .Range("CertRng:L" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("L2")
        
    End With
    
    wsCSV.Move
    
    ActiveSheet.Name = "OSP_CSV"
    
    Deskstr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
              & Application.PathSeparator & "OSP BACKUP"
        
        If Dir(Deskstr, vbDirectory) = "" Then MkDir Deskstr
              
    SaveStr = Deskstr & Application.PathSeparator & ActiveSheet.Name _
            & " - " _
            & Environ("USERNAME") _
            & " - " _
            & Format(Now, " d-m-yy h.mm AM/PM")
    
    ActiveWorkbook.SaveAs Filename:=SaveStr & ".csv", FileFormat:=xlCSVWindows, CreateBackup:=False, local:=True
    
    ActiveWorkbook.Close False
    Worksheets("INPUT").Activate
    
    End Sub

    NEW:


    Option Explicit
    
    Sub CreateCSV()
    'Author:    Jerry Beaucaire, ExcelForum.com
    'Date:      10/8/2010
    'Summary:   Create a specifically formatted CSV file from activesheet
    Dim cell    As Long
    Dim NR      As Long
    Dim wsData  As Worksheet
    Dim wsCSV   As Worksheet
    Dim SaveStr As String
    Dim Deskstr As String
    
    Set wsData = Sheets("OUTPUT")
    Set wsCSV = Worksheets.Add(After:=Sheets(Sheets.Count))
    
    With wsData
    
        wsCSV.Range("A1") = "EMP_ID"
        .Range("EMPRNG:A" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("A2")
        
        wsCSV.Range("B1") = "TYPE_ID"
        .Range("TYRng:B" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("B2")
    
        wsCSV.Range("C1") = "ABSENT_ID"
        .Range("ABSRng:C" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("C2")
    
        wsCSV.Range("D1") = "START_DATE"
        .Range("SDRng:D" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("D2")
     
        wsCSV.Range("E1") = "START_TIME"
        .Range("STRng:E" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("E2")
    
        wsCSV.Range("F1") = "END_DATE"
        .Range("EDRng:F" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("F2")
    
        wsCSV.Range("G1") = "END_TIME"
        .Range("ETRng:G" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("G2")
        
        wsCSV.Range("H1") = "DURATION"
        .Range("DRng:H" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("H2")
        
        wsCSV.Range("I1") = "DUR_CAL_DAYS"
        .Range("CALRng:I" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("I2")
    
        wsCSV.Range("J1") = "ACTION_ID"
        .Range("ACTRng:J" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("J2")
        
        wsCSV.Range("K1") = "DESCRIPTION"
        .Range("ADPRng:K" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("K2")
        
        wsCSV.Range("L1") = "CERT_METHOD_ID"
        .Range("NOTERng:L" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("L2")
        
        wsCSV.Range("M1") = "NOTE_TEXT"
        .Range("FNOTERng:M" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("M2")
        
        wsCSV.Range("N1") = "CERTIFIED"
        .Range("CertRng:N" & Range("A1").End(xlDown).Row).Copy wsCSV.Range("N2")
        
    
    End With
    
    wsCSV.Move
    
    ActiveSheet.Name = "OSP_CSV"
    
    Deskstr = CreateObject("WScript.Shell").SpecialFolders("Desktop") _
              & Application.PathSeparator & "OSP BACKUP"
        
        If Dir(Deskstr, vbDirectory) = "" Then MkDir Deskstr
              
    SaveStr = Deskstr & Application.PathSeparator & ActiveSheet.Name _
            & " - " _
            & Environ("USERNAME") _
            & " - " _
            & Format(Now, " d-m-yy h.mm AM/PM")
    
    ActiveWorkbook.SaveAs Filename:=SaveStr & ".csv", FileFormat:=xlCSVWindows, CreateBackup:=False, local:=True
    
    ActiveWorkbook.Close False
    Worksheets("INPUT").Activate
    
    End Sub
    Last edited by mcinnes01; 11-16-2010 at 10:32 AM.

Thread Information

Users Browsing this Thread

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

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