VBA代码自动填充

时间:2017-11-09 16:37:29

标签: vba excel-vba excel

H包含字母数字字符。此列中的某些单元格的内容为(RAM),后跟5位数字,从 00000 99999 。 如果单元格H219的内容(RAM)23596 ,那么我必须填充单元格A219并注释“已完成”
必须对内容为“(RAM)后跟5位”的所有单元格进行此操作

Sub Macro16_B()
    ' ' Macro16_B Macro ' '
    intRowCount = Worksheets("Reconciliation").UsedRange.Rows.Count
    For i = 11 To intRowCount
        If InStr(Range("H" & i).Value, "(RAM 00000-99999") Then
            Range("A" & i).Value = "Completed"
        End If
    Next i
End Sub

3 个答案:

答案 0 :(得分:1)

一种方法是使用Like运算符。字符串的精确格式不明确,因此您可能需要修改(并假设不区分大小写)。 #代表一个数字; *表示零个或多个字符。

Sub Macro16_B()
    Dim intRowCount As Long, i As Long
    ' ' Macro16_B Macro ' '
    intRowCount = Worksheets("Reconciliation").UsedRange.Rows.Count
    For i = 11 To intRowCount
        If Range("H" & i).Value Like "(RAM) #####*" Then
            Range("A" & i).Value = "Completed"
        End If
    Next i
End Sub

答案 1 :(得分:1)

非VBA答案可能是(如果单元格没有(RAM)& 5号码以外的其他文本):

=IFERROR(IF(LEN(VALUE(TRIM(SUBSTITUTE(H1,"(RAM)",""))))=5,"completed",""),"")

我的VBA答案是:

Sub Test()

    Dim rLastCell As Range
    Dim rCell As Range

    With Worksheets("Reconciliation")
        Set rLastCell = .Columns(8).Find("*", , , , xlByColumns, xlPrevious)
        If Not rLastCell Is Nothing Then
            For Each rCell In .Range(.Cells(1, 8), rLastCell)
                If rCell Like "*(RAM) #####*" Then
                    rCell.Offset(, -7) = "complete"
                End If
            Next rCell
        End If
    End With

End Sub  

对于*提醒的@Excelosaurus干杯也会忘记它。 :)

答案 2 :(得分:0)

嗯,已经有2个很好的答案了,但是请允许我在这里粘贴我的代码以获得良好的衡量标准,目标是将@ user2574与可以在下一步中重复使用的代码一起淹没:

Sub Macro16_B()
    'In the search spec below, * stands for anything, and # for a digit.
    'Remove the * characters if you expect the content to be limited to "(RAM #####)" only.
    Const SEARCH_SPEC As String = "*(RAM #####)*"

    Dim bScreenUpdating As Boolean
    Dim bEnableEvents As Boolean

    'Keep track of some settings.
    bScreenUpdating = Application.ScreenUpdating
    bEnableEvents = Application.EnableEvents

    On Error GoTo errHandler

    'Prevent Excel from updating the screen in real-time,
    'and disable events to prevent unwanted side effects.
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    'Down with business...

    Dim scanRange As Excel.Range
    Dim cell As Excel.Range
    Dim content As String
    Dim ramOffset As Long

    With ThisWorkbook.Worksheets("Reconciliation").Columns("H")
        Set scanRange = .Worksheet.Range(.Cells(11), .Cells(.Cells.Count).End(xlUp))
    End With

    For Each cell In scanRange
        content = CStr(cell.Value2)
        If content Like SEARCH_SPEC Then
            cell.EntireRow.Columns("A").Value = "Completed"
        End If
    Next

Recover:
    On Error Resume Next
    'Restore the settings as they were upon entering this sub.
    Application.ScreenUpdating = bScreenUpdating
    Application.EnableEvents = bEnableEvents
    Exit Sub

errHandler:
    MsgBox Err.Description, vbExclamation + vbOKOnly, "Error"
    Resume Recover
End Sub