+ Reply to Thread
Results 1 to 11 of 11

Workbook.Open Filename procedure

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Workbook.Open Filename procedure

    I am trying to modify my macro for the import of XML files. I want the new macro to open CSV files. I attempted it but encountered a problem with the syntax for the Open Filename procedure.

    Just below is the code with the problem. Further down is the whole script.
    Please somebody assist in debugging this problem.

    'Import files
    
            For i = LBound(FileName) To UBound(FileName)
                Sheets(i).Select
                FileName2 = FileName(i)
                ActiveWorkbook.Open FileName:=FileName2
            Next i
    Sub ImportMultipleFiles()
    
    'Define variables
        
        Dim Filt As String
        Dim FilterIndex As Integer
        Dim Title As String
        Dim FileName As Variant
        Dim FileName2 As Variant
        Dim i As Integer
        Dim j As Integer
        Dim Msg As String
    
    'Select files to Import
    
        'Set up list of file filters
            Filt = "CSV Files (*.csv) ,*.csv,"
    
        'Display * * by default
            FilterIndex = 5
    
        'Set the dialog box caption
            Title = "Select a File to Import"
    
        'Get the file name
            FileName = Application.GetOpenFilename _
                (FileFilter:=Filt, _
                FilterIndex:=FilterIndex, _
                Title:=Title, _
                MultiSelect:=True)
    
        'Exit if dialog box canceled
            If Not IsArray(FileName) Then Exit Sub
    
    'Create sheets, one for each file to import
           
        For j = LBound(FileName) To UBound(FileName) - 1
            Sheets.Add After:=Sheets(j)
            Sheets(j + 1).Select
            Sheets(j + 1).Name = j + 1
        Next j
    
    'Import files
    
            For i = LBound(FileName) To UBound(FileName)
                Sheets(i).Select
                FileName2 = FileName(i)
                ActiveWorkbook.Open FileName:=FileName2
                    
                Cells.Select
                Selection.RowHeight = 12.5
                Selection.ColumnWidth = 15
        
            Next i
        
    End Sub
    Last edited by dschmitt; 06-16-2010 at 10:30 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with Workbook.Open Filename procedure

    found the problem.

    For i = LBound(FileName) To UBound(FileName)
          Sheets(i).Select
          FileName2 = FileName(i)
          Workbooks.Open FileName:=FileName2
          Sheets(i).Name = i
    Next i

  3. #3
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with Workbook.Open Filename procedure

    I have another problem with the Workbooks.Open Filename procedure.

    The script line below opens the comma delimited CSV file in a new workbook. That means if I execute the macro in one workbook the data will appear in a newly created workbook.

    That's bad. I need the data in the workbook in which I execute the macro. Is there a way to modify the script below so that I can achieve that?

    I tried ActiveWorkbook and ActiveWorksheet but both don't work.

    Workbooks.Open FileName:=FileName2

  4. #4
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Problem with Workbook.Open Filename procedure

    One work-around can be to copy data from new workbook, paste in your workbook (ehich has macro), and close the new workbook.

    hth
    Ajay

  5. #5
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with Workbook.Open Filename procedure

    yes, that one I already thought about. The other one is to import the data. I am working on that too. But with that I am having a problem. I may have to ask a question about that tomorrow.

    Anyway, I hope that there is a more elegant way than to copy the data and delete the workbook.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,280

    Re: Problem with Workbook.Open Filename procedure

    You either copy or you import; no other choices since if you open a workbook, you obviously get a new workbook opened!
    Remember what the dormouse said
    Feed your head

  7. #7
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with Workbook.Open Filename procedure

    I was afraid I would receive this sobering answer abruptly pulling me out of my dream
    Thanks.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,280

    Re: Problem with Workbook.Open Filename procedure

    That may be the first time I've ever been accused of sobering things up...

  9. #9
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with Workbook.Open Filename procedure

    I decided to go with the open file method.

    The below macro does copy the data from the opened csv file to my main workbook. However, closing the csv workbook gives me a "script out of range error".
    Please somebody help to debug this step. Also please include a handler that deals with the keep/not keep info in the clipboard at closing.

    Below is the line that gives me the error. Furtherdown is the complete sub. Attached is a test.csv file.

    Workbooks(FileName).Close
    Sub ImportMultipleFiles()
    
        Application.ScreenUpdating = False
    
    'Define variables
        
        Dim Filt As String
        Dim FilterIndex As Integer
        Dim Title As String
        Dim FileName As Variant
        Dim MainWorkbook As String
    
        MainWorkbook = ActiveWorkbook.Name
    
    'Select files to Import
    
        Filt = "CSV Files (*.csv) ,*.csv,"
        FilterIndex = 5
        Title = "Select a File to Import"
        FileName = Application.GetOpenFilename _
            (FileFilter:=Filt, _
            FilterIndex:=FilterIndex, _
            Title:=Title, _
            MultiSelect:=False)
    
    'Import file
    
        Workbooks.Open FileName:=FileName
        Cells.Select
        Cells.Copy
    
        Workbooks(MainWorkbook).Activate
        Sheets("Sheet1").Select
        ActiveSheet.Paste
        
        Workbooks(FileName).Close
        
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    Last edited by dschmitt; 06-16-2010 at 10:01 PM.

  10. #10
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with Workbook.Open Filename procedure

    I solved the problem that I had with closing the csv workbook. See script below.
    But I am still looking for code to handle the question about what to do with the large data in the clipboard.

    Sub ImportMultipleFiles()
    
        Application.ScreenUpdating = False
    
    'Define variables
        
        Dim Filt As String
        Dim FilterIndex As Integer
        Dim Title As String
        Dim FileName As Variant
        Dim MainWorkbook As String
        Dim csvWorkbook As String
    
        MainWorkbook = ActiveWorkbook.Name
    
    'Select files to Import
    
        Filt = "CSV Files (*.csv) ,*.csv,"
        FilterIndex = 5
        Title = "Select a File to Import"
        FileName = Application.GetOpenFilename _
            (FileFilter:=Filt, _
            FilterIndex:=FilterIndex, _
            Title:=Title, _
            MultiSelect:=False)
    
    'Import file
    
        Workbooks.Open FileName:=FileName
        csvWorkbook = ActiveWorkbook.Name
        Cells.Select
        Cells.Copy
    
        Workbooks(MainWorkbook).Activate
        Sheets("Sheet1").Select
        ActiveSheet.Paste
        
        Workbooks(csvWorkbook).Close
        Range("A1").Select
        
        Application.ScreenUpdating = True
    
    End Sub

  11. #11
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with Workbook.Open Filename procedure

    solved it.

    Sub ImportMultipleFiles()
    
        Application.ScreenUpdating = False
    
    'Define variables
        
        Dim Filt As String
        Dim FilterIndex As Integer
        Dim Title As String
        Dim FileName As Variant
        Dim MainWorkbook As String
        Dim csvWorkbook As String
    
        MainWorkbook = ActiveWorkbook.Name
    
    'Select files to Import
    
        Filt = "CSV Files (*.csv) ,*.csv,"
        FilterIndex = 5
        Title = "Select a File to Import"
        FileName = Application.GetOpenFilename _
            (FileFilter:=Filt, _
            FilterIndex:=FilterIndex, _
            Title:=Title, _
            MultiSelect:=False)
    
    'Import file
    
        Workbooks.Open FileName:=FileName
        csvWorkbook = ActiveWorkbook.Name
        Cells.Select
        Cells.Copy
    
        Workbooks(MainWorkbook).Activate
        Sheets("Sheet1").Select
        ActiveSheet.Paste
        
        Workbooks(csvWorkbook).Activate
        Range("A1").Copy
        ActiveWorkbook.Close
            
        Range("A1").Select
        
        Application.ScreenUpdating = True
    
    End Sub

+ 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