在VBA中使用查找功能

时间:2012-11-11 03:45:40

标签: excel-vba vba excel

1
2
3
4  
.
.

所以我有一系列从1-20开始运行的数字。我在顶部选择了数字“1”,我想搜索整个列并找到数字“9”。当我没有命名范围“rng”时代码有效;它找到数字并选择。但是当我命名数字范围时代码停止工作。范围功能有什么问题?可能是因为如果我定义Dim rng as Range,那么当我后来定义"Set rng="时,我最终无法使用".Select"".Copy"扩展名吗?

Sub macro2()

Dim rng As Range
Set rng = Range(ActiveCell, ActiveCell.End(xlDown)).Select
rng.Find(10).Select

End Sub

另外,如果我想从1-20总和整列,在数字“20”下面的最后一个单元格中,我应该使用以下代码吗?因为应用程序对象似乎没有这样做。谢谢!

rng.End(xlDown).Offset(1, 0).Select
Application.WorksheetFunction.Sum (rng.Value)

2 个答案:

答案 0 :(得分:2)

要在有效列中查找10,您可以尝试此操作(最终会选择第一个10 - 虽然中的Select通常不需要用户到代码末端的位置)

  • 测试找到的范围是否存在(即您可以在继续之前找到10
  • 如果 [lookAt] 的当前默认值为xlWhole
  • ,您还应该使用100来避免匹配xlPart
  • 使用搜索 [After] 作为Cells(1, ActiveCell.Column [搜索方向] 作为xlNext找到俯视的第一个值。

代码

Sub QuickFind()
Dim rng1 As Range
Set rng1 = ActiveCell.EntireColumn.Find(10, Cells(1, ActiveCell.Column), xlFormulas, xlWhole, , xlNext)
If Not rng1 Is Nothing Then
Application.Goto rng1
Else
MsgBox "10 not found"
End If
End Sub

第2部分

Sub Other()
Dim rng1 As Range
Set rng1 = Range(Cells(1, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column).End(xlUp))
rng1.Cells(rng1.Cells.Count).Offset(1, 0) = Application.WorksheetFunction.Sum(rng1.Value)
End Sub

答案 1 :(得分:1)

试试这个,我希望这有助于你找到特定的行号以及列名。在代码中,您可以使用

strRw = FindColumn(Sheet name, "Value which need to be found", True, "Cell Name",Row number)
sourceCOL = colname(FindColumn(Shee Name, "Value which need to be found", False, , 4))

以下是find的主要功能

Public Function FindColumn(colnocountWS As Worksheet, srcstr As String, Optional rowflag As Boolean, Optional bycol As String, Optional strw As Integer, Optional stcol As Integer) As Integer
            Dim srcrng      As Range     'range of search text
            Dim srcAddr     As String  'address of search text
            Dim stcolnm     As String

            colnocountWS.Activate
            If stcol <> 0 Then stcolnm = colname(stcol)
            If stcol = 0 Then stcolnm = "A"
            If strw = 0 Then strw = 1


            colnocountWS.Range(stcolnm & strw).Select

            If ActiveSheet.Range(stcolnm & strw) = srcstr Then
                ActiveSheet.Range(stcolnm & strw).Select
                FindColumn = 1
            Else
            If bycol = "" Then
                Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
                , LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
            Else
                Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
                , LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
            End If
            'ByPart
            If srcrng Is Nothing Then
                If bycol = "" Then
                    Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
                    , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
                Else
                    Set srcrng = colnocountWS.Cells.Find(Trim(srcstr), after:=ActiveCell, LookIn:=xlValues _
                    , LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
                End If
            End If

                If srcrng Is Nothing Then
                FindColumn = 0
                 Exit Function
                Else
                    srcAddr = srcrng.Address
                    colnocountWS.Range(srcAddr).Select
                    FindColumn = ActiveCell.Column
                    If rowflag = True Then FindColumn = ActiveCell.Row
                End If

            End If
        End Function

        'this function find column name
        Public Function colname(iFinalCol1 As Integer) As String
        Dim colnm As String
        On Error GoTo gg
        If Mid(Cells(1, iFinalCol1).Address, 3, 1) = "$" Then
                colnm = Mid(Cells(1, iFinalCol1).Address, 2, 1)
          Else
                colnm = Mid(Cells(1, iFinalCol1).Address, 2, 2)
        End If
        gg: colname = colnm

        End Function