大小写为真时如何选择单元格

时间:2016-02-08 01:47:24

标签: excel vba excel-vba

首先,昨天开始尝试学习Excel和VBA ....所以请记住。

Private Sub CommandButton2_Click()

For a = 1 To myLastRow
    Select Case ActiveWorkbook.Sheets("Sheet2").Cells(a, 2).Value
        Case Is = myOrderNumber
            ActiveWorkbook.Sheets("Sheet2").Cells(a, 2).Active
        Case False: MsgBox "False"
    End Select
Next a
End Sub

我想知道哪个单元格或与我的变量匹配的单元格行。这不符合我的要求.......

谢谢你们

3 个答案:

答案 0 :(得分:1)

我感谢您正在学习代码。但除了使范围语法正确之外,还有两种更好的方法(效率明智)如下所示

  1. 阵列
  2. 重切

    Private Sub CommandButton2_Click()
    Dim myLastRow As Long
    Dim myOrderNumber As Long
    Dim lngCnt As Long
    Dim ws As Worksheet
    Dim X
    
    myOrderNumber = 2
    
    Set ws = ActiveWorkbook.Sheets("Sheet2")
    
    X = ws.Range(ws.[b1], ws.[b10])
    For lngCnt = 1 To UBound(X)
    If X(lngCnt, 1) = myOrderNumber Then MsgBox "True " & lngCnt
    Next
    
    End Sub
    
    1. 评估
    2. 来自Is it possible to fill an array with row numbers which match a certain criteria without looping?

      myOrderNumber = 2
      MsgBox Join(Filter(Application.Transpose(Application.Evaluate("=IF(B1:B10=" & myOrderNumber & ",ROW(B1:B10),""x"")")), "x", False), ",")
      

答案 1 :(得分:0)

试试这个

Private Sub CommandButton2_Click()
Dim myLastRow As Long, a As Long, myOrderNumber As Long

myLastRow = 10
For a = 1 To myLastRow
    With ActiveWorkbook.Sheets("Sheet2")
        If .Cells(a, 2).Value = myOrderNumber Then
            MsgBox "True " & .Cells(a, 2).Row
        Else
            Msgbox "False"
        End If
    End With
Next a
End Sub

答案 2 :(得分:0)

对于a = 1到myLastRow

With ActiveWorkbook.Sheets("Sheet2")
    If .Cells(a, 2).Value = myOrderNumber Then
        myRow = .Cells(a, 2).Row
        MsgBox "True " & myRow
    Else
        'MsgBox "False "
    End If
End With

接下来