Results 1 to 3 of 3

Close workbook with use of filename?

Threaded View

  1. #1
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Close workbook with use of filename?

    Hi there,

    I currently use the following VBA code to find the most recent file within a folder, open it and copy the data to another sheet. I'm looking for a way to close it again afterwards.

    Private Sub Workbook_Open()
        Application.ScreenUpdating = False
        Sheets("Price List").Visible = True
               
      Dim CSV_file As Object
      Dim CSV_folder As String
      Dim file_ext As String
      Dim FSO As Object
      Dim most_recent As Object
      
        file_ext = "csv"
        CSV_folder = "DIRECTORY/FILENAME"
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
          For Each CSV_file In FSO.GetFolder(CSV_folder).Files
            If FSO.GetExtensionName(CSV_file) = file_ext Then
              If most_recent Is Nothing Then Set most_recent = CSV_file
              If CSV_file.DateLastModified > most_recent.DateLastModified Then
                Set most_recent = CSV_file
              End If
            End If
          Next CSV_file
          
       If Not most_recent Is Nothing Then Workbooks.Open most_recent
       
       Set FSO = Nothing
       
        Cells.Select
        Selection.Copy
        Windows("ORDERING.xls").Activate
        Sheets("Price List").Select
        Range("A1").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Sheets("Price List").Visible = False
        Application.ScreenUpdating = True
        Sheets("Order").Select
    
    MsgBox "Please close the spreadsheet F4122_catalogue"
       
       
       End Sub
    Currently I am relying on the user by presenting them with a prompt box, I would like to avoid this altogether. Does anyone have any suggesstions?

    in addition I'm also wondering if it would be possible to enter the file name (actually part of the filename) returned with this code into a specific cell within the worksheet?

    Many thanks in advance.
    Last edited by Kramxel; 04-13-2012 at 08:55 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