Excel VBA重复创建

时间:2015-04-09 09:16:14

标签: excel-vba vba excel

目标:

  1. 在第18栏中搜索Sheet1以获取关键字(关键字:案例等)

  2. 找到关键字后,偏移量(0,-11)

  3. 在相应的单元格中输入给定值C1008。

  4. 并重复相同的操作直至完成整列(约1500行)

  5. 我无法执行第4步。 我需要多个关键字来搜索并执行相同的步骤。

    程序:

    Sub RCIM()                                    
        Dim ws As Worksheet
        Dim aCell As Range
    
        Range("A1").Select
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        With ws
            Set aCell = .Columns(18).Find(What:="case", LookIn:=xlValues, _
                        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
            If Not aCell Is Nothing Then
                aCell.Offset(0, -10).Value = "C1008"
            Else
                MsgBox "Not Found"
             End If
        End With
    End Sub
    

1 个答案:

答案 0 :(得分:0)

如果你只想重复你的代码,请使用这样的循环语句:

Sub RCIM()                                    
    Dim ws As Worksheet
    Dim aCell As Range
    DIM isTimetoExit As Boolean
    Dim currentRow As Long   'Counter of Rows
    Dim KeyWord as String

    Range("A1").Select
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws

        isTimetoExit = False
        currentRow = 1            'If you start from row 1
        KeyWord = "case"

        DO
            ' 
            Set aCell = .Columns(18).Find(What:=KeyWord, LookIn:=xlValues, _
                            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)

            If Not aCell Is Nothing Then
                aCell.Offset(0, -10).Value = "C1008"
            Else
                MsgBox "Not Found"
            End If
            '

            ' Whit a condition go out of loop
            isTimetoExit = NOT (Trim(.Cells(currentRow, 1).Value & "") = "")
            If (isTimetoExit) THEN EXIT DO

            'Go for next row
            currentRow = currentRow + 1
            ' And set the keyword to your new value;
            KeyWord = "next case"  'But I don't know where are those KeyWords stroes !?
        LOOP
    End With
End Sub