【问题标题】:check is user select data from first or other columns检查是用户从第一列或其他列中选择数据
【发布时间】:2015-01-06 02:23:50
【问题描述】:

大家好,我有一个问题,我需要一些帮助

在 vba 中,我使用输入框让用户从第一列中选​​择一个范围。但我想检查用户是否只使用了第一列中的数据。

dim x 作为范围 将 y 调暗为范围

Set X = Application.InputBox(prompt:=" Enter the range for X from the first column ", Type:=8)

'现在我想检查用户是否只使用了第一列中的数据'

Set Y=Application.InputBox(prompt:="从第二列输入y的范围", Type:=8)

'检查用户是否从第二列中选择了数据') 结束子

结束子

【问题讨论】:

  • 在 Y 之后,If Y.Column = 1 And Y.Columns.Count = 1 And Y.Areas.Count = 1 Then: do some stuff: Else: do something else: End If。此外,您应该在输入框之后测试每个变量,以检查它不是什么都没有(如果用户按下取消键就会这样)(例如If X is Nothing Then Exit Sub
  • @Cor_Blimey - Range("A1:B5").Column 仍然会给 1...
  • @TimWilliams 哎呀。好点子。将编辑。
  • 您希望用户从选定的列范围中选择一个值吗?为什么不使用带有组合框的自定义消息框?组合框的行源将是您要选择的列范围.. 这样您就不需要检查,因为除 null 之外的任何值都是有效值
  • 我希望用户从第 1 列中选择数据,如果用户也会从其他列中选择数据,那么 msgbox 会出现类似的内容

标签: arrays vba if-statement range


【解决方案1】:

编辑...

Sub Test()
    Dim x As Range, x2 As Range, bOK As Boolean
    On Error Resume Next
    Set x = Application.InputBox(prompt:=" Enter the range for X from the first column ", Type:=8)
    On Error GoTo 0

    'x will be Nothing if user cancels selection
    If Not x Is Nothing Then

        'find all cells in x which are also in ColA
        Set x2 = Application.Intersect(x, x.Parent.Columns(1))

        'x2 will be Nothing if no cells in ColA were selected
        If Not x2 Is Nothing Then
            bOK = (x2.Cells.Count = x.Cells.Count)
        End If

        If Not bOK Then
            MsgBox "Please only select cells from Column A"
        End If
    Else
        Exit Sub 'user cancelled selection
    End If
    'etc...
End Sub

【讨论】:

  • 您好,请问一下 x2 的范围究竟是什么?
  • x2 是一个范围,它代表x 中的所有单元格它们也在ColumnA 中(x 的Parent 的第一列 - 即包含工作表)。如果x2x 具有相同数量的单元格,则一定意味着x 中的所有单元格都在第一列中。
猜你喜欢
  • 1970-01-01
  • 2021-12-26
  • 2013-03-02
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 2020-03-07
  • 2016-05-29
相关资源
最近更新 更多