簡體   English   中英

數據驗證vba中的間接函數

[英]indirect function in data validation vba

我只是一個小問題!

我使用帶有兩個驗證列表的excel做一個宏,第一個列表可以使用,但是使用INDIRECT函數可以使用。

第一個列表在單元格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)不帶引號,這是函數,我嘗試刪除引號,第一次還可以,但是之后是錯誤1004。

我知道是最小錯誤,或者我的變量是錯誤的,但我無法弄清楚。 有什么幫助嗎?

我添加了.delete,並且錯誤是相同的。

這是截圖

在此處輸入圖片說明

帶有調試的屏幕截圖在這里...

在此處輸入圖片說明

在設置Validation Formula之前,您需要通過添加代碼行來清除上一個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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM