+ Reply to Thread
Results 1 to 2 of 2

Newbie needing help with basic macro!

  1. #1
    Registered User
    Join Date
    04-24-2008
    Posts
    1

    Unhappy Newbie needing help with basic macro!

    Hi,

    I'm attempting to use a script I found on the net for a macro to batch convert a large number of XLS files to CSV format, specifically a particular sheet from each file, then output these CSV files to a separate folder with the same name as the original XLS files but with the .csv extension.

    However, whenever I run the macro I get a "Run-time error '9': Subscript out of range" error, and in debug mode it highlights this line:

    Workbooks(vFile).Close SaveChanges:=False

    Why would I be getting this error when just trying to close the file?
    I'm a complete newbie to this so sorry if it's something blatantly obvious and simple!!

    Any help would be appreciated!!

    The complete script is as follows:

    Sub ProcessXLSFilesInDirectory()
    Dim aFiles() As String, iFile As Integer
    Dim stFile As String, vFile As Variant
    Dim stDirectory As String
    Dim stCSV As String
    Dim stCSVDir As String


    ' first build an array of the files and then process them
    ' this is because you may upset the Dir function if you save a file


    stDirectory = "C:\Dave\Completion reports\" ' name of directory to look in
    stCSVDir = "C:\Dave\Completion reports\CSV\" ' where to put the CSV files
    ' use Dir function to find XLS files in Directory
    stFile = Dir(stDirectory & "*.XLS")
    If stFile = "" Then Exit Sub ' no files to process
    Do While stFile <> ""
    ' add to array of files
    iFile = iFile + 1
    ' add one element to the array
    ReDim Preserve aFiles(1 To iFile)
    aFiles(iFile) = stFile
    stFile = Dir() ' gets next file
    Loop


    ' now process the files
    Application.DisplayAlerts = False ' no messages about overwriting
    For Each vFile In aFiles
    Workbooks.Open stDirectory & vFile
    Sheets("PLUMA ").Select
    stCSV = Application.Substitute(vFile, ".xls", ".csv")
    Workbooks(vFile).SaveAs stCSVDir & stCSV, FileFormat:=xlCSV
    Workbooks(vFile).Close SaveChanges:=False
    Next vFile

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Please read the forum rules (see my signature for linik)....

    You need to adjust your title to be more descriptive of the question.

    You also need to enclose all code snippets within Code tags....
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

+ Reply to Thread

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