How to export a row from Excel and save as a new CSV file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • massamiya
    New Member
    • Aug 2010
    • 8

    How to export a row from Excel and save as a new CSV file?

    I am trying to export a row from an excel sheet and then save it as a new CSV file but I have hit a
    giant wall, any help would be highly appreciated.
    This is the flow of my program.
    First of all when the save button is clicked some data from a form is saved on the excel sheet since there is
    need for users to correct or add on the saved data.
    I want users to save and close the workbook.
    And also want to save the saved row as a CSV file
    only after users have made changes to the saved excel
    data.
    I don't know where and when to carry out the saving to
    a CSV file. Users don't have to be aware of the saving
    to csv file.
    Below is a portion of the code
    The copy and paste of the row doesn't take place what
    can I be doing wrong ?

    Code:
     Dim xlapp As Excel.Application
     Dim xlbook As Excel.Workbook
     Dim xlsheet As Excel.Worksheet
     Dim SheetName As String
     Dim i As Long
     Dim RSEQ As Integer
     Dim LastRow As Long
     Dim LastColumn As Integer
     Dim strFound As String
     Dim RowNum As Long
     Set xlapp = New Excel.Application
    
       'Open the file
       
       With xlapp
          
             xlapp.Visible = True
          
             Set xlbook = .Workbooks.Open("C:\Drawings_CSV_Spec.xls")                                    
                     
             SheetName = "specialEstimate"
             
             Set xlsheet = .Worksheets("specialEstimate")                                                   
             
             'find Last Row & Column
             LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row                      
             LastColumn = xlsheet.Cells.Find(What:="*", After:=xlsheet.Range("A1"), _
             SearchOrder:=xlByColumns, _
             SearchDirection:=xlPrevious).Column
             'unhide all hidden columns        
             For i = 1 To LastColumn
                 If Columns(i).EntireColumn.Hidden = True Then
                     Columns(i).EntireColumn.Hidden = False
                 End If
             Next i
            
             T_EstimateNum = S_EstimateNum      'S_EstimateNum created by access estimation prog
                         
             '------------------------------------------------------------------------------------------------
             'Find row number of Estimate Number  
             
             strFound = Cells.Find(What:=T_EstimateNum, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
             xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
             
             ' Write data to Excel sheet here 
    
             'Store row number in Variable
             RowNum = ActiveCell.Row
             
             xlsheet.Range("A" & RowNum & ":AH" & RowNum).Select
    
             xlsheet.Columns("C").EntireColumn.Hidden = True
             xlsheet.Columns("D").EntireColumn.Hidden = True
             xlsheet.Columns("E").EntireColumn.Hidden = True
             xlsheet.Columns("F").EntireColumn.Hidden = True
             xlsheet.Columns("G").EntireColumn.Hidden = True
             xlsheet.Columns("O").EntireColumn.Hidden = True
             xlsheet.Columns("R").EntireColumn.Hidden = True                  
             .Visible = True
             xlsheet.Select         
        End With
        ' Below selected range    E.g A1459:AH1459 is saved as a CSV file
        ' T_EstimateNum is used as file Name
          
         strFound = Cells.Find(What:=T_EstimateNum, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
         XlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Activate
                                    
         xlsheet.Range("A" & RowNum & ":AH" & RowNum).Select
         Selection.Copy
         Workbooks.Add
         ActiveSheet.Paste
          
         xlbook.Application.DisplayAlerts = False
                     
         ActiveWorkbook.SaveAs FileName:= _
         "C:\Documents and Settings\designsection\My Documents\" & T_EstimateNum & ".csv" _
         , FileFormat:=xlCSV, CreateBackup:=False
         
         ActiveWorkbook.Close
         Xlbook.Application.DisplayAlerts = True
  • franknagy
    New Member
    • Oct 2011
    • 27

    #2
    Save touched records as concatenated strings

    Put an own Save Button on your worksheet. Write event handlers which disable the ordinary Save menu point of the main menu and the Ctrl-S key combination or redirect them to the click event handler of your own Save button.
    Define a flag for usage in the event handler of closing with false initial value.
    In this event handler
    1. Display a common dialog where the user can enter the name of the CSV file.
    2. Open the selected file for writing.
    3. Identify somehow the cells to be save in CSV format.
    4. For each row do:
    4.1. Concatenate the cells separated with commas into one string.
    4.2. Write the string to the selected file terminated by newline.
    5. Close the file.
    6. Set your "saved" flag to "true".

    Hint:
    I had a similar problem when I had to pass an data from an Excel worksheet to a text file having fixed field widths of 12 characters used by Fortran Read statements.
    I truncated or padded each cells to exactly 12 characters and concatenated them without separators.

    Comment

    • massamiya
      New Member
      • Aug 2010
      • 8

      #3
      Thanks Frank for the prompt response.
      I can't place code on the excel sheet
      because the Excel workbook is used online by
      alot of people and the data is usually input manually
      and I don't have editing rights on the workbook.
      I have been tasked with automating the transfer of data
      from an access Estimation program that I am writing.
      I am new at VBA and don't know how to go about with the automation.

      Comment

      • franknagy
        New Member
        • Oct 2011
        • 27

        #4
        Massamiya,
        Download my tutorial about macros in Word and Excel.

        http://franknagy.atw.h u/Tipp/Macros/Macros_in_Micro soft_Excel_and_ Word.ppt

        Regards
        Frank

        Comment

        Working...