2016-10-03 376 views
0

我只是一個小問題!數據驗證的間接函數vba

我做了一個宏與兩個驗證列表的Excel,第一個列表功能,但與INDIRECT函數我有cuestion。

第一清單中的B17單元格

我的第二個列表必須採取參考B17做間接的功能,但不工作,與我研究的功能,但不下拉列表參考B17列表中的代碼只是「複製」這個單元格中的文本。

這是我的代碼

Sub insertfamilyValidate() 

' Selecciona la celda basica de indirecto 

' insertfamilyValidate Macro 
' 
' Acceso directo: CTRL+f 
' 
    Rows("17:18").Select 
    Selection.Insert Shift:=xlDown 
    Range("A17:M17").Select 
    With Selection.Interior 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
     .Color = 39423 
     .TintAndShade = 0 
     .PatternTintAndShade = 0 
    End With 
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone 
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone 
    With Selection.Borders(xlEdgeLeft) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeTop) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeBottom) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeRight) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    Selection.Borders(xlInsideVertical).LineStyle = xlNone 
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone 
    Range("A17").Select 
    ActiveCell.FormulaR1C1 = "Código" 
    Range("B17:D17").Select 
    With Selection 
     .HorizontalAlignment = xlCenter 
     .VerticalAlignment = xlBottom 
     .WrapText = False 
     .Orientation = 0 
     .AddIndent = False 
     .IndentLevel = 0 
     .ShrinkToFit = False 
     .ReadingOrder = xlContext 
     .MergeCells = False 
    End With 
    Selection.Merge 
    Range("A17").Select 
    Selection.Font.Bold = True 
    With Selection 
     .HorizontalAlignment = xlCenter 
     .VerticalAlignment = xlBottom 
     .WrapText = False 
     .Orientation = 0 
     .AddIndent = False 
     .IndentLevel = 0 
     .ShrinkToFit = False 
     .ReadingOrder = xlContext 
     .MergeCells = False 
    End With 
    Range("B17:D17").Select 
    With Selection.Validation 
     .Delete 
     .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
     xlBetween, Formula1:="=Familias" 
     .IgnoreBlank = True 
     .InCellDropdown = True 
     .InputTitle = "" 
     .ErrorTitle = "" 
     .InputMessage = "" 
     .ErrorMessage = "" 
     .ShowInput = True 
     .ShowError = True 
    End With 
    Range("E17").Select 
    ActiveCell.FormulaR1C1 = "Pax Sentadas" 
    Range("F17").Select 
    ActiveCell.FormulaR1C1 = "Cant." 
    Range("G17").Select 
    ActiveCell.FormulaR1C1 = "Cost. Unit." 
    Range("H17").Select 
    ActiveCell.FormulaR1C1 = "Días" 
    Range("I17").Select 
    ActiveCell.FormulaR1C1 = "Total" 
    Range("J17").Select 
    ActiveCell.FormulaR1C1 = "%" 
    Range("K17").Select 
    ActiveCell.FormulaR1C1 = "Descuento" 
    Range("L17").Select 
    ActiveCell.FormulaR1C1 = "Sub total" 
    Range("M17").Select 
    ActiveCell.FormulaR1C1 = "Total" 
    Range("A17:M17").Select 
    Range("M17").Activate 
    Selection.Font.Bold = True 
    With Selection 
     .HorizontalAlignment = xlCenter 
     .VerticalAlignment = xlBottom 
     .WrapText = False 
     .Orientation = 0 
     .AddIndent = False 
     .IndentLevel = 0 
     .ShrinkToFit = False 
     .ReadingOrder = xlContext 
    End With 

    Dim MiCelda As Range 

Set MiCelda = Worksheets("cotizacion").Cells(17, 2) 

