Open File, copy to WB1 - getting hung up

melodramatic

Board Regular
Joined
Apr 28, 2003
Messages
187
Office Version
  1. 365
Platform
  1. Windows
I'm been tasked with creating a macro that will take a new listing of data, and compare it to existing data in my main workbook. Unfortunately, I keep getting hung up with it, and I am totally lost.

What I'm wanting to do -

Go to the specific folder, and choose the file to update (works fine)
Open that file and copy a range of data (works fine)
Go back to my original file (the one running the macro) - THIS IS WHERE I HANG UP - Run-time error '13'
Copy the data to a worksheet in my original file
Close the newly opened file (without saving)

Here's my coding...

Code:
Sub CompareData()
'
'
'
Dim WBPath As String
Dim WB1 As Workbook
'Dim WB2 As Workbook

Set WB1 = ActiveWorkbook

ChDrive "P:\"
ChDir "\ADMINISTRATIVE\Extension Macro\Updates"
WB2 = Application.GetOpenFilename

    If OpenWB = "False" Then

        Exit Sub

    Else

        Workbooks.Open Filename:=WB2
            Range("A1:Z5000").Select
            Selection.Copy
            
        Workbooks(WB1).Activate ' THIS IS WHERE MY MACRO HANGS UP
            Sheets("Compare").Select
            Range("B5").Select
            ActiveSheet.Paste
            
        Workbooks(WB2).Activate
            Workbooks(WB2).Close SaveChanges:=False
        
        WB1.Activate
    End If

End Sub

I know it's something so simple that I should be slapped in the face, but I just can't figure it out. Help???
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I did find one error looking this over again - the If statement should be "If WB2 = "False" Then... I made that change, and I'm still getting hung up at the same spot (trying to activate WB1).
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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