添加数据验证时发生VBA运行时错误

时间:2017-12-22 05:53:58

标签: excel vba validation

我正在尝试使用宏更新多个工作表上的数据验证列表。您可以在下面找到代码。 不幸的是,我正在定义一个&34;应用程序定义或对象定义"错误在" .Add类型:= xlVali ...."行。

你能帮助我吗,为什么我会收到这个错误。

Dim sh As Worksheet

Application.ScreenUpdating = False

For Each sh In ActiveWorkbook.Sheets
sh.Visible = xlSheetVisible
Next

Dim Xws As Worksheet
For Each Xws In Application.ActiveWorkbook.Worksheets
    If Xws.Name = "Acilis" Or Xws.Name = "Admin_Sayfasi" Or Xws.Name = "Sheet4" Or Xws.Name = "Sheet2" Or Xws.Name = "Giris" Or Xws.Name = "Kunye" Or Xws.Name = "Kilavuz" Or Xws.Name = "Yonetim" Or Xws.Name = "SheetX" Or Xws.Name = "Butonlar" Or Xws.Name = "Sheet3" Or Xws.Name = "Ders_TEMP" Or Xws.Name = "Egitim Bilgileri" Or Xws.Name = "Mudurluk_TEMP" Or Xws.Name = "Egitmen_TEMP" Or Xws.Name = "Egitmen_List" Or Xws.Name = "Baskanlik_TEMP" Or Xws.Name = "Ekip_Uye_List" Or Xws.Name = "Deneme2" Or Xws.Name = "Uretim_TEMP" Or Xws.Name = "Personel_TEMP" Or Xws.Name = "Bilgiler" Or Xws.Name = "Sheet1" Or Xws.Name = "Puanlama" Or Xws.Name = "Deneme" Or Xws.Name = "Admin" Then

GoTo Next1
Else

XwsQ = Xws.Name

Worksheets(XwsQ).Select
Worksheets(XwsQ).Range("P22:P1111").Select
With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=Ekip_Uye_List!$A$1:$A$" & LastRowB & ""
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With


Next1:

Next

Dim Xws2 As Worksheet
For Each Xws2 In Application.ActiveWorkbook.Worksheets
    If Xws2.Name <> "Acilis" Then
        Xws2.Visible = xlSheetHidden
    End If
Next

Application.ScreenUpdating = True

代码很长,就像这样。但我无法清除这个错误。

很抱歉这样说,但这是我自己的错。我发现在遗憾地声明LastRowB时我犯了一个错误。错误是由于这个原因引起的。我在这里粘贴的第一个代码效果很好。问题发生在LastRowB的行上,给出了错误。

2 个答案:

答案 0 :(得分:0)

添加&amp;在这里

  

=&amp; “= Ekip_Uye_List!$ A $ 1:$ A $”&amp;

Dim sh As Worksheet

Application.ScreenUpdating = False

For Each sh In ActiveWorkbook.Sheets
sh.Visible = xlSheetVisible
Next

Dim Xws As Worksheet
For Each Xws In Application.ActiveWorkbook.Worksheets
    If Xws.Name = "Acilis" Or Xws.Name = "Admin_Sayfasi" Or Xws.Name = "Sheet4" Or Xws.Name = "Sheet2" Or Xws.Name = "Giris" Or Xws.Name = "Kunye" Or Xws.Name = "Kilavuz" Or Xws.Name = "Yonetim" Or Xws.Name = "SheetX" Or Xws.Name = "Butonlar" Or Xws.Name = "Sheet3" Or Xws.Name = "Ders_TEMP" Or Xws.Name = "Egitim Bilgileri" Or Xws.Name = "Mudurluk_TEMP" Or Xws.Name = "Egitmen_TEMP" Or Xws.Name = "Egitmen_List" Or Xws.Name = "Baskanlik_TEMP" Or Xws.Name = "Ekip_Uye_List" Or Xws.Name = "Deneme2" Or Xws.Name = "Uretim_TEMP" Or Xws.Name = "Personel_TEMP" Or Xws.Name = "Bilgiler" Or Xws.Name = "Sheet1" Or Xws.Name = "Puanlama" Or Xws.Name = "Deneme" Or Xws.Name = "Admin" Then

GoTo Next1
Else

XwsQ = Xws.Name

Worksheets(XwsQ).Select
Worksheets(XwsQ).Range("P22:P1111").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:= & "=Ekip_Uye_List!$A$1:$A$" & LastRowB & ""
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With

答案 1 :(得分:0)

你可以请格式化你的代码示例,以便可以正确地编译(Alt + D)

  1. “如果”未关闭
  2. “缺少下一个XWS”
  3. 未宣布XwsQ
  4. 如果你以“工作”的方式向你提出问题,那么帮助你会更容易。