如何在另一个工作表上查找单元格值,以及如何在找到第一个值的行的另一列上放置一个值

时间:2019-04-24 13:52:35

标签: excel vba

(我承认我是vba的新手,因此很抱歉,这是一个愚蠢的问题-对于任何英语错误,我也感到抱歉)

一些背景:

我负责公司支持人员的活动安排。例如,其领导者可以发送请求以将其从日程表中删除。每个代理商在同一天可以有多个请求。 (时间表是每天的)

所以,我有两个电子表格,“请求”和“时间表”

在请求工作表上: 我需要检查每种请求类型的列(假期,休息日..)(“ E”列) 如果找到此请求,则需要另一行(P列)的代理电子邮件,在“计划”工作表(“ A”列)上搜索此电子邮件,然后在“ AI”列上放置“ 1”(我必须报告每个请求的金额

我遇到的问题主要是在“时间表”工作表上找到代理并放置“ 1”,并且我尝试了其他代码,但这是我一直在努力的最后一个代码(a工作同事被“ Do While”和“ Find”轻描淡写

(目前,Excel只是崩溃了)

我尝试过的一些事情

If wal.Cells(row, 5) = "Remove from the Schedule - Vacation" Then
    'Application.WorksheetFunction.Match(wal.Cells(row, 16).Value, wsc.Range("A:A"), 0).row
    wsc.Cells(Application.WorksheetFunction.Match(wal.Cells(row, 16).Value, wsc.Range("A:A"), 0).row, 35) = 1

ElseIf Cells(row, 5) = "Remove from the Schedule - Vacation" Then
    row = Application.WorksheetFunction.Match(wal.Cells(row, 16), sc.Range("A:A"), 0)
    wsc.Cells(row, 35) = 1

    Set wal = Worksheets("Alterações")
    Set wsc = Worksheets("Schedule")

    i = 5
    j = 41

    Do While wal.Range("E" & i) <> 0
        If wal.Range("E" & i) = "Remover do Schedule - Férias"  Then        
            Set agent = wal.Range("P" & i).Find(wsc.Range("A" & j).Value).row                
            Range("AI" & agent) = 1
        End If
    Loop

1 个答案:

答案 0 :(得分:0)

看看下面的Find示例

Sub demo()
    Dim wal As Worksheet: Set wal = Worksheets("Alterações")
    Dim wsc As Worksheet: Set wsc = Worksheets("Schedule")
    Dim rng As Range, UserRow As Range
    Dim UserEmail As String

    With wal
        Set rng = .Columns(5).Find("Remove from the Schedule - Vacation")
        If Not rng Is Nothing Then
            UserEmail = rng.Offset(0, 11).Value2
            Debug.Print rng.Address, UserEmail
            With wsc
                Set UserRow = .Columns(1).Find(what:=UserEmail)
                If Not UserRow Is Nothing Then
                    UserRow.Offset(0, Columns("AI").Column - UserRow.Column).Value2 = 1
                End If
            End With
        End If
    End With
End Sub