要自动填充的VBA代码
有一个带有字母数字字符的列要自动填充的VBA代码,vba,excel,Vba,Excel,有一个带有字母数字字符的列H。此列中的某些单元格的内容(RAM)后跟从00000到99999的5位数字。 如果单元格H219的内容(RAM)为23596,则我必须在单元格A219中填入注释“已完成” 必须对内容为“(RAM)后跟5位”的所有单元格执行此操作 一种方法是使用Like操作符。字符串的精确格式不清楚,因此您可能需要修改(并假设不区分大小写)。#表示单个数字;*表示零个或多个字符 Sub Macro16_B() Dim intRowCount As Long, i As Long
H
。此列中的某些单元格的内容(RAM)后跟从00000到99999的5位数字。如果单元格
H219
的内容(RAM)为23596,则我必须在单元格A219
中填入注释“已完成”必须对内容为“(RAM)后跟5位”的所有单元格执行此操作
一种方法是使用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
一种方法是使用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个数字以外的额外文本):
我的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为*
上的提醒干杯,我也会忘记的。:) 非VBA答案可以是(如果单元格没有(RAM)和5个数字以外的额外文本):
我的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为
*
上的提醒干杯,我也会忘记的。:) 好吧,已经有两个很好的答案了,但请允许我将代码粘贴到这里,以便更好地衡量,目标是用代码淹没@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
好的,已经有两个很好的答案了,但是请允许我将我的代码粘贴到这里,目的是让@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
你能展示你到目前为止尝试过的吗?Sub-Macro16_B()''Macro16_B Macro''intRowCount=工作表(“对账”)。如果InStr(范围(“H”)和i.Value,“(RAM 00000-99999”)然后是范围(“A”&i.Value=”完成,则使用i=11的drange.Rows.Count到intRowCount如果下一步结束,我将结束对编码的完全陌生。因此,我不确定我的代码!!如果您试图找到包含(RAM)的单元格,那么您应该将instr条件更改为
如果instr(1,Range(“H”)和i.Value,“(RAM)”)
。您能展示您迄今为止所做的尝试吗?Sub-Macro16_B()“”Macro16_B Macro“”intRowCount=工作表(“对账”).UsedRange.Rows.Count For i=11如果InStr(范围(“H”和“i”).Value为“(RAM 00000-99999”),那么范围(“A”和“i”).Value=“已完成”如果下一步结束,我将结束对编码的次完全陌生。因此,我不确定我的代码!!如果您试图查找包含(RAM)的单元格,那么您应该将instr条件更改为If instr(1,Range(“H”)和i).Value(“RAM”)
。您缺少搜索字符串周围的*三明治。编辑:除非我们查找整个单元格内容,否则这是可能的……OP说“有内容”,所以我认为它可能包含更多内容。@Excelosaurus-是的,很好,将等待OP的澄清。“(RAM)00009 S2X”(这是我字符串的精确格式)。我添加了一个星号,它应该覆盖S2X(或数字后面的任何内容)。“(RAM)00009 S2X”-字符串的最后三个字符是字母、数字或字母数字。但字符串的第一部分始终是(RAM)后跟5位数字,最后3个字符更改!!您缺少搜索字符串周围的*三明治。编辑:除非我们查找整个单元格内容,否则这是可能的…OP说“有内容”,所以我认为它可能包含更多内容。@Excelosaurus-是的,很好,将等待OP的澄清。”(RAM)00009 S2X“(这是我字符串的精确格式)。我添加了一个星号,它应该覆盖S2X(或数字后面的任何内容)。“(RAM)00009 S2X”-字符串的最后三个字符是字母、数字或字母数字。但字符串的第一部分始终是(RAM)然后是5位数字,最后3个字符发生变化!!我给你的投票是因为我提供了两种做事方式,让你的代表超过9000。干杯。我想我还有一小段路要走。你给我的投票是因为我提供了两种做事方式,让你的代表超过9000。干杯。我想我还是获得9k的一个小方法。感谢我们的最好方法是,您投票选出有用的答案,并选择带有绿色大对勾的最佳答案。@SJR-如果,字符串在多列中该怎么办?感谢我们的最好方法是,您投票选出有用的答案,并选择带有绿色大对勾的最佳答案。@SJR-如果,字符串在多列中该怎么办伦姆斯??