簡體   English   中英

自動填充的VBA代碼

[英]VBA Code to Autofill

帶有字母數字字符的H列。 此列中的某些單元格具有內容(RAM),后跟5位數字,從0000099999
如果單元格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

一種方法是使用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

非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個好的答案,但是請允許我在此處粘貼我的代碼以作適當的衡量,目的是將@ 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