【问题标题】:Excel Macro: Find and Compare Username from a Range of Authorized UsernamesExcel 宏:从一系列授权用户名中查找和比较用户名
【发布时间】:2012-07-27 09:28:53
【问题描述】:

Excel 宏:如何编写此“如果用户名 [示例代码:Environ("Username")] 等于此范围内的值之一 [示例:我从工作簿创建的范围:范围(“Authorized_Users”) ] 然后..." 谢谢!

【问题讨论】:

    标签: excel vba object date-range


    【解决方案1】:

    基本上你必须完全按照你所写的去做。您的解决方案目前缺少两件事:

    1. 一种检索用户名的方法。
    2. 一种在范围内搜索的方法。

    这两项任务应该没有那么难,因为 excel 已经提供了一些很棒的功能来做到这一点。

    对于第二点,您必须创建一个搜索范围的函数。 find in range 函数看起来像这样:

    Function ExistsInRange(range As range, name As String) As Boolean
        ExistsInRange = False
        Dim resultRange As range
        If Trim(name) <> "" Then
            With range
                Set resultRange = .Find(What:=name, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not resultRange Is Nothing Then
                    ExistsInRange = True
                End If
            End With
        End If
    End Function
    

    如果您想在 B 列中的所有行中搜索用户名“jeff”,调用将如下所示:

    If ExistsInRange( sheet.Range("B:B"), "jeff") Then 
    

    【讨论】:

      【解决方案2】:

      您的规范非常接近所需的代码...

      If Not IsError(Application.Match(Environ("Username"), [Authorized_Users], 0)) Then
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-24
        相关资源
        最近更新 更多