简体   繁体   中英

How to find a cell value on another worksheet and place a value on another column for the row where the first value was found

(I admit that I'm a newbie with vba, so I'm sorry if it's a silly question - I'm also sorry for any english mistakes)

Some background:

I'm responsible for the activities schedule of the support agents on my company. Their leaders can send requests to remove them from the schedule, for example. And there can be more than one request per agent for the same day. (the schedule is daily)

So, I have two spreadsheets, "Requests" and "Schedule"

On the request worksheet: I need to check on the column for each type of requests (Vacations, Day Off..) ("E" Column) If I find this request, I need the agent email from another row (P Column), search this email on the "Schedule" worksheet ("A" Column) and place an "1" on ther "AI" column (I have to report the amout of each request)

I've been having a problem mostly to find the agent on the "schedule" worksheet and place the "1", and I've tried other codes, but this is the last one I've been working on (a work colleague sugested using the "Do While" and "Find")

(for now the excel just crash)

Some of what I've tried:

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

And

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

Have a look at the Find example below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM