2012-11-11 48 views
3
1 
2 
3 
4 
. 
. 

所以我有一個數字序列從1-20運行。我在頂部選擇了數字「1」,我想搜索整個列並找到數字「9」。該代碼適用於我未命名範圍「rng」的情況;它找到數字並選擇。但是,當我命名數字範圍時,代碼停止工作。範圍功能有什麼問題?如果我定義了Dim rng as Range,那麼當我後來定義"Set rng="時,我不能在末尾添加".Select"".Copy"擴展名嗎?另外,如果我想總結從1-20整個列,在數字「20」下面的最後一個單元格,我應該使用下面的代碼嗎?因爲應用程序對象似乎沒有這樣做。謝謝!在VBA中使用查找功能

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

請不要寫假答案補充添加到您的問題,使用編輯按鈕,並添加到您的問題。 –

+0

對不起。剛剛鞏固了評論 – gabriel

回答

2

爲了尋找10中的活性柱可以嘗試這(這最終選擇第一10 - 雖然在Select通常不需要其他比在代碼端用戶帶到位置)

  • 測試發現的存在範圍(即你可以找到10繼續之前)
  • ,你也應該使用xlWhole避免匹配100如果當前的默認[的lookAt ]xlPart
  • 使用搜索[後]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 
+0

Brett不夠謝謝你! – gabriel

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)) 

下面是主要功能找到

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