Vba: searching value and reporting them in another sheet

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I'm coping with the following situation.

I have to report values from sheet2 to sheet1.

The condition are as follows:

1) not consider data that in sheet2 are in state open (column B);
2) if the state is closed (column B, sheet2), then report in sheet1 on the row corresponding to the code (match column A for both the sheets): the date on the first empty cell, the state "CLOSED" next to the right.

https://imgur.com/a/K5eiKth

So, in this example, sheet1 cell E2 --> 25/06/2018, sheet1 cell F2 --> CLOSED

How can I approach the task?

Thank you.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Code:
Sub TryThis()
    Dim src As Worksheet, dest As Worksheet
    Dim rng As Range, cel As Range, fndRng As Range
    
Set src = ThisWorkbook.Sheets("Sheet2")
Set dest = ThisWorkbook.Sheets("Sheet1")

'the range to work with
Set rng = Range(src.Range("A2"), src.Range("A" & Rows.Count).End(xlUp))
'deal with each cell in that range
For Each cel In rng
    'look for CLOSED in next column
    If cel.Offset(, 1).Value = "CLOSED" Then
        'find the match on sheet1
        With dest
            Set fndRng = .Range("A:A").Find(What:=cel.Value, _
                                            LookIn:=xlValues, _
                                            LookAt:=xlWhole, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlNext, _
                                            MatchCase:=False)
            'if match found it will not be nothing
            If Not fndRng Is Nothing Then
                'write date to next empty column
                .Cells(fndRng.Row, .Columns.Count).End(xlToLeft).Offset(, 1).Value = cel.Offset(, 2).Value
            End If
        End With
    End If
Next cel
End Sub
 
Upvote 0
Rich (BB code):
Sub TryThis()
    Dim src As Worksheet, dest As Worksheet
    Dim rng As Range, cel As Range, fndRng As Range
    
Set src = ThisWorkbook.Sheets("Sheet2")
Set dest = ThisWorkbook.Sheets("Sheet1")

'the range to work with
Set rng = Range(src.Range("A2"), src.Range("A" & Rows.Count).End(xlUp))
'deal with each cell in that range
For Each cel In rng
    'look for CLOSED in next column
    If cel.Offset(, 1).Value = "CLOSED" Then
        'find the match on sheet1
        With dest
            Set fndRng = .Range("A:A").Find(What:=cel.Value, _
                                            LookIn:=xlValues, _
                                            LookAt:=xlWhole, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlNext, _
                                            MatchCase:=False)
            'if match found it will not be nothing
            If Not fndRng Is Nothing Then
                'write date to next empty column
                .Cells(fndRng.Row, .Columns.Count).End(xlToLeft).Offset(, 1).Value = cel.Offset(, 2).Value
                .Cells(fndRng.Row, .Columns.Count).End(xlToLeft).Offset(, 1).Value = "CLOSED"
            End If
        End With
    End If
Next cel
End Sub

I have added an instruction to your excellent job.
 
Upvote 0
Rich (BB code):
                'write date to next empty column
                .Cells(fndRng.Row, .Columns.Count).End(xlToLeft).Offset(, 1).Value = cel.Offset(, 2).Value

Unfortunately, something is wrong.

It doesn't write in the first empty column, but it writes in the first empty column after the last not empty column.
 
Upvote 0
I have added an instruction to your excellent job.

Maybe

Code:
.Cells(fndRng.Row, [COLOR="#FF0000"]"A"[/COLOR]).End(xlTo[COLOR="#FF0000"]Right[/COLOR]).Offset(0, 1).Value = cel.Offset(0, 10).Value
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,543
Messages
6,120,123
Members
448,947
Latest member
test111

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top