VBA - 数据验证空单元格

时间:2018-04-30 11:56:17

标签: excel vba excel-vba

我在excel中创建了命名区域,范围的命名基于sheet2上的键值。 现在我在另一个sheet1上创建了下拉列表,在sheet1上使用公式用法 - INDIRECT,再次基于key。如何在下拉列表中添加空白/特殊符号?我无法在工作表上的已排序项目之间添加空单元格。 Sheet2中: enter image description here

我有两个基于MAT / AE列的命名范围,第一个是C2:C4的范围,下一个是C5:C6。

enter image description here

我有Sheet1,我使用数据验证,使用公式INDIRECT连接MAT1和AE11,我有基于Sheet2范围的值。

所以我的问题是,如何在此列表中添加空白/特殊字符?

范围代码:

    Sub Start()

lf_index_row = 1
lf_name_space_row = 2

gf_namespace = ""

Do

lf_index_row = lf_index_row + 1

lf_material = Sheets(gc_data).Cells(lf_index_row, 1)
lf_location = Sheets(gc_data).Cells(lf_index_row, 2)

gf_new_namespace = "X" & lf_material & lf_location

If gf_new_namespace = "X" Then
 If gf_namespace = "" Then
    End
 Else
    'create namespace
    Set lf_range = Range(Cells(lf_start_number, 3), Cells(lf_end_number, 3))
    lf_range.Select
    Range(Cells(lf_start_number, 3), Cells(lf_end_number, 3)).Select
    ActiveWorkbook.Names.Add Name:=gf_namespace, RefersTo:=lf_range
    End
 End If
End If
If gf_namespace <> gf_new_namespace Then
    If gf_namespace = "" Then
        'initialize newnamespace
        gf_namespace = gf_new_namespace
        lf_start_number = lf_index_row
        lf_end_number = lf_index_row
    Else
        'create namespace
        Set lf_range = Range(Cells(lf_start_number, 3), Cells(lf_end_number, 3))
        lf_range.Select
        Range(Cells(lf_start_number, 3), Cells(lf_end_number, 3)).Select
        ActiveWorkbook.Names.Add Name:=gf_namespace, RefersTo:=lf_range
        'initialize newnamespace
        gf_namespace = gf_new_namespace
        lf_start_number = lf_index_row
        lf_end_number = lf_index_row
    End If
Else
    lf_end_number = lf_index_row
End If

Loop

End Sub

间接公式:

enter image description here

第一个命名范围的定义:

enter image description here

2 个答案:

答案 0 :(得分:3)

如果列表在Range("A1:A10")中,这就是如何实现只有一个空位的验证列表:

enter image description here

使用以下代码:

Sub TestMe()

    Dim list1               As Range
    Dim validationFormula   As String

    Set list1 = Range("A1:A10")

    Dim myCell As Range
    For Each myCell In list1
        If Not IsEmpty(myCell) Then
            validationFormula = validationFormula & myCell.Value2 & ","
        End If
    Next

    validationFormula = validationFormula & Chr(160)

    With Range("B5").Validation
        .Delete
        .Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=validationFormula
        .IgnoreBlank = False
        .InCellDropdown = True
    End With

End Sub

代码的概念是什么?验证字符串在validationFormula中通过连接Not IsEmpty()的所有单元格来生成。一旦validationFormula准备就绪,就会向其添加Chr(160),以确保我们也有空单元格可用。

如果您需要在第一个位置添加它,可以添加如下:validationFormula = Chr(160) & "," & validationFormula

enter image description here

准备好validationFormula字符串后,我们可以允许自己写.IgnoreBlank = True,只要列表中只有一个空格 - 我们需要的那个。

对于这个人来说,这是一个循环的想法 - https://superuser.com/questions/1254754/data-validation-from-2-lists-excel-2010

答案 1 :(得分:0)

检查具有行值= 3和列值= 4的单元格是否为空白,并显示以下内容:

Set objExcel = CreateObject("Excel.Application")
Set excelInput = objExcel.Workbooks.Open("myfile")
If excelInput.Sheets("Sheet1").Cells(3, 4) <> vbNullString Then
    'do the thing
End If

上面的代码是VBScript但它应该可以工作。如果没有,它在VBA中几乎相同。

相关问题