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

2 に答える 2

2

アクティブな列で10を探すには、これを試すことができます(vbaでは、ユーザーをコードの最後の場所に移動する以外は通常は必要ありませんが、最初の列を選択することなり10ますSelect

  • 10見つかった範囲が存在することをテストします(つまり、続行する前に見つけることができます)
  • [ lookAt ]xlWhole100現在のデフォルトが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
于 2012-11-11T04:41:27.740 に答える
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
于 2015-07-09T03:44:00.063 に答える