有特定文本时启动条件

时间:2016-07-13 09:04:37

标签: excel vba excel-vba

我想复制工作表1中B列和C列的末尾 到工作表2中的B和C列

我有这个: Worksheet1

以上是:above

这是代码

'Copy of "Maintenances" in worksheet 2
                n = 58 'start to look row 58
                j = 2 'copy in row 2 in worksheet 2
                Sheets("1").Select 'select sheet 1 
             
                Do While Cells(n, 1) <> "x" 'do for each cells untill there is a "x" in column A                    
                    If Cells(n, 1) <> "x" Then 'if column A is empty, then :                    
                        Sheets("2").Cells(j, 2) = Sheets("1").Cells(n, 2) '                             
Sheets("2").Cells(j, 3) = Sheets("1").Cells(n, 3) '                             
j = j + 1
                    End If 'fin du if
                        n = n + 1
                Loop 'retourne au do while

我想复制工作表2中“维护”下的所有行,但“维护”行可以更改。问题不在于行结束时,而是在行开始时。在我的代码中,它将列B和C复制到58之间,当第1列中有“x”时,但我希望复制在B列中开始“维护”

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,那么您可以使用以下代码替换n = 58 'start to look row 58,以确定变量n的值作为起始行。

Dim WordToFind As String, FirstWord As String
Dim FindWord

WordToFind = "Maintenance"
    With Sheets("1").Range("A:C")
        Set FindWord = .Find(what:=WordToFind, SearchDirection:=xlNext)

        If Not FindWord Is Nothing Then
            FirstWord = FindWord.Row
            Do
                n = FindWord.Row + 1 'The value of n start from the row below "Maintenance"
                Exit Do
            Loop While Not FindWord Is Nothing And FindWord.Row <> FirstWord
        Else
            MsgBox "There is no word " & WordToFind
        Exit Sub
        End If
    End With

您可以根据要查找的字词或字符串更改值WordToFind