如果单元格值以一组特定的数字开始,请replace数据
我的单元格值是一串数字(总是大于5个单元格中的数字,即67391853214等)
如果一个单元格以三个特定的数字开始(即单元格值67391853214中的673),我希望单元格中的数据被replace为不同的值(如果673是第一个数字,则用“790”replace整个单元格的值)。
我知道有一种方法使用asterick只使用部分单元格值,但我不是100%的语法。 这是我现在使用的代码,但它是专门search“### *”,而不是以“###”开头的值。 任何和所有的帮助,非常感谢!
lastRow = Range("A" & Rows.Count).End(xlUp).Row colNum = WorksheetFunction.Match("Number", Range("A1:CC1"), 0) For Each c In Range(Cells(2, colNum), Cells(lastRow, colNum)) If c.Value = "614*" _ Or c.Value = "626*" _ Or c.Value = "618*" _ Or c.Value = "609*" _ Or c.Value = "605*" Then c.Value = "737"
`
使用LEFT()
函数,如下所示:
lastRow = Range("A" & Rows.Count).End(xlUp).Row colNum = WorksheetFunction.Match("Number", Range("A1:CC1"), 0) For Each c In Range(Cells(2, colNum), Cells(lastRow, colNum)) If LEFT(c.Value,3) = "614" _ Or LEFT(c.Value,3) = "626" _ Or LEFT(c.Value,3) = "618" _ Or LEFT(c.Value,3) = "609" _ Or LEFT(c.Value,3) = "605" Then c.Value = "737"
更好地做一个范围replace而不是循环通过每个单元格的速度:
Dim rng1 As Range Dim LastRow As Long Dim ColNum As Long LastRow = Range("A" & Rows.Count).End(xlUp).Row On Error Resume Next ColNum = Application.Match("Number", Range("A1:CC1"), 0) On Error GoTo 0 If Column Is Nothing Then Exit Sub Set rng1 = Range(Cells(2, ColNum), Cells(LastRow, ColNum)) With rng1 .Replace "626*", "727", xlWhole .Replace "618*", "727", xlWhole .Replace "609*", "727", xlWhole .Replace "737*", "727", xlWhole End With
这是我的问题:
Sub SO() Dim MyString As String MyString = "614,626,618,609,605" For X = 1 To Range("C" & Rows.Count).End(xlUp).Row If Replace(MyString, Left(Range("C" & X).Value, 3), "") <> MyString Then Range("C" & X).Value = "737" Next End Sub