如何检查字符串是否为有效的列名? [英] How to check if a string is a valid column name?

查看:90
本文介绍了如何检查字符串是否为有效的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在字符串变量中存储了列名(A,B,C,AB,AC,...).

I have column names (A,B,C,AB,AC,,,,) stored in string variable.

我的要求是获取列号.在此之前,我要检查给定的列名是否有效.例如, AD,AC,DF,ER,FC,KL 有效,而 World,kavisuja 无效.

My requirment is to get column number. Before that, I want to check the given column name is valid. For example AD,AC,DF,ER,FC,KL are valid where World,kavisuja are invalid.

如果我具有有效的列名,则可以通过以下代码获取:

If I have valid column name I can get by following code:

Range(ColumnChar & 1).Column

推荐答案

这就是我要这样做的方式,与注释中@ YowE3K的想法非常相似:

This is how I would have done it, pretty similar to the idea of @YowE3K in the comments:

Option Explicit

Public Sub TestMe()

    Debug.Print isValid("ZZZ")    'False
    Debug.Print isValid("ZZ")     'True
    Debug.Print isValid("ABCD")   'False

End Sub

Public Function isValid(strInput As String) As Boolean

    On Error GoTo isValid_Error

    Dim rngSet  As Range
    Set rngSet = Range(strInput & "1")
    isValid = True

    On Error GoTo 0
    Exit Function

isValid_Error:
End Function

该函数的默认值为 False ,因此,如果未将其设置为 True ,则默认情况下为 False .

The default value of the function is False, thus if it is not set to True it gives False by default.

这篇关于如何检查字符串是否为有效的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