+ Reply to Thread
Results 1 to 7 of 7

Modify code to work with xls and xlsx files

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-16-2004
    Posts
    125

    Modify code to work with xls and xlsx files

    Hello,

    I have a code (probably found it on this forum) that opens up an Open Dialog Box to open another Excel Workbook. When you select the Excel Workbook you want to open, it copies data from the first workbook to the new one and saves the file. It works great except it will only open .xls files. My work is starting to use .xlsx files now and I need to update the code. Can someone help modify this code to be able to open .xls and .xlsx files? Thanks for any help and here is the code.


    Private Sub CommandButton2_Click()
    
    MsgBox "Select File to Open."
    
    Dim wb As Workbook
    
    ' speed up by turning screenupdating off
        Application.ScreenUpdating = False ' turn off the screen updating
    
    ' set workbooks
        Set MasterWB = ActiveWorkbook
    
    ' open file browser and select XLS files only, end macro if no file is selected
        newFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Select COMMENTS report for import")
        If newFN = False Then
            MsgBox "No file selected."
            Exit Sub
        End If
    
    ' open selected workbook in read only and copy all cells of worksheet 1
        Set wb = Workbooks.Open(newFN, True, False)
        
        Sheets("Setup").Select
        Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("D6").Select
        ActiveCell.FormulaR1C1 = "='Record'!R[-5]C[1]"
        Range("D6").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("E40").Select
        ActiveCell.FormulaR1C1 = "=SUM(R[-10]C:R[-1]C)"
        Range("E41").Select
        Sheets("Record").Select
        Range("A1").Select
    ' close workbook without saving changes and free memory
        'wb.Close False
        'Set wb = Nothing
        
    ' turn screenupdating back on
        Application.ScreenUpdating = True ' turn on the screen updating
        
        Sheets("Setup").Select
        Range("A1:G67").Select
        Application.CutCopyMode = False
        Selection.Copy
        
        Sheets("Record").Select
        
    End Sub
    Last edited by CJ-22; 08-31-2013 at 11:23 AM.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Modify code to work with xls and xlsx files

    Try changing
    (*.xls), *.xls", Title:="Select COMMENTS report for import")
    to
    (*.xlsx), *.xlsx", Title:="Select COMMENTS report for import")
    first workbook to the new one and saves the file
    I don't see where this code saves your workbook FYI
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Contributor
    Join Date
    12-16-2004
    Posts
    125

    Re: Modify code to work with xls and xlsx files

    Thanks for the reply XeRo Solus. I tried your suggestion, but it did not change anything. Either way I can only see .xls files in the dialog box. Even if there are both .xls files and .xlsx files in the folder I am viewing, I can only see the .xls files. I don't know why. Also, you are right. It does not save the file automatically. The file is left open and I manually save it. Sorry for the confusion. Any other suggestions?

  4. #4
    Forum Contributor
    Join Date
    12-16-2004
    Posts
    125

    Re: Modify code to work with xls and xlsx files

    Hi Jindon,

    Thanks for the suggestion. I tried it but I can still only view .xls files in the Open Dialog Box. The drop down arrow to change file types only has the .xls option, but I do not know why.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,608

    Re: Modify code to work with xls and xlsx files

    Change to
    newFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls", Title:="Select COMMENTS report for import")

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,608

    Re: Modify code to work with xls and xlsx files

    Then your Excel is something wrong....

    I can see .xls, .xlsx, xlsm etc, any files starts from .xls

  7. #7
    Forum Contributor
    Join Date
    12-16-2004
    Posts
    125

    Re: Modify code to work with xls and xlsx files

    Sorry Jindon, you are right. Somehow I copied and pasted incorrectly. It is now working great. Thanks for all the help.

+ 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. modify SUMIF to work w/ closed files
    By Mathiasben in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-26-2013, 11:29 AM
  2. Merge Data from different .xlsx files & different sheet to a new .xlsx
    By QcSylvanio in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-03-2012, 01:11 PM
  3. How to add the columns data of several xlsx files of a folder in another xlsx file
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-25-2012, 04:29 AM
  4. VBA code to turn CSV files into XLSX
    By bryanbak3 in forum Excel General
    Replies: 1
    Last Post: 04-06-2012, 06:24 PM
  5. [SOLVED] modify xlsx files with excel 2003
    By mallorypr in forum Excel General
    Replies: 0
    Last Post: 07-10-2006, 10:19 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