简体   繁体   English

使用VBA检查给定范围是否有效

[英]Check with VBA if given range is valid or not

I have a range variable stored as string. 我有一个范围变量存储为字符串。 I just ant to check the given range is valid or not. 我只是想检查给定范围是否有效。 For example: A11:Z4, D8:H7, B112:H80, M5:P45 not valid, A5:A5 not valid (one scenario), A11:Z11, D8:H8, A5:M5 is valid. 例如: A11:Z4, D8:H7, B112:H80, M5:P45无效, A5:A5无效(一种情况), A11:Z11, D8:H8, A5:M5有效。 I just want to get in same row, different columns. 我只想进入同一行,不同列。

How can we do this? 我们应该怎么做?

Here's some code you can use: 这是您可以使用的一些代码:

Sub Validate()
Dim rng As String
'here you can define your range
rng = "C11:D12"

If Range(rng).Rows.Count = 1 And Range(rng).Columns.Count > 1 And Range(rng).Areas.Count = 1 Then
    MsgBox "Range is valid"
Else
    MsgBox "Range is invalid"
End If

End Sub

If you print out a Range object's address inside VBA, it'll print the address in proper order. 如果在VBA中打印出Range对象的地址,它将以正确的顺序打印地址。 So, if you pass it A6:A5 , it'll know that you're actually referring to A5:A6 . 因此,如果将其传递给 A6:A5 ,它将知道您实际上是在指 A5:A6

\n

You can use that to create a UDF: 您可以使用它来创建UDF:

Edit: 编辑:

I misunderstood the question. 我误解了这个问题。 So, I've modified my answer to make rectify the mistakes. 因此,我修改了答案以纠正错误。

Note : This is basically Michal Turczyn's answer, rewritten in the form of a UDF: 注意 :这基本上是Michal Turczyn的回答,以UDF的形式重写:

Function IS_RANGE_VALID(s As String) As Boolean
    IS_RANGE_VALID = (Range(s).Rows.Count = 1 And Range(s).Columns.Count > 1)
End Function

The result: 结果:

╔═══╦══════════╦═══════╗
║   ║    A     ║   B   ║
╠═══╬══════════╬═══════╣
║ 1 ║ A11:Z4   ║ FALSE ║
║ 2 ║ D8:H7    ║ FALSE ║
║ 3 ║ B112:H80 ║ FALSE ║
║ 4 ║ M5:P45   ║ FALSE ║
║ 5 ║ A5:A5    ║ FALSE ║
║ 6 ║ A11:Z11  ║ TRUE  ║
║ 7 ║ D8:H8    ║ TRUE  ║
║ 8 ║ A5:M5    ║ TRUE  ║
╚═══╩══════════╩═══════╝

Note : Like I asked you in the comment, why is M5:P45 invalid? 注意 :就像我在评论中问您一样, M5:P45为什么无效? If it was a typo, then this should work for you. 如果是错字,那么这应该适合您。 Otherwise you'll have to be clearer about the criterion you're using to determine validity of a range. 否则,您将必须更加清楚确定范围有效性的标准。

Thank you for your help. 谢谢您的帮助。 I just used the below method to solve this issue. 我只是使用以下方法来解决此问题。

                Dim i As Integer
                Dim retval As String
                Dim retval1 As String
                Dim colFrom As String
                Dim colTo As String

        'Example : FromRange = A5, ToRange =D5
                If FromRange = ToRange  Then  'For Same Column
                    MsgBox "The Input Range specified is invalid"
                    Exit Sub
                End If

                colFrom = FromRange 
                colTo = ToRange  
        'Get the numbers only from given range and compare it
                For i = 1 To Len(colFrom)
                    If Mid(colFrom, i, 1) >= "0" And Mid(colFrom, i, 1) <= "9" Then
                        retval = retval + Mid(colFrom, i, 1)
                    End If
                Next

                For i = 1 To Len(colTo)
                    If Mid(colTo, i, 1) >= "0" And Mid(colTo, i, 1) <= "9" Then
                        retval1 = retval1 + Mid(colTo, i, 1)
                    End If
                Next

                If retval <> "" And retval1 <> "" And retval = retval1 Then
                    myCellRange= FromRange  & ":" & ToRange  
                Else
                    MsgBox "Input Range is invalid"
                    Exit Sub
                End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM