如果单元格值以特定数字集开头,则替换数据

时间:2015-06-22 17:12:19

标签: excel vba excel-vba

我的单元格值是数字串(单元格中总是大于5个数字,即67391853214等)

如果一个单元格以三个特定数字开头(即单元格值为67391853214,则为673)我希望单元格中的数据替换为不同的值(如果673是第一个数字,则用“790”替换整个单元格值)

我知道有一种方法可以使用asterick只使用部分单元格值,但我不是100%的语法。这是我当前的代码,但它专门搜索“### *”,而不是以“###”开头的值。非常感谢任何和所有的帮助!

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"

`

3 个答案:

答案 0 :(得分:8)

使用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"

答案 1 :(得分:3)

最好进行范围替换而不是循环遍历每个单元格以获得速度:

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

答案 2 :(得分:0)

以下是我对这个问题的看法:

RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams dateArea = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, 
90);

dateArea .addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);