Excel VBA下拉列表和vlookup问题

时间:2015-03-02 09:24:57

标签: excel vba excel-vba

我正在尝试创建一个VBA代码,该代码将创建一个下拉列表或在单元格中具有Vlookup功能。 我是VBA的新手所以请怜悯。 :) 问题是,使用下面的代码它总是崩溃Excel。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Lookup_Range As Range

Set shList = ThisWorkbook.Sheets("ListaEchipamente")
Set Lookup_Range = shList.Range("G10", "M345")

If Cells(Target.Row, 13).Value = " " Then

With Range("J2:J100").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=ListaEchipamente!K10:K345"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With

ElseIf Not Cells(Target.Row, 13).Value = " " Then

Cells(Target.Row, 10).Value = "=VLookup(Range(target.row, 13), Lookup_Range, 2, False)"

End If

End Sub

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您的代码更改Cells(Target.Row, 10).Value会触发另一个Change事件,您将获得无限循环。为了避免它首先禁用事件:

Application.EnableEvents = False

'code to modify cells here 

Application.EnableEvents = True