+ Reply to Thread
Results 1 to 13 of 13

Use Macro to open a file with excel (underfined type of file)

Hybrid View

  1. #1
    Registered User
    Join Date
    07-29-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    34

    Use Macro to open a file with excel (underfined type of file)

    Dear Professionals

    I wish to write a macro to do the following steps:

    1. open a (fileA) (undefined type) with excel.
    2. After opened the file, copying the content into the active workbook (Database).

    I have tried the following code, it fails

    Sub openQIR()
    '
    ' openQIR 巨集
    '
    
      
    Dim wbSource As Workbook, wbDestination As Workbook
              
               
        Set wbSource = Workbooks.Open("C:\user\desktop\tatfish\fileA.xlsx")
        Set wbDestination = ThisWorkbook
        
        With wbSource.Sheets(1).Select
        Range("A1:H1", Range("A20000:H20000").End(xlUp)).Select
        Selection.Copy
            
        Windows("Database.xlsm").Activate
        Sheets("2").Select
        Range("A" & Rows.Count).End(xlUp).Offset(2).Select
        Selection.PasteSpecial Paste:=xlPasteValues
        Selection.PasteSpecial Paste:=xlPasteFormats
        Application.CutCopyMode = False
        
    End With
            
        Sheets("1").Select
        'Close source:
        wbSource.Close
    '
    
    End Sub
    I understand it must fail because the type of fileA is not .xlsx
    The file A is underfined type but it can be opened with excel (currently do it manually).

    Please kindly teach me to amend my code for making it done.
    Thousands thanks.
    Last edited by tatfish87; 12-17-2014 at 05:16 AM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Use Macro to open a file with excel (underfined type of file)

    Set wbSource = Workbooks.Open("C:\user\destop\fileA.xlsx")'-Change the extension to the proper one.
    Set wbDestination = ThisWorkbook'-You are not in chronological order.-this has to be before you open the other workbook.
    You haven't explained what fails!!

  3. #3
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Use Macro to open a file with excel (underfined type of file)

    check this line:-

        Set wbSource = Workbooks.Open("C:\user\destop\fileA.xlsx")
    destop?        desktop
    user\destop?   user\tatfish\desktop

    See if this opens your file NB the :-

    Sub Macro1()
    Dim fso, fsoFolder, fsoFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = fso.GetFolder(ActiveWorkbook.Path)
    ' replace with, if macro not in same folder " Set fsoFolder =Workbooks.Open("C:\user\tatfish\desktop"
    
    For Each fsoFile In fsoFolder.Files
    
    strname = fsoFile.Name
    
    If Left(strname, 6) = "fileA." Or Right(strname, 5) = ".xlsx" Then
    
        Workbooks.Open Filename:=ActiveWorkbook.Path & "\" & strname
        'ActiveWindow.Close False
    End If
    
    Next
    
    200 ' Exit
    End Sub
    Last edited by mehmetcik; 12-17-2014 at 05:12 AM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  4. #4
    Registered User
    Join Date
    07-29-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    34

    Re: Use Macro to open a file with excel (underfined type of file)

    Dear davesexcel

    I tried both orders, its the same

    the error should be due to the line

     Set wbSource = Workbooks.Open("C:\user\destop\fileA.xlsx")
    thanks

  5. #5
    Registered User
    Join Date
    07-29-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    34

    Re: Use Macro to open a file with excel (underfined type of file)

    Dear Mehmetcik

    I just typing an example, sorry for that. It fails at the line

    Set wbSource = Workbooks.Open("C:\user\destop\fileA.xlsx")
    the reason is that, fileA is not a excel file. But i wish to open it with excel.

    Thanks

  6. #6
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: Use Macro to open a file with excel (underfined type of file)

    Quote Originally Posted by tatfish87 View Post
    Dear Mehmetcik

    ...........]

    the reason is that, fileA is not a excel file. But i wish to open it with excel.

    Thanks
    When you use the macro recorder to open the file what does the code look like?

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Use Macro to open a file with excel (underfined type of file)

    See if this opens your file NB the :-

    
    Sub Macro1()
    Dim fso, fsoFolder, fsoFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = fso.GetFolder(ActiveWorkbook.Path)
    ' replace with, if macro not in same folder " Set fsoFolder =Workbooks.Open("C:\user\tatfish\desktop"
    
    For Each fsoFile In fsoFolder.Files
    
    strname = fsoFile.Name
    
    If Left(strname, 6) = "fileA." Or Right(strname, 5) = ".xlsx" Then
    
        Workbooks.Open Filename:=ActiveWorkbook.Path & "\" & strname
        'ActiveWindow.Close False
    End If
    
    Next
    
    200 ' Exit
    End Sub

  8. #8
    Registered User
    Join Date
    07-29-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    34

    Re: Use Macro to open a file with excel (underfined type of file)

    Dear mehmetcik

    I have tried your code with little amendment for my path as follow:

    Sub Macro1()
    Dim fso, fsoFolder, fsoFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = Workbooks.Open("C:\Users\tatfish\Desktop\ABC20141216")
    
    ' replace with, if macro not in same folder " Set fsoFolder =Workbooks.Open("C:\user\tatfish\desktop"
    
    ' if same folder, Set fsoFolder = fso.GetFolder(C:\Users\tatfish\Desktop\database)
    
    For Each fsoFile In fsoFolder.Files
    
    strname = fsoFile.Name
    
    If Left(strname, 6) = "ABC20141216." Or Right(strname, 5) = ".xls" Then
    
        Workbooks.Open Filename:=ActiveWorkbook.Path & "\" & strname
        'ActiveWindow.Close False
    End If
    
    Next
    
    200 ' Exit
    End Sub
    After i run the macro, the file is opened, but an error occur at this line
    For Each fsoFile In fsoFolder.Files
    It still open the file successfully, although the format inside is a bit strange.

    For further clarification, "database" is the excel with the macro inside.

    ABC20141216 is the fileA.

    Thanks
    Last edited by tatfish87; 12-17-2014 at 05:53 AM.

  9. #9
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Use Macro to open a file with excel (underfined type of file)

    
    Sub Macro1()
    Dim fso, fsoFolder, fsoFile
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = Workbooks.Open("C:\Users\tatfish\Desktop\ABC20141216")
    
    ' replace with, if macro not in same folder " Set fsoFolder =Workbooks.Open("C:\user\tatfish\desktop"
    
    ' if same folder, Set fsoFolder = fso.GetFolder(C:\Users\tatfish\Desktop\database)
    
    On Error resume next
    For Each fsoFile In fsoFolder.Files
    
    strname = fsoFile.Name
    
    If Left(strname, 6) = "ABC20141216." Or Right(strname, 5) = ".xls" Then
    
        Workbooks.Open Filename:=ActiveWorkbook.Path & "\" & strname
    
    on error goto 0
    
    
        'ActiveWindow.Close False
    End If
    
    Next
    
    200 ' Exit
    End Sub

  10. #10
    Registered User
    Join Date
    07-29-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    34

    Re: Use Macro to open a file with excel (underfined type of file)

    Dear Mehmetcik

    Thank you, thank error is no longer happen. It comes to another strange thing after the file is opened.

    When the file is opened, there are many column and rows, the data are allocated in the respective cells correctly (the same as if i open the file manually with excel program).

    some of the columns are "dates", i mean the information of the cell is a "date"
    some are fine, but some of them are wrong format.

    For example, some dates are "13-12-14" but some are "9/1/2013", i have tried to select them and manually change format, but it fails; regarding this, I realize that is a small green triangle show at the cells which have bad format "13-12-14". I have to manually click on the green triangle to fix the cell one by one. (I would like to have all date in the format of dd/mm/yyyy)

    Can the macro cater for this?

    Thank you very much.
    Last edited by tatfish87; 12-20-2014 at 06:00 AM.

  11. #11
    Registered User
    Join Date
    07-29-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    34

    Re: Use Macro to open a file with excel (underfined type of file)

    Dear Mehmetcik

    Sorry, have i made myself clear about the last problem? thanks

  12. #12
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Use Macro to open a file with excel (underfined type of file)

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  13. #13
    Registered User
    Join Date
    07-29-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    34

    Re: Use Macro to open a file with excel (underfined type of file)

    Hello

    I have attached 2 excel files.
    ABC_correct format : opened by double click the undefined file, and select "excel" to open the file.

    ABC_wrong format: i use the macro to open the file, you may realize that the columns "D:F" dates are in strange format.

    I cannot attach the source file (undefined file) as those data cannot be desensitived. Since it was "undefined" type, if i have edited it, i cannot save as undefined file.

    Hopefully these two files can demonstrate my question...

    Thank you very much!

+ 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. Macro needed to open "File Save As" and change save file type
    By blainerhett in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2014, 02:40 PM
  2. Save as macro that specifies file type, file location and takes file name from three cells
    By ExcelFailure in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-13-2013, 10:09 PM
  3. VBA/Macro Code needed to open 'Save As' dialogue box and set the file type to CSV
    By daleski in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2013, 12:58 PM
  4. macro to open, copy paste from existing excel file to a new blank file
    By 2blessed in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-15-2012, 06:05 PM
  5. Replies: 4
    Last Post: 02-18-2005, 10:06 PM

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