2013-02-19 47 views
1

我有這段代碼工作正常:「結束,如果沒有塊,如果錯誤」在我更新的代碼

其目的是爲了輸出一個新行,B列數據驗證列表

Sub RICH() 


    Dim ws As Worksheet 
    Dim fnd As Range 
    Dim fndstr As String 

ActiveSheet.Unprotect 

    fndstr = "Targeted Premium Ads" 
    Set ws = Worksheets("Inputsheet") 

     Set fnd = ws.Columns(2).Find(What:=fndstr, After:=ws.Range("B11"), _ 
     LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _ 
     SearchDirection:=xlNext, MatchCase:=False) 

     If Not fnd Is Nothing Then 


      Rows(fnd.Row - 1).Select 
      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
      Range("B" & fnd.Row - 2).Select 
      With Selection.Validation 
       .Delete 
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
       xlBetween, Formula1:="=USD" 
       .IgnoreBlank = True 
       .InCellDropdown = True 
       .InputTitle = "" 
       .ErrorTitle = "" 
       .InputMessage = "" 
       .ErrorMessage = "" 
       .ShowInput = True 
       .ShowError = True 

      End With 
     End If 
End Sub 

不過,我累了,然後添加一個數據驗證列新行的單元格,但我得到的「塊,如果錯誤」 :(

Sub RICH() 


    Dim ws As Worksheet 
    Dim fnd As Range 
    Dim fndstr As String 

ActiveSheet.Unprotect 

    fndstr = "Targeted Premium Ads" 
    Set ws = Worksheets("Inputsheet") 

     Set fnd = ws.Columns(2).Find(What:=fndstr, After:=ws.Range("B11"), _ 
     LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _ 
     SearchDirection:=xlNext, MatchCase:=False) 

     If Not fnd Is Nothing Then 



      Rows(fnd.Row - 1).Select 
      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
      Range("B" & fnd.Row - 2).Select 
      With Selection.Validation 
       .Delete 
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
       xlBetween, Formula1:="=USD" 
       .IgnoreBlank = True 
       .InCellDropdown = True 
       .InputTitle = "" 
       .ErrorTitle = "" 
       .InputMessage = "" 
       .ErrorMessage = "" 
       .ShowInput = True 
       .ShowError = True 


      Range("A" & fnd.Row - 2).Select 
      With Selection.Validation 
       .Delete 
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
       xlBetween, Formula1:="=F6:F7" 
       .IgnoreBlank = True 
       .InCellDropdown = True 
       .InputTitle = "" 
       .ErrorTitle = "" 
       .InputMessage = "" 
       .ErrorMessage = "" 
       .ShowInput = True 
       .ShowError = True 



      End With 
     End If 
End Sub 
+0

雙單元格範圍「Formula1:=」F6:F7「? (不太熟悉驗證,所以可能會關閉基地 – chuff 2013-02-19 18:43:52

+0

不,這是正確的,我把它從記錄宏..但是我現在已經添加了貨幣符號 – user1717622 2013-02-19 18:54:16

回答

3

要麼你需要結束你先用語句或無法啓動第二個具有聲明:

With Selection.Validation 
       .Delete 
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
       xlBetween, Formula1:="=USD" 
       .IgnoreBlank = True 
       .InCellDropdown = True 
       .InputTitle = "" 
       .ErrorTitle = "" 
       .InputMessage = "" 
       .ErrorMessage = "" 
       .ShowInput = True 
       .ShowError = True 
END WITH 

     Range("A" & fnd.Row - 2).Select 
     With Selection.Validation 
      .Delete 
      .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
      xlBetween, Formula1:="=F6:F7" 
      .IgnoreBlank = True 
      .InCellDropdown = True 
      .InputTitle = "" 
      .ErrorTitle = "" 
      .InputMessage = "" 
      .ErrorMessage = "" 
      .ShowInput = True 
      .ShowError = True 



     End With 
+0

這是現在的功能..很多謝謝。快速的問題,但是...如果我去選擇不與第二個語句開始我將如何控制驗證? – user1717622 2013-02-19 18:59:23

+0

你wouldn 't。除非塊被嵌套,否則在啓動另一個塊之前,總是需要結束一個'With'塊。 – 2013-02-19 19:03:58

1

更改行...。新增類型:= xlValidateList ,AlertStyle:= xlValidAlertStop,Opera TOR:= _ xlBetween,一級方程式:= 「USD」

子RICH()

Dim ws As Worksheet 
    Dim fnd As Range 
    Dim fndstr As String 

ActiveSheet.Unprotect 

    fndstr = "Targeted Premium Ads" 
    Set ws = Worksheets("Inputsheet") 

     Set fnd = ws.Columns(2).Find(What:=fndstr, After:=ws.Range("B11"), _ 
     LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _ 
     SearchDirection:=xlNext, MatchCase:=False) 

     If Not fnd Is Nothing Then 


      Rows(fnd.Row - 1).Select 
      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
      Range("B" & fnd.Row - 2).Select 
      With Selection.Validation 
       .Delete 
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
       xlBetween, Formula1:="USD" 
       .IgnoreBlank = True 
       .InCellDropdown = True 
       .InputTitle = "" 
       .ErrorTitle = "" 
       .InputMessage = "" 
       .ErrorMessage = "" 
       .ShowInput = True 
       .ShowError = True 

      End With 
     End If 
End Sub 
+0

爲什麼B列中的行會影響事物,當添加驗證在A列是什麼停止它的工作? – user1717622 2013-02-19 18:49:58

相關問題