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-27T22:02:42+00:00

    Your Amazing. Thank you!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-10-27T21:32:32+00:00

    This is what I have, but not working

    Private Sub Worksheet_Change(ByVal Target As Range)

    'Updateby Extendoffice

    selectedNa = Target.Value 
    
    If Target.Column = 5 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 = 7 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
  3. HansV 462.6K Reputation points
    2023-10-27T21:28:53+00:00

    Try this version:

    Private Sub Worksheet_Change(ByVal Target As Range)
    'Updateby Extendoffice
        Dim selectedNa As Variant
        Dim selectedNum As Variant
        If Target.CountLarge > 1 Then Exit Sub
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        On Error GoTo ExitHere
        Select Case Target.Column
            Case 4
                selectedNa = Target.Value
                selectedNum = Application.VLookup(selectedNa, Worksheets("Materials").Range("Material_List"), 2, False)
                If Not IsError(selectedNum) Then
                    Target.Value = selectedNum
                End If
            Case 6
                selectedNa = Target.Value
                selectedNum = Application.VLookup(selectedNa, Worksheets("Time").Range("Time_List"), 2, False)
                If Not IsError(selectedNum) Then
                    Target.Value = selectedNum
                End If
        End Select
    ExitHere:
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End Sub
    

    Was this answer helpful?

    0 comments No comments