2012-03-16 52 views
3

我知道,在VBA,我们可以做是否可以修复或声明VBA中单元格的类型?

Cells(4, 2).Value = 100  'the cell is an integer 
Cells(4, 2).Value = True  'the cell is Boolean 
Cells(4, 2).Value = "abc" 'the cell is Text 

是否可以修复或声明的细胞类型,例如,让Cells(4,2)只接受布尔,使得分配一个IntegerTextCells(4, 2)给一个错误?

+1

仅供参考 - 试用过的数据验证无效。 – 2012-03-16 00:29:10

+0

我主要关注VBA ......只是想确保在VBA中不可能“修复”或“声明”。 – SoftTimur 2012-03-16 00:51:33

+1

您想在使用VBA或在Excel中输入时引发错误? – JMax 2012-03-16 07:45:14

回答

4

[编辑该解决方案可以从VBA来实现,但它不能从VBA使用,即仍然可以设置单元格的值是从什么VBA(尽管不是在Excel表格中手动)。不确定OP实际需要什么。 ]

使用数据验证。

您可以通过VBA做到这一点:

Range("A1").Validation.Add Type:=xlValidateList, Formula1:="TRUE,FALSE" 

或手动(在Excel 2003:数据>验证...)

enter image description here

现在,你可以只输入布尔TRUE或单元格A1中的FALSE。如果您尝试输入其他内容,例如数字:

enter image description here

使用数据验证,也可以限制细胞只接受数字,仅整数,一定长度的文本,基本上什么。例如,要只接受文本和而不是数字,您可以使用允许:自定义,公式:=NOT(ISNUMBER(A1))

+0

+1数据验证是要走的路!为什么重新发明轮子,你说得对;)? – 2012-03-16 11:40:29

+0

这很好,但是OP需要一些VBA的东西。 – JimmyPena 2012-03-16 13:59:02

+0

@ Jean-FrançoisCorbett:关于你的编辑:Worksheet_Change Event也可以捕捉这个:) – 2012-03-16 14:23:48

4

如果您确实需要指定单元类型,则不能。尽我所知,VBA中的所有单元格都包含不同的数据类型。

如果您的意思是变体的数据类型,那么当然可以这样做。这是一个建议,它有点快而肮脏,但它的工作原理。您需要将它放在工作表代码模块中。请注意,它不测试你的布尔范围,int范围,任何相交,如果他们这样做可能会导致你一些问题。

Private Sub Worksheet_Change(ByVal Target As Range) 

    On Error GoTo handler 

    Dim cell As Range, _ 
     boolRng As Range, _ 
     intRng As Range 

    Set boolRng = Union(Sheet1.Range("A1:B2"), Sheet1.Range("E:E")) 
    Set intRng = Union(Sheet1.Range("B7:K12"), Sheet1.Range("M:M")) 

    If Not Intersect(Target, boolRng) Is Nothing Then 
     For Each cell In Intersect(Target, boolRng) 
      If cell.Value <> "" Then 
       cell.Value = CBool(cell.Value) 
      End If 
     Next cell 
    End If 

    If Not Intersect(Target, intRng) Is Nothing Then 
     For Each cell In Intersect(Target, intRng) 
      If cell.Value <> "" Then 
       cell.Value = CInt(cell.Value) 
      End If 
     Next cell 
    End If 

    Exit Sub 

handler: 
    Select Case Err.Number 
     Case 13 'Type mismatch, raised when cint/cbool/c*** fails 
      cell.Value = "" 
      Resume Next 
     Case Else 
      Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext 
    End Select 

End Sub 

编辑:我注意到你想提出一个错误,如果该值不正确地分配,你可以做的是,在错误处理部分。取而代之的

Cell.value = "" 
Resume Next 

你可以使用

Err.Raise ISuggestAnEnumForErrorNumbers, "Sheet1.Worksheet_Change(Event)", "Attempted to assign wrong type to cell." 
+0

我不会这样做所有的逻辑,但这是一个很好的刺(和+1) – JMax 2012-03-16 07:44:12

+0

不错的努力,但你正在重新发明轮子!数据验证已在Excel中实施。 – 2012-03-16 07:58:10

+0

糟糕,我可能说得太快,取决于OP实际需要什么(这不完全清楚)。 – 2012-03-16 13:03:29

4

我第二次JFC关于使用数据验证的建议。

为了测试它,把这个代码模块(久经考验

Sub Sample() 
    With Sheets("Sheet1").Range("A1") 
     .Validation.Delete 
     .Validation.Add Type:=xlValidateList, Formula1:="TRUE,FALSE" 
     .Value = "SID" 
    End With 
End Sub 

,这在相关表

Private Sub Worksheet_Change(ByVal Target As Range) 
    On Error GoTo Whoa 

    If Not Intersect(Target, Range("A1")) Is Nothing Then 
     Application.EnableEvents = False 

     On Error Resume Next 
     If Not Target.SpecialCells(xlCellTypeSameValidation).Cells.Count < 1 Then 
      Dim currentValidation As Excel.Validation 
      Set currentValidation = Target.Validation 

      If currentValidation.Type = xlValidateList Then 
       '~~> I am using INSTR. If you want you can split it using "," as delim 
       '~~> and check for the value. 
       If Not InStr(1, currentValidation.Formula1, Target.Value, vbTextCompare) Then 
        MsgBox "Incorrect Value" 
        Target.ClearContents 
       End If 
      End If 
     End If 
     On Error GoTo 0 
    End If 
LetsContinue: 
    Application.EnableEvents = True 
    Exit Sub 
Whoa: 
    MsgBox Err.Description 
    Resume LetsContinue 
End Sub 

现在试着运行模块中的Sub Sample()

+0

+1 LOL。该死的库尔。那么是否有任何你不知道答案的VBA问题? :D – 2012-03-16 15:04:16

+0

@Pradeep Kumar:是的!不幸的是,它们中有很多:(LOL。 – 2012-03-16 15:06:44

+0

实际上,您正在执行数据验证的横向执行......但这只适用于“List”数据验证。但仍然不错。 – 2012-03-16 15:42:51

相关问题