VBA - VLOOKUP for a potential pasted range

JayBurn

New Member
Joined
Jun 13, 2013
Messages
44
Morning All

A while ago I was given a bit of help to get this far, but now need a bit of tweaking.

I'm working on a product quoting tool, and it's doing a very basic vlookup using VBA. It works fine when a single product code is entered into a cell between B22:B121, however my sales guys will at times paste in multiple codes, and the VBA will only apply the lookup to the first row.

Here is what I'm using currently;
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim KeyCells As Range
    Dim Rw As Long
 
    Set KeyCells = Range("B22:B121")
   
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
        Rw = Target.Row
       On Error Resume Next
        Range("C" & Rw).ClearContents
        Range("E" & Rw).ClearContents
        Range("F" & Rw).ClearContents
        Range("G" & Rw).ClearContents
        
           Range("C" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Target.Value, Sheets("VS Data").Range("B1:C5000"), 2, False), "")
           Range("E" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Target.Value, Sheets("VS Data").Range("B1:D5000"), 3, False), "")
           Range("F" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Target.Value, Sheets("VS Data").Range("B1:E5000"), 4, False), "")
           Range("G" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Target.Value, Sheets("VS Data").Range("B1:F5000"), 5, False), "")
            
On Error GoTo MyErrorHandler
 
MyErrorHandler:
If Err.Number = 1004 Then
result = ""
End If
End If

End Sub

I've played around with various loops, but can't figure out how best to get it repeating for each of the pasted cells rather than just the first...

Any advice?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
No worries! Solved myself with a simple loop...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim KeyCells As Range
    Dim Rw As Long
    Dim i As Integer
    
    Set KeyCells = Range("B22:B121")
    
   
    Rw = 22
    Do
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
       Is Nothing Then
         
    
       On Error Resume Next
        Range("C" & Rw).ClearContents
        Range("E" & Rw).ClearContents
        Range("F" & Rw).ClearContents
        Range("G" & Rw).ClearContents
        Range("C" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Range("B" & Rw), Sheets("VS Data").Range("B1:C5000"), 2, False), "")
        Range("E" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Range("B" & Rw), Sheets("VS Data").Range("B1:D5000"), 3, False), "")
        Range("F" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Range("B" & Rw), Sheets("VS Data").Range("B1:E5000"), 4, False), "")
        Range("G" & Rw) = Application.WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Range("B" & Rw), Sheets("VS Data").Range("B1:F5000"), 5, False), "")

On Error GoTo MyErrorHandler
 
MyErrorHandler:
If Err.Number = 1004 Then
result = ""
End If
End If

Rw = Rw + 1
Loop While Rw < 122

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

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