+ Reply to Thread
Results 1 to 2 of 2

Open, then auto select file and automatically run program

Hybrid View

  1. #1
    Registered User
    Join Date
    06-27-2018
    Location
    America
    MS-Off Ver
    2007
    Posts
    30

    Open, then auto select file and automatically run program

    I am looking for a way to open a folder, and have different workbooks open different files, using a partial title and then run specific programs.

    For workbook1 I have this code:

    Sub Foo()
    Dim vFile As Variant
    Dim wbCopyTo As Workbook
    Dim wsCopyTo As Worksheet
    Dim wbCopyFrom As Workbook
    Dim wsCopyFrom As Worksheet
    Dim Fnd As Range
    Dim Ary As Variant
    Dim i As Long
    
    Set wbCopyTo = ActiveWorkbook
    Set wsCopyTo = ActiveSheet
    Ary = Array("Total", 24, "t-4", 4, "t-3", 5, "t-2", 6, "t-1", 7, "Behr SOP = t0", 8, "t1", 9, "t2", 10, "t3", 11, "t4", 12, "t5", 13, "t6", 14, "t7", 15, "t8", 16, "t9", 17, "t10", 18, "t11", 19, "t12", 20, "t13", 21, "t14", 22, "t15", 23)
        '-------------------------------------------------------------
        'Open file with data to be copied
        
        vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
        "*.xl*", 1, "Select Excel File", "Open", False)
        
        'If Cancel then Exit
        If TypeName(vFile) = "Boolean" Then
            Exit Sub
        Else
        Set wbCopyFrom = Workbooks.Open(vFile)
        Set wsCopyFrom = wbCopyFrom.Worksheets(1)
        End If
        
        '--------------------------------------------------------------
     'Copy Range
       For i = 0 To UBound(Ary) Step 2
          Set Fnd = wsCopyFrom.Range("5:5").Find(Ary(i), , , xlWhole, , , False, , False)
          If Not Fnd Is Nothing Then
             wsCopyFrom.Range(Fnd.Offset(1), wsCopyFrom.Cells(wsCopyFrom.Rows.Count, Fnd.Column).End(xlUp)).Copy
             wsCopyTo.Cells(7, Ary(i + 1)).PasteSpecial xlPasteValues
          End If
       Next i
       Application.CutCopyMode = False
       wbCopyFrom.Close SaveChanges:=False
    End Sub
    For workobook2 I have this code:


    Sub Foo()
    Dim vFile As Variant
    Dim wbCopyTo As Workbook
    Dim wsCopyTo As Worksheet
    Dim wbCopyFrom As Workbook
    Dim wsCopyFrom As Worksheet
    
    Set wbCopyTo = ActiveWorkbook
    Set wsCopyTo = ActiveSheet
    
        '-------------------------------------------------------------
        'Open file with data to be copied
        
        vFile = Application.GetOpenFilename("Excel Files (*.xl*)," & _
        "*.xl*", 1, "Select Excel File", "Open", False)
        
        'If Cancel then Exit
        If TypeName(vFile) = "Boolean" Then
            Exit Sub
        Else
        Set wbCopyFrom = Workbooks.Open(vFile)
        Set wsCopyFrom = wbCopyFrom.Worksheets(1)
        End If
        
        '--------------------------------------------------------------
        'Copy Range
        wsCopyFrom.Range("B1:G43").Copy
        wsCopyTo.Range("B4").PasteSpecial Paste:=xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        
        wsCopyFrom.Range("H1:H43").Copy
        wsCopyTo.Range("Q4").PasteSpecial Paste:=xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                
        wsCopyFrom.Range("I1:N36").Copy
        wsCopyTo.Range("B54").PasteSpecial Paste:=xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        
        wsCopyFrom.Range("O1:O36").Copy
        wsCopyTo.Range("Q54").PasteSpecial Paste:=xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                
        'Close file that was opened
        wbCopyFrom.Close SaveChanges:=False
    End Sub
    Workbook 1 should automatically select "Fileabc 1" and workbook 2 should automatically select "Filedef 2" and run the codes automatically.

    Is this even possible?

    I have a thread available as well on mrexcel.com
    Last edited by kbenjamin827; 07-16-2018 at 07:52 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,774

    Re: Open, then auto select file and automatically run program

    Rule 08: Cross-posting Without Telling Us

    Your post does not comply with Rule 8 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question. If you have fewer than 10 posts here, you will not be able to post a link, but you must still tell us where else you have asked the question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    If you have less than 10 posts, do not try to copy and paste the link. Instead, type the link out in your thread.

    No further help to be offered, please, until the OP has complied with this request.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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. [SOLVED] Excel Hyperlink to open pdf- Error - "No program is registered to open this file
    By laxminarayana in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-27-2016, 10:03 AM
  2. URGENT - make drop down box select open file automatically
    By Muzza86 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-12-2014, 04:10 AM
  3. Formula to open external file with especific program (like open with)
    By pemartins in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-14-2014, 06:38 PM
  4. Replies: 3
    Last Post: 03-17-2009, 01:59 PM
  5. Replies: 1
    Last Post: 02-08-2006, 08:50 AM
  6. [SOLVED] Program to Automatically Save Word File
    By maperalia in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-26-2005, 07:05 PM
  7. [SOLVED] open up an excel program with an auto open macro
    By john.9.williams@bt.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2005, 12:05 PM

Tags for this Thread

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