With Range("A18").Validation 
    ' clear previous validation to existing cell 
    .Delete 

    ' *** Added this debug part *** 
    Dim ValidStr   As String 
    ValidStr = "=INDIRECT(" & MiCelda.Address(True, True) & ")" 
    Debug.Print ValidStr 

    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
    xlBetween, Formula1:="=INDIRECT(" & MiCelda.Address(True, True) & ")" 
    .IgnoreBlank = True 
    .InCellDropdown = True 
    .InputTitle = "" 
    .ErrorTitle = "" 
    .InputMessage = "" 
    .ErrorMessage = "" 
    .ShowInput = True 
    .ShowError = True 
End With 
    Range("B18:D18").Select 
    Selection.Merge 
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,LISTAPRECIOS2016,2,FALSE)" 
    Range("E18").Select 
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,LISTAPRECIOS2016,3,FALSE)" 
    Range("F18").Select 
    ActiveCell.FormulaR1C1 = "1" 
    Range("G18").Select 
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,LISTAPRECIOS2016,9,FALSE)" 
    Range("H18").Select 
    ActiveCell.FormulaR1C1 = "1" 
    Range("I18").Select 
    Application.WindowState = xlMinimized 
    Application.WindowState = xlNormal 
    ActiveCell.FormulaR1C1 = "=RC[-3]*RC[-2]*RC[-1]" 
    Range("J18").Select 
    ActiveCell.FormulaR1C1 = "0" 
    Range("K18").Select 
    ActiveCell.FormulaR1C1 = "=RC[-2]*RC[-1]" 
    Range("L18").Select 
    ActiveCell.FormulaR1C1 = "=RC[-3]-RC[-1]" 
    Range("M18").Select 
    ActiveCell.FormulaR1C1 = "=SUM(RC[-1])" 
    Range("G18").Select 
    Selection.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" 
    Range("I18").Select 
    Selection.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" 
    Range("J18").Select 
    Selection.Style = "Percent" 
    Range("K18").Select 
    Selection.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" 
    Range("L18").Select 
    Selection.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" 
    Range("M18").Select 
    Selection.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" 
    Range("A18").Select 
End Sub 

我認爲問題是,結果間接爲「$ B $ 17」,因此,是一個字符串,如果我這樣做在Excel中驗證列表中的結果是= INDIRECT ($ B $ 17)沒有引號,這是funcional,我嘗試刪除引號,第一次是好的,但在此之後是一個錯誤1004.

我知道是一個最小錯誤或我的變量是錯誤的,但我不能確定它。 有什麼幫助嗎?

我加了.delete,錯誤是一樣的。

這是截圖

enter image description here

與調試的截圖在這裏...

enter image description here

回答

0

在安裝之前,您Validation Formula你需要添加清除前一個代碼行:

Range("A18").Validation.Delete 

而對於完整的代碼(測試):

Sub Validate() 

Dim MiCelda As Range 

Set MiCelda = Worksheets("cotizacion").Cells(17, 2) 

With Range("A18").Validation 
    ' clear previous validation to existing cell 
    .Delete 

    ' *** Added this debug part *** 
    Dim ValidStr   As String 
    ValidStr = "=INDIRECT(" & MiCelda.Address(True, True) & ")" 
    Debug.Print ValidStr 

    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ 
    xlBetween, Formula1:="=INDIRECT(" & MiCelda.Address(True, True) & ")" 
    .IgnoreBlank = True 
    .InCellDropdown = True 
    .InputTitle = "" 
    .ErrorTitle = "" 
    .InputMessage = "" 
    .ErrorMessage = "" 
    .ShowInput = True 
    .ShowError = True 
End With 

End Sub 

我在即時窗口中得到的結果是:

=INDIRECT($B$17) 
+0

感謝您的幫助,但即使是.delete代碼是相同的錯誤代碼1004 ... –

+0

@LadyMuerte,但你沒有複製'.Add類型:= xlValidateList'的下一行...沒有「」「 –

+0

我這樣做,但如果我刪除引號,該函數說錯誤1004,用三個引號的功能是完整的,但與單元格b17的內容在a18 –