VBA找到行号,然后使用循环查找值为的新单元格

时间:2016-05-19 11:03:22

标签: excel vba excel-vba

我有一个工作簿“Vendor Management Simplified8”,其中包含“Overdue PO”工作表中的数据和另一个工作表Org Chart中的组织结构图“组织结构图”。在数据表中有一份管理承包商的人员清单。我想遍历每个名​​称(此列中没有空格)并在组织结构图中找到它们的名称并返回常规管理器名称和部门,并将这些值返回到列N和O中。名称可能不是在组织结构图中找到。

到目前为止,我已经有了......

Dim Cell As String
Dim loopy As Integer
Dim findrow As range
Dim finddata As Integer

loopy = 2
Do Until Worksheets("Overdue PO").range("E" & loopy).VALUE = "" 'loops through the name of person managing contractor
    Cell = Trim(Worksheets("Overdue PO").range("E" & loopy).VALUE) 'value which needs to be looked up

'finds the row number where value is found
    Set findrow = Worksheets("Organisation Chart").range("G:L").find(What:=Cell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False)

    If findrow Is Nothing Then
    Else: finddata = findrow.Row
    End If

        Do Until Worksheets("Organisation Chart").range("D" & finddata).VALUE <> "" 'if cell is not empty
            If Worksheets("Organisation Chart").range("D" & finddata).VALUE = "" Then
                finddata = finddata - 1 'go up the cells
            End If
        Loop

    Worksheets("Overdue PO").range("0" & loopy).VALUE = Worksheets("Organisation Chart").range("D" & finddata).VALUE 'department
    Worksheets("Overdue PO").range("N" & loopy).VALUE = Worksheets("Organisation Chart").range("D" & finddata - 1).VALUE 'general manager
    loopy = loopy + 1

Loop

到目前为止 - 它找到了值并返回'findrow'中的行号,它循环组织结构图中的行,直到该值不为空,然后出现错误(运行时错误1004 - 应用程序定义当尝试将部门输入到过期采购订单工作表的O列时,在Worksheets("Overdue PO").range("0" & loopy).VALUE = Worksheets("Organisation Chart").range("D" & finddata).VALUE行上出现或出现对象定义的错误。

我错误地得到了这个错误?是否有更简单的方法来循环查找总管和部门?

提前致谢:)

3 个答案:

答案 0 :(得分:0)

您使用了0(零)而不是字母O。

答案 1 :(得分:0)

以下是使用With声明修改的代码:

Dim Cell As String
Dim loopy As Integer
Dim findrow As range
Dim finddata As Integer

loopy = 2
Do Until Worksheets("Overdue PO").range("E" & loopy).VALUE = "" 'loops through the name of person managing contractor
    Cell = Trim(Worksheets("Overdue PO").range("E" & loopy).VALUE) 'value which needs to be looked up

    'finds the row number where value is found
    With Worksheets("Organization Chart")
        Set findrow = .range("G:L").find(What:=Cell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False)

        If findrow Is Nothing Then
        Else: finddata = findrow.Row
        End If

        Do Until .range("D" & finddata).VALUE <> "" 'if cell is not empty
            If .range("D" & finddata).VALUE = "" Then
                finddata = finddata - 1 'go up the cells
            End If
        Loop

        Worksheets("Overdue PO").range("O" & loopy).VALUE = .range("D" & finddata).VALUE 'department
        Worksheets("Overdue PO").range("N" & loopy).VALUE = .range("D" & finddata - 1).VALUE 'general manager
        loopy = loopy + 1
    End With
Loop

答案 2 :(得分:0)

我破解了!我把一个转到SkipRecord跳到最后循环到loopy = loopy + 1

Dim Cell As String
Dim loopy As Integer
Dim findrow As range
Dim finddata As Integer

loopy = 2

Do Until Worksheets("Overdue PO").range("E" & loopy).VALUE = "" 'loops through the name of person managing contractor
    Cell = Trim(Worksheets("Overdue PO").range("E" & loopy).VALUE) 'value which needs to be looked up

    'finds the row number where value is found
    With Worksheets("Organisation Chart")
        Set findrow = .range("G:L").find(What:=Cell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False)

        If findrow Is Nothing Then
        GoTo skiprecord
        Else: finddata = findrow.Row
        End If

        Do Until .range("D" & finddata).VALUE <> "" 'if cell is not empty
            If .range("D" & finddata).VALUE = "" Then
                finddata = finddata - 1 'go up the cells
            End If
        Loop

        Worksheets("Overdue PO").range("O" & loopy).VALUE = .range("D" & finddata).VALUE 'department
        Worksheets("Overdue PO").range("N" & loopy).VALUE = .range("D" & finddata - 1).VALUE 'general manager
skiprecord:
        loopy = loopy + 1
    End With
Loop

感谢您的帮助:)