I'm having difficulties expanding the following program:
PHP Code:
Sub Demo2() DDim Rs As Range, Rg, Rz As Range, C&, A$, Rf As Range, l&, R&
Dim LoB As String
Dim OrgWsName As String Dim DstWsName As String
OrgWsName = firm
DstWsName = "BT_" & firm
Set Rs = Worksheets(OrgWsName).UsedRange Set Rg = Rs.Find("Summe", , xlValues, xlWhole, xlByRows) If Rg Is Nothing Then Beep Else C = Rg.CurrentRegion.Columns.Count - 1 l = 2 With Worksheets(DstWsName) '.Range("A2:A300,C2:O300").Clear Application.ScreenUpdating = False A = Rg.Address Do Set Rf = Rg.End(xlUp).End(xlUp) l = l + 1 LoB = Application.WorksheetFunction.VLookup(Rf.Value, Worksheets(OrgWsName).Range("A15:C15"), 3, False).Value If Rg.Row > R Then l = l + 1 R = Rg.Row [COLOR="#FF0000"] .Cells(l, 1).Value = Rf.Value[/COLOR] .Cells(l, 2).Value = LoB End If .Cells(l, 3).Value = Rf(0).Value Rg(1, 2).Resize(, C).Copy .Cells(l, 4) Set Rg = Rs.FindNext(Rg) Loop Until Rg.Address = A Set Rg = Nothing: Set Rf = Nothing .UsedRange.Font.Bold = False End With Application.ScreenUpdating = True End If
Set Rs = Nothing End Sub
In column 1, we have a code line which writes data in the sheet DstWsName (see red line in the code):
I want to expand the program, so that it creates in the cell next to this cell (same row, column 2) a vlookup which checks if the value in .Cells(l, 1) in the sheet DstWsName is in the Range A15:C15 in the sheet OrgWsName and returns the corresponding value from the column C.
thank you for your answer. I have uploaded a sample and took your advice and defined the VLookup directly on the cell.
The problem is, that Vlookup doesn't find any values. For instance, I'm looking for the Rf.Value = 1 in the range A15:C15 of the sheet "AZ". I'd expect it to return the value in C3 "AZ\O2_Life",
because we have in A3 the value "1". The Vlookup produces a "NV"-error. I don't understand why.
I've tried a few things and noticed that the code works, if the range where the vlookup searches, contains no empty rows. In my example, A2:C2 is empty and this leads to the error. If I run the VLookup on A3:C15, the code works. Is this normal?
Bookmarks