Consulting

Results 1 to 7 of 7

Thread: Comparing cells from different files

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location

    Question Comparing cells from different files

    How can I compare cells from different excel files?

    I've tried with this code with no success:

    Sub test()
        Dim ULin As Long
        ULin = Range("A65536").End(xlUp).Row
        For i = 2 To ULin
            If Range("AE" & i).Value = Windows("CN.xls").Range("R" & i).Value Then
                Range("AF" & i) = "Ok"
            Else
                Range("AF" & i) = "Problem"
            End If
        Next
    End Sub
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  2. #2
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    I know I could do it this way:



    Sub test()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim ULin As Long
    ULin = Range("A65536").End(xlUp).Row
    For i = 2 To ULin
    Range("AF" & i).Select
    ActiveCell.FormulaR1C1 = _
    "=IF(RC[-1]=[CN.xls]Can_nov!R" & (i + 2) & "C18,""Ok"",""Errado"")"
    Next
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  3. #3
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Paleo, try this:

    Sub test2()
          Dim ULin As Long, i%
          ULin = Range("A65536").End(xlUp).Row
                For i = 2 To ULin
                      If Range("AE" & i) = Workbooks("CN.xls").Worksheets(1).Range("R" & i) Then
                            Range("AF" & i) = "Ok"
                      Else
                            Range("AF" & i) = "Problem"
                      End If
                Next
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi John,

    worked just fine. I made only one change. From "Worksheets(1)" to "Worksheets("CN3")", because I have more than one sheet there, but you couldnt know that.

    One more thing. The workbook CN.xls must be openned in order to this work.

    Thank you very much!
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Not a prob,
    BTW, you should also be able to use Sheets("CN3") instead of Worksheets("CN3")
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi John,

    I was trying that just now, .

    Thanks again.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Quote Originally Posted by johnske
    Not a prob,
    BTW, you should also be able to use Sheets("CN3") instead of Worksheets("CN3")
    John

    That is true, usually you can use both interchangably. Sheets are any type of sheet in a workbook, whereas Worksheets are only worksheet type sheets.

    To make that a bit clearer, chart sheets are not Worksheets, but they are Sheets. So if you had chart sheets and worksheets in a workbook you could use Worksheets(1) to get the first worksheet and ignore any chart sheets that may be before it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •