VBA在两个不同的工作簿中比较单元格值

时间:2017-12-05 18:13:33

标签: excel vba

我试图比较两个不同工作簿中的两个单元格值。我调试并发现Cells(rw, "A")这部分不起作用。我也尝试过分配参数并使用Option Explicit Sub findcell() Dim rw As Integer, re As Integer Dim extwbk As Workbook, twb As Workbook Dim x As Worksheet, y As Worksheet Dim Rng As Range Set twb = ThisWorkbook Set extwbk = Workbooks.Open(".........xlsx") Set x = twb.Worksheets("ABC") Set y = extwbk.Worksheets("DEF") For rw = 2 To x.UsedRange.Columns("A").Rows.Count For re = 3 To y.UsedRange.Columns("N").Rows.Count If x.Range("A" & rw).Value = y.Range("N" & re).Value And x.Cells(rw, 1) <> Null Then ' If use below line, it is working 'If x.Cells(rw, 1) = 45032 And y.Cells(re, 14) = 45032 Then MsgBox x.Cells(rw, 1).Value MsgBox y.Cells(re, 14).Value Exit For End If Next re Next rw extwbk.Close savechanges:=False End Sub ,但仍无效。有人可以帮忙吗?

{{1}}

1 个答案:

答案 0 :(得分:0)

我同意大卫认为问题可能是

x.Cells(rw, 1) <> Null

而是尝试

Not IsEmpty(x.Cells(rw, 1))