excel 允许字母数字值而不是isnumeric

kr98yfug  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(129)

我有下面的代码输入一个函数,并复制方向和边界的上述行。但在这只接受数值,我如何修改代码,使我可以输入字母数字值在该单元格。
Below is the code

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B:B")) Is Nothing Then
        If IsNumeric(Target.Value) Then ' Check if cell contains a numeric value
            If Target.Value <> "" Then
            Range("A" & Target.Row).Formula = "=IF(B" & Target.Row & "<>"""",ROW()-ROW($A$15)+1,"""")"
            ' Copy border, border color and orientation from row above
            With Range("A" & Target.Row & ":H" & Target.Row)
                .Borders.LineStyle = .Offset(-1, 0).Borders.LineStyle
                .Borders.Color = .Offset(-1, 0).Borders.Color
                .Orientation = .Offset(-1, 0).Orientation
            End With
        Else
            ' Check if entire row in column B is empty
            If WorksheetFunction.CountA(Range("B" & Target.Row & ":H" & Target.Row)) = 0 Then
                ' Delete entire row
                Rows(Target.Row).Delete
            Else
                ' Clear contents of column A to H for the row where value was deleted in column B
                Range("A" & Target.Row & ":H" & Target.Row).ClearContents
            End If
        End If
    End If
End If
End Sub
iqih9akk

iqih9akk1#

这里有一个小的 * Function *,您可以将其添加到代码中,以赋予它IsAlphaNumeric功能。

Function IsAlphaNumeric(t) as Boolean
    Dim i as Long
    IsAlphaNumeric = True
    For i = 1 To Len(t)
        If Not (Mid(t, i, 1) Like "[A-z0-9]") Then
            IsAlphaNumeric = False
            Exit For
        End If
    Next
End Function

您可以像这样使用它:

If IsAlphaNumeric(Target.Value) Then ' Check if cell contains alpha-numeric value

相关问题