0

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

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

感谢您的时间!!!

IE

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
4

1 回答 1

1

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

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

您需要更改范围参考。

于 2013-10-15T17:51:22.583 回答