vba 如何查找特定单元格并使其成为 ActiveCell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23832861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:14:02  来源:igfitidea点击:

How to find a specific cell and make it the ActiveCell

excelvbaexcel-vba

提问by user3669425

I am writing a VBA code that will open a specific worksheet in my workbook in Excel, and then find the cell in Column A that has the value "TOTAL". This then must be set as the ActiveCell, so that the rest of my macro can perform actions on the row containing this cell.

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

I want it so that when the user runs the macro, this cell is specifically chosen right off the bat. The position of this cell will change after the macro is run, so I need it to work no matter what cell this value is in. Everytime the macro runs, a new row is added above the row containing "TOTAL" and therefore the position of this cell is ever-changing.

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

So far I have come up with this, just from readin through forums. It still doesn't work, but I'm new to this language and I can't determine where the error is.

到目前为止,我已经想出了这个,只是从阅读论坛。它仍然不起作用,但我是这种语言的新手,我无法确定错误在哪里。

Sub Macro2()

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

End Sub

回答by Alex P

Try this:

尝试这个:

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

回答by BrainO2

Try this:

尝试这个:

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