您的位置:首页 > 其它

EXCEL 跨表比较数据

2016-04-13 10:54 183 查看
Public Sub Compare(fullname As String, sheet As String)

Dim conn, sql, rows, i, cellContents ,rowIndex, colIndex

  rowIndex = 2

  colIndex = getColumnIndex("Name")

'获取目标表数据
Set conn = CreateObject("adodb.connection")
conn.Open "provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0';data source=" & fullname
sql = "select * from [" & sheet & "$]"
rows = conn.Execute(sql).getrows
conn.Close

Do
'选择原表单元格
ActiveSheet.Cells(rowIndex, colIndex).Select
    '去除填充色
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With

cellContents = ActiveCell.Value

If cellContents <> "" Then
For i = 0 To UBound(rows, 2)
If cellContents = rows(2, i) Then
If rows(0, i) <> "apple" Then
'填充黄色
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With

End If
Exit For
End If
Next
End If

rowIndex = rowIndex + 1

Loop Until cellContents = ""

End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: