Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

doesn't open file despite of it's really existed

0

Hi  Guys  

I  no  know  why  shows  this error 

"Sorry We Could Not Find FILE1.xlsx. Is it possible it was moved, renamed or deleted"?

  in  this  line 

 Set Workbook1 = Workbooks.Open(File1)

despite  of  I'm  really  sure  this  file  is  existed  and  matched  with  name  and  I  check  it  by  another  macro  and  open  it  without  any problem  . I  no  know  why  show  this  error!! 

Sub Compare_Two_Files()

    Dim Workbook1 As Workbook
    Dim Workbook2 As Workbook
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim i As Integer, j As Integer

    File1 = "File1.xlsx"
    File2 = "File2.xlsx"
    File1_Sheet = "Sheet1"
    File2_Sheet = "Sheet1"

    Set Workbook1 = Workbooks.Open(File1)
    Set Workbook2 = Workbooks.Open(File2)

    Set Rng1 = Workbook1.Worksheets(File1_Sheet).UsedRange
    Set Rng2 = Workbook2.Worksheets(File2_Sheet).UsedRange
    Count = 1
    For i = 1 To Rng1.Rows.Count
        For j = 1 To Rng1.Columns.Count
            If Rng1.Cells(i, j) <> Rng2.Cells(i, j) Then
                For k = 1 To Rng1.Rows.Count
                    Sheet1.Cells(Count, k) = Rng2.Cells(i, k)
                Next k
                Count = Count + 1
                Exit For
            End If
        Next j
    Next i
    Workbook1.Close
    Workbook2.Close
    Set Rng1 = Nothing
    Set Rng2 = Nothing
    Set Workbook1 = Nothing
    Set Workbook2 = Nothing
End Sub

anybody  can  guid  me  what's  my  mistake ,please?

Answer
Discuss

Answers

0
Selected Answer

Hello Kalil,

From what I can tell you need to change:

    File1 = "File1.xlsx"
    File2 = "File2.xlsx"

to include the full file path like this (edit to suit your situation):

MyFile = "C:\Users\USER\Documents\My Excel Stuff\Report 1.xlsm"

Hope this helps

Cheers   :-)

Discuss

Discussion

Hi willie,
I don't  understand  you , why  I  have  to use    this  way ?
Kalil (rep: 36) Dec 18, '22 at 2:47 pm
Hi Kalil,
Using the full file path with file name tells Excel specificly which file to open.

You also need to declare the variables File1, File2, File1_Sheet, and File2_sheet as strings:
Dim File1 As String, File2 As String, File1_Sheet As String, File2_Sheet As String

Declaring your variables may be enough to avoid the error.
WillieD24 (rep: 557) Dec 18, '22 at 4:32 pm
this works  as  I  want 
thanks willie 
Kalil (rep: 36) Dec 19, '22 at 4:47 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login