简体   繁体   English

自动填充的VBA代码

[英]VBA Code to Autofill

Have a column H with alphanumeric characters. 带有字母数字字符的H列。 Some cells in this column have the content (RAM) followed by 5 digits starting from 00000 to 99999 . 此列中的某些单元格具有内容(RAM),后跟5位数字,从0000099999
If cell H219 has the content (RAM) 23596 then i have to fill cell A219 with a comment "completed" . 如果单元格H219具有内容(RAM)23596,则我必须在单元格A219填充注释“已完成”
This has to be done for all cells with the content " (RAM) followed by 5 digits " 必须对所有内容为“ (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

One way is to use the Like operator. 一种方法是使用Like运算符。 The precise format of your string is not clear so you may have to amend (and assuming case insensitive). 字符串的确切格式尚不清楚,因此您可能需要进行修改(并假定不区分大小写)。 # represents a single number; #代表一个数字; the * represents zero or more characters. *表示零个或多个字符。

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

A non-VBA answer could be (if the cell doesn't have extra text other than (RAM) & 5 numbers): 非VBA答案可能是(如果单元格没有(RAM)和5个数字以外的其他文字):

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

My VBA answer would be: 我的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  

Cheers @Excelosaurus for heads up on the * would've forgotten it as well. 欢呼@Excelosaurus提醒*也会忘记它。 :) :)

Well, there are already 2 good answers, but allow me to paste my code here for good measure, the goal being to submerge @user2574 with code that can be re-used in his/her next endeavors: 好的,已经有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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM