从单元格

时间:2017-09-12 09:33:34

标签: excel vba excel-vba

我有一张纸,S和P.在Sheet S中,我有很少的ID从D2E开始,很少Id从第N列的4开始。

我正在将包含4的ID与L列中的表P进行比较。如果它们匹配,那么我在N列的A列中写入表P的ID。

我在快照中的情况很少,我无法提取。任何人都可以帮助我,我该怎么做

在表单S中,我有一个像41035036_drw_000_draf的Id,在表单PI中能够找到相应的D2E编号并打印出来,但是我希望这个数字打印在P列的表单S中。

我相信我需要修改rng.find函数。我寻找前8个字符。任何人都可以帮忙,我该怎么做

以下是我的代码

Sub drwmatch()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim cell As Range, cell2 As Range, lstcl As Variant, lstcl2 As Variant, rgFnd As Variant
    Dim n As Double, ID As String
    Dim a As String
    Dim b As Variant

    Set sh1 = ThisWorkbook.Sheets("S")
    Set sh2 = ThisWorkbook.Sheets("P")

    ' ID starts with number 4
    ID = "4"
    lstcl = sh1.Range("N10000").End(xlUp).Row
    lstcl2 = sh2.Range("L10000").End(xlUp).Row

    'comparing columns N and L in both sheets
    For Each cell In sh2.Range("L5:L" & lstcl2)
        For n = 5 To lstcl
            a = Left(sh1.Range("N" & n), 8)
            If cell = a Then
                'the cell in column M next to the matching cell is equal to the 4xxxxxxx number
                cell.Offset(0, 1) = a
                'the next cell in column N is equal to the A2C number in column A
                cell.Offset(0, 2) = cell.Offset(0, -11)
            End If
        Next
    Next

    'test that each cell in the first sheet corresponds to the located results in the second sheet _
    'and pastes back the A2C number, using the Range.Find function
    For Each cell2 In sh1.Range("N5:N" & n)
        If Left(cell2, 1) = ID Then
            Set rgFnd = sh2.Range("M5:M" & lstcl2).Find(cell2.Value)
            If Not rgFnd Is Nothing Then
                cell2.Offset(0, 1) = sh2.Range(rgFnd.Address).Offset(0, 1)
            End If
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

要搜索前8个字符,您可以像这样编写查找指令

Set rgFnd = sh2.Range("M5:M" & lstcl2).Find(Left(cell2.Value, 8), lookat:=xlPart)