如何找到特定的单元格并使其成为ActiveCell

时间:2014-05-23 15:15:29

标签: excel vba excel-vba

我正在编写一个VBA代码,它将在Excel中的工作簿中打开一个特定的工作表,然后在A列中找到值为" TOTAL"的单元格。然后必须将其设置为ActiveCell,以便我的其余部分可以对包含此单元格的行执行操作。

我想要它,以便当用户运行宏时,这个单元格是专门选择的。宏运行后,这个单元格的位置会发生变化,所以无论这个值是什么单元格,我都需要它才能工作。每次宏运行时,都会在包含" TOTAL"的行上方添加一个新行。因此,这个细胞的位置在不断变化。

到目前为止,我已经提出了这个问题,只是通过论坛阅读。它仍然无法正常工作,但我是这种语言的新手,我无法确定错误的位置。

Sub Macro2()

    Dim C As Range
    Worksheets("Project Total").Select
    With Selection
        C = .Find("TOTAL", After:=Range("A2"), MatchCase:=True)
    End With

End Sub

3 个答案:

答案 0 :(得分:5)

试试这个:

Sub Macro2()
    Dim cl As Range

    With Worksheets("Project Total").Cells
        Set cl = .Find("TOTAL", After:=.Range("A2"), LookIn:=xlValues)
        If Not cl Is Nothing Then
            cl.Select
        End If
    End With
End Sub

答案 1 :(得分:0)

试试这个:

Sub activateCellContainingTOTAL()

          'Go to the worksheet
          Worsheets("Project Total").Activate

          'Start going down column A to see if you find the total
           dim loopBool as Boolean
           loopBool = True
           Worksheets("Project Total").Range("A1").Activate
           Do While loopBool=True
                if Activecell.value = "TOTAL" then
                      loop = false
                else
                      activecell.offset(1, 0).Activate
                end if
           loop
End sub

答案 2 :(得分:0)

Sub Getvaluesfromeachcolumns()

    Dim loopcounter As Integer
    Dim loopcounter1 As Integer
    Dim dumvalue As String
    Dim rrange As Range
    dumvalue = Activecell.value           'you can replace your cell reference or any value which you want to search. also you can use input method.'

    loopcounter1 = Range("A1:C1").Count
    
    For loopcounter = 1 To loopcounter1
    
        Cells(1, loopcounter).Select
    
        Range(ActiveCell.Address).Select  ' to know the active cell and address
        Set rrange = Range(ActiveCell.Address, ActiveCell.End(xlDown)).Find(dumvalue)
        
        rrange.Select

    Next loopcounter
End Sub