+ Reply to Thread
Results 1 to 2 of 2

problem with save as?

Hybrid View

njs27 problem with save as? 12-06-2013, 02:16 PM
njs27 Re: problem with save as? 12-06-2013, 03:49 PM
  1. #1
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    problem with save as?

    In the following code (full subroutine at the bottom) I want to save a worksheet row by row into textfiles. Row 1 has the headers, Col 1 has the filename for the row.

    It works fine until I get to the execution of this line inside the For loop:
    wbNew.SaveAs fileName:=strFile, FileFormat:=xlText, CreateBackup:=False
    And the error I get is a run-time error "Automation error", and when I hit DEBUG, it highlights the line of code above it:
    wb.ActiveSheet.Rows(r).Copy wbNew.Sheets(1).Rows(1)
    As far as I can tell, that line executes correctly, so I'm confused as to why it would highlight that line.

    I've used the Saveas code in other subroutines and have been successful.
    Can anyone figure out what's wrong?

    Thanks in advance!


    Full code here:

      Sub SaveRowsAsTXT()
        Dim wb As Excel.Workbook, wbNew As Excel.Workbook
        Dim r As Long
        Dim strFolder As String
        Dim strFile As String
        Dim fName As String
        Dim LastCol As Long
        Dim LastRow As Long
       
        
        strFolder = "L:\test\" 'folder where files will be saved
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False 'will overwrite existing files without asking
            
        ActiveWorkbook.Worksheets("Sheet1").Visible = True 'currently hidden
        ActiveWorkbook.Worksheets("Sheet1").Copy
        
        Set wb = ActiveWorkbook
            
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
            
        Set wbNew = Workbooks.Add 'adds blank workbook
        wb.Activate
            
        For r = 2 To LastRow
            fName = Cells(r, 1).Value
            strFile = strFolder & fName & ".txt"
            'copy row to new wkbk
            wb.ActiveSheet.Rows(r).Copy wbNew.Sheets(1).Rows(1)
            'save
            wbNew.SaveAs fileName:=strFile, FileFormat:=xlText, CreateBackup:=False
            'close
            wbNew.Close
        
        Next
        
        ActiveWorkbook.Worksheets("Sheet1").Visible = False
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
    End Sub
    This is a crosspost from: http://www.mrexcel.com/forum/excel-q...blem-save.html

  2. #2
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    Re: problem with save as?

    Figured out that I had to move
    Set wbNew = Workbooks.Add 'adds blank workbook
            wb.Activate
    into the For loop,
    add
    Dim ws As Excel.Worksheet
    and replace
    wb.ActiveSheet.Rows(r).Copy wbNew.Sheets(1).Rows(1)
    with
    ws.Rows(r).Copy wbNew.Sheets(1).Rows(1)

+ 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. Save As Problem
    By Dean81 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-21-2011, 06:27 AM
  2. Save As problem
    By Bob@Sun in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2010, 05:57 AM
  3. Problem with 'save as'
    By Boz in forum Excel General
    Replies: 1
    Last Post: 04-07-2006, 03:40 PM
  4. Save as problem with Save In drop down box
    By Doug in forum Excel General
    Replies: 3
    Last Post: 03-16-2006, 10:50 AM
  5. save as problem
    By Gauss_chan in forum Excel General
    Replies: 2
    Last Post: 03-05-2006, 08:24 AM

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