0

以下は、Excel で .Find 関数を使用して、シート 2 に dodCell が表示される場所を見つけ、シート 1 からシート 2 の列 18 に reeCell を追加するコードです。これは、rRange で strSearch が見つかった回数に基づいてループすると想定されています。

しかし、現在は一度だけ実行されて停止しています。「Do Whileループ」に間違いがあると思いますが、修正する方法がわかりません。

何かご意見は?

そのため、コードで指摘されたいくつかのエラーを修正した後、サブを修正しました。ループの問題に対処したと思いますが、プログラムが一度実行され、Excel がフリーズし、Excel を再起動する必要があることはわかっています。無限ループを作成したと思いますが、それを修正する方法がわかりませんか?


    Sub addnumber()
    'used to add ree value to Dod projects
    Dim sSht As Worksheet, dSht As Worksheet
    Dim lastrow As Integer
    Dim firstAddress As String
    Dim strSearch As String
    Dim ReeCell As Range, dodCell As Range, aRange As Range, rRange As Range, aaRange As Range
    Dim hold1Cell As Range, holdCell As Range, lastCell As Range
    Set sSht = Worksheets("Sheet1")
    Set dSht = Worksheets("Sheet2")
    Set rRange = sSht.Columns(18)
    Set aRange = sSht.Columns(1)
    Set aaRange = dSht.Columns(1)
    lastrow = sSht.Range("A" & Rows.Count).End(xlUp).Row
    strSearch = "2*"

    Set dodCell = rRange.Find(What:=strSearch, LookIn:=xlValues, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
    'If something dodCell holds a value then enter loop
    If Not dodCell Is Nothing Then
        'Set lastCell to dodCell
        firstAddress = dodCell.Address
        Do
                    'Set ReeCell to the value of the Ree number
                    Set ReeCell = dodCell.Offset(0, -17)
                    'Set holdCell to the Cell that holds that Dod number in "Sheet2"
                    Set holdCell = aaRange.Find(What:=dodCell, LookIn:=xlValues, _
                                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                    MatchCase:=False, SearchFormat:=False)
                    'Set hold1Cell to the location that the Ree Value should be
                    Set hold1Cell = holdCell.Offset(0, 9)
                    'Give hold1Cell the Ree # from ReeCell
                    hold1Cell = ReeCell.Value
                    Set dodCell = rRange.FindNext(dodCell)

        Loop While Not dodCell Is Nothing And dodCell.Address <> firstAddress
    End If
End Sub

4

3 に答える 3

0

あなたの Do While ループは正しく機能していると思います。セルの内容を変更するコードがループ内にもないことが問題のようです。dodCell を設定し、別の検索結果に基づいてその値を変更し、ループして次の dodCell を検索し、そのアドレス (Set dodCell = rRange.FindNextなど) を取得しますが、ループを終了してから Sub.

他のコードの一部をループに移動してみて、期待どおりの結果が得られるかどうかを確認してください。

于 2013-08-12T19:39:56.903 に答える