Vba比较然后复制两个不同的表格

时间:2013-10-15 17:26:43

标签: excel vba

我意识到这里有一些不同的类似想法。但是我需要这个简单比较功能的帮助。

我的目标是比较两个不同的单元格,如果它们相同,则将其替换为完整的非缩写名称。

感谢您的时间!!!

Sheet1           Sheet2
Column H    Column A   Column B
Dept        Dept       Department

这就是我所拥有的(是的),但是单元格H没有更新为非缩写:

 Sub updateDeptNames()

'Format user ID from the email column
Dim ws As Worksheet, ws2 As Worksheet
Dim LastRow As Long, i As Long
Dim tmpArray() As String, tempDept As String
Set ws = ActiveWorkbook.Sheets("Student_Travel_DB") '--> This is the relevant sheet
Set ws2 = ActiveWorkbook.Sheets("gokoutd") '--> This is the relevant sheet

    LastRow = 1000 ''Bug finding the last row, had to hard code it

    For i = 2 To LastRow 'Iterate through all the rows in the sheet
       For j = 2 To 112
        tempDept = ws2.Range("A" & j).Value
        If ws.Range("H" & i).Value = tempDept Then
            ws.Range("H" & i) = ws2.Range("B" & j).Value
        End If
        Next j
    Next i
 End Sub

1 个答案:

答案 0 :(得分:1)

您可以在工作表或VBA上更轻松地使用VLOOKUP

Sub GetFullName()
    Dim cl As Range, data As Range, lookUpRng As Range

    Set data = Worksheets("Student_Travel_DB").Range("A1:A10")
    Set lookUpRng = Worksheets("gokoutd").Range("A1:B10")

    On Error Resume Next
    For Each cl In data
        cl = WorksheetFunction.VLookup(cl, lookUpRng, 2, False)
    Next cl
End Sub

您需要更改范围参考。