0

CommandButton別のワークシートの列でテキスト文字列を検索し、見つかった場合は、元のワークシートの列に「見つかった」というテキストを追加するマクロに取り組んでいます。検索テキスト文字列は、元のワークシートの 2 つの特定のセル内のテキストによって定義されます。

別のワークシートの範囲内のテキストを見つける作業コードを作成しましたが、数千行を処理する場合は非常に遅くなります。このシナリオでループを使用するようにコードを変換するにはどうすればよいですか (これが最も簡単な方法だと思います)。

私の現在のコード:

Private Sub CommandButton1_Click()
On Error Resume Next
Application.ScreenUpdating = False
Dim artist As String
artist = ActiveSheet.Range("C4").Text

Dim title As String 
title = ActiveSheet.Range("C5").Text

Dim tick As String
tick = "found"

Dim c As Range
Dim d As Range
For Each c In Sheets("Repertoire").Range("F1:F2000")
For Each d In Sheets("Repertoire").Range("G1:G2000")

If c.Value = artist And d.Value = title Then

Sheets("Dashboard").Range("F4").Value = artist
Sheets("Dashboard").Range("G4").Value = title
Sheets("Dashboard").Range("H4").Value = tick

End If
Next
Next

End Sub
4

2 に答える 2

1

Findメソッドを使用してこれを試してください

Private Sub CommandButton1_Click()
    Dim artistFound As Range, titleFound As Range, artist As String, title As String, c As Range, d As Range

    artist = ActiveSheet.Range("C4")
    title = ActiveSheet.Range("C5")

    Set c = Sheets("Repertoire").Range("F1:F2000")
    Set d = Sheets("Repertoire").Range("G1:G2000")

    Set artistFound = c.Find(What:=artist, After:=c.Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    Set titleFound = d.Find(What:=title, After:=d.Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

    If Not artistFound Is Nothing And Not titleFound Is Nothing Then
        With Sheets("Dashboard")
            .Range("F4").Value = artist
            .Range("G4").Value = title
            .Range("H4").Value = "found"
        End With
    End If
End Sub
于 2013-10-20T13:20:46.990 に答える