Macro: Open .xlsx in folder

StudentofExcel

New Member
Joined
Dec 2, 2017
Messages
7
Hello all,

I have searched around the web but I didn't find answer to this "problem".

What I would like is Macro that would open .xlsx file that is located in the same folder as Excel with Macro (only these two files would be in this folder.), but without specifying path location of both files because it will be moved around by other people.(Excel with Macro name can be name specific but .xlsx would not be named same every time) Folder name can be fixed if that makes it easier.

Goal is to open .xlsx file and move tab "Sheet1" to Excel with Macro.

If someone could help that would be great.

Thanks
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this
- first found "other file" opened
- first sheet copied as first sheet in macro file
- "other file" closed
- macro file saved


Code:
Sub OpenOtherFileAndCopyFirstSheet()
Application.ScreenUpdating = False: Application.Calculation = xlCalculationManual
    Dim file As Variant, myFolder As String, wb2 As Workbook, wb1 As Workbook
    Set wb1 = ThisWorkbook
    myFolder = wb1.Path & "\"
    file = Dir(myFolder)
    While (file <> "")
        If file <> wb1.Name Then
            Set wb2 = Workbooks.Open(myFolder & file)
            wb2.Sheets(1).Copy before:=wb1.Sheets(1)
            wb2.Close False
            wb1.Save
            Exit Sub
      End If
        file = Dir
    Wend
Application.ScreenUpdating = True: Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
It would be safer to check that the folder contains exactly 2 files before the macro continues, otherwise
- VBA will fail if only one file in folder
- wrong file may be opened if folder contains more than 2 files

So here is a version that does just that:
Code:
Sub OpenOtherFileAndCopyFirstSheet2()
Application.ScreenUpdating = False: Application.Calculation = xlCalculationManual
    Dim file As Variant, myFolder As String, wb2 As Workbook, wb1 As Workbook, c As long
    Set wb1 = ThisWorkbook
    myFolder = wb1.Path & "\"
    file = Dir(myFolder)
    While (file <> "")
        c = c + 1
        file = Dir
    Wend
    If c = 2 Then
        file = Dir(myFolder)
        While (file <> "")
            If file <> wb1.Name Then
                Set wb2 = Workbooks.Open(myFolder & file)
                wb2.Sheets(1).Copy before:=wb1.Sheets(1)
                wb2.Close False
                wb1.Save
                [COLOR=#ff0000]GoTo TheEnd[/COLOR]
          End If
            file = Dir
        Wend
    Else
        MsgBox "Proces cancelled" & vbCr & "Folder contains " & c & " files", vbExclamation, "WARNING"
    End If
[COLOR=#ff0000]TheEnd:[/COLOR]
Application.ScreenUpdating = True: Application.Calculation = xlCalculationAutomatic
End Sub

Note the amendment in Red - needed also in other version - otherwise recalculation remains set to manual!!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top