Share via

Drop Down Code Help

Anonymous
2023-10-27T15:01:58+00:00

I have the following code to enter a value based on a drop down.  It works well if the table is on the same sheet; however,  I need for this to work with the data in a different sheet named "Material"

[code]

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice
selectedNa = Target.Value
If Target.Column = 4 Then
selectedNum = Application.VLookup(selectedNa, ActiveSheet.Range("Material_List"), 2, False)
If Not IsError(selectedNum) Then
Target.Value = selectedNum
End If
End If
End Sub

[code]

Microsoft 365 and Office | Excel | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2023-10-27T15:16:29+00:00

Change ActiveSheet to Worksheets("Material")

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-10-27T21:15:30+00:00

    Oh the first one is suppose to be column 4 the other should be 6. I am getting an error in the end sub

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2023-10-27T19:08:10+00:00

    You have 2 sections of code that both apply to the same column 4 (column D) buy that look up a value in a different table.

    How should that work?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-10-27T18:16:34+00:00

    Another Question . . . If I want multiple change events on same worksheet? I tried this, but does not work:

    Private Sub Worksheet_Change(ByVal Target As Range)

    'Updateby Extendoffice

    selectedNa = Target.Value 
    
    If Target.Column = 4 Then 
    
        selectedNum = Application.VLookup(selectedNa, Worksheets("Materials").Range("Material\_List"), 2, False) 
    
        If Not IsError(selectedNum) Then 
    
            Target.Value = selectedNum 
    
        End If 
    
            If Target.Column = 4 Then 
    
        selectedNum = Application.VLookup(selectedNa, Worksheets("Time").Range("Time\_List"), 2, False) 
    
        If Not IsError(selectedNum) Then 
    
            Target.Value = selectedNum 
    
        End If 
    
    End If 
    
    Exit Sub 
    

    End Sub

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-10-27T15:38:57+00:00

    I knew it was a simple fix. Thank you for the quick reply!

    Was this answer helpful?

    0 comments No comments