VBA enter value on change

LUCYtm

New Member
Joined
Jan 8, 2013
Messages
4
Hi,

I have problems adjusting my VBA code to apply when not only cell by cell value is entered but when I paste several cells in column D.
I think it works OK when I enter value for example in cell d4, but when I try to paste values from D4 trough D8, only values are pasted and other columns stay empty.

Here is my VBA code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, res As Variant
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("d:d")) Is Nothing Then Exit Sub
Set rng = Worksheets("Sheet3").Range("C:G")
res = Application.VLookup(Target, rng, 2, False)
Application.EnableEvents = False
If IsError(res) Then 'code not found
Target.Offset(0, 1).Resize(1, 3).Value = "No data found"
Beep
Else
Target.Offset(0, 1).Value = res
Target.Offset(0, 2).Value = Application.VLookup(Target, rng, 3, False)
Target.Offset(0, 3).Value = Application.VLookup(Target, rng, 4, False)
End If
Application.EnableEvents = True
End Sub

If someone can explain me if this formula can be fixed or this is not a proper way to 'Autofill' the columns. :confused:

Thank you

Bye
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, res As Variant, c As Range
If Intersect(Target, Range("d:d")) Is Nothing Then Exit Sub
Set rng = Worksheets("Sheet3").Range("C:G")
For Each c In Target
    res = Application.VLookup(c, rng, 2, False)
    Application.EnableEvents = False
    If IsError(res) Then 'code not found
        c.Offset(0, 1).Resize(1, 3).Value = "No data found"
        Beep
    Else
        c.Offset(0, 1).Value = res
        c.Offset(0, 2).Value = Application.VLookup(c, rng, 3, False)
        c.Offset(0, 3).Value = Application.VLookup(c, rng, 4, False)
    End If
    Application.EnableEvents = True
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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