Excel - 在选择多个单元格时在VBA中键入不匹配错误13

时间:2017-08-09 20:23:21

标签: excel vba excel-vba type-mismatch

我是VBA的新手,我遇到了一个小问题。当我只选择一个单元格时,代码工作正常。但是当我尝试选择多个单元格时,它表示类型不匹配。 我感谢任何帮助:),这是代码:

Sub Asterisk()
Dim Cell As Range
If Selection.Value = "<.0001" Then
    Selection.Value = 0.0001
Else
    Selection.Value = Selection.Value
        If (Selection.Value < 0.001) Then
            Selection.Value = "***"
        ElseIf (Selection.Value >= 0.001) And (Selection.Value < 0.01) Then
            Selection.Value = "**"
        ElseIf (Selection.Value >= 0.01) And (Selection.Value < 0.05) Then
            Selection.Value = "*"
        Else
            Selection.Value = "Not significant"
        End If
End If
End Sub

1 个答案:

答案 0 :(得分:1)

根据tigeravatar的建议,以下是你如何使用a来循环选择中的单元格

Sub Asterisk()
Dim Cell As Range
    For Each Cell In Selection
        If Cell.Value = "<.0001" Then
            Cell.Value = 0.0001
        Else
            Cell.Value = Cell.Value
                If (Cell.Value < 0.001) Then
                    Cell.Value = "***"
                ElseIf (Cell.Value >= 0.001) And (Cell.Value < 0.01) Then
                    Cell.Value = "**"
                ElseIf (Cell.Value >= 0.01) And (Cell.Value < 0.05) Then
                    Cell.Value = "*"
                Else
                    Cell.Value = "Not significant"
                End If
        End If
    Next Cell
End Sub