gpt4 book ai didi

vba - 根据长度和前 3 个字符替换数字

转载 作者:行者123 更新时间:2023-12-04 21:21:42 25 4
gpt4 key购买 nike

我正在尝试替换包含客户手机号码的范围内的号码。我想更改不是 6 位、7 位、10 位和 11 位数字的数字,我也想更改前 3 位不是“032”的 10 位数字。这是我到目前为止提出的:

Sub changetold()
Dim rCel As Range
Dim sTxt As String
Dim first3numbers As String
Dim lastrow, i, currentcell As Long

Const MaxLength As Long = 11
Const MinLength As Long = 6
Const MidLength As Long = 7
Const MaxmidLength As Long = 10

first3numbers = "032"

With ActiveSheet
lastrow = .Cells(.Rows.count, "AC").End(xlUp).row
End With

currentcell = 12

For i = 13 To lastrow
currentcell = currentcell + 1
sTxt = ActiveSheet.Range("CW" & currentcell).Value

MsgBox (Len(sTxt))

If Len(sTxt) <> MaxLength Or Len(sTxt) <> MinLength Or Len(sTxt) <> MidLength Or Len(sTxt) <> MaxmidLength Then
sTxt = "101011"
End If

If Left(sTxt, 3) <> "032" And Len(sTxt) = MaxmidLength Then
sTxt = "101011"
End If
Next i
End Sub

该代码确实更改了单元格,但问题是它不准确。我想知道这将如何正确完成,如下面的示例图像:

Initial data

Desired result

最佳答案

我认为代码会是这样的

Sub changetold()
Dim sTxt As String
Dim lastrow As Long, i As Long

With ActiveSheet
lastrow = .Cells(.Rows.Count, "AC").End(xlUp).Row
End With

For i = 13 To lastrow
sTxt = Range("CW" & i)

Select Case Len(sTxt)
Case 6, 7, 11
Case 10
If Left(sTxt, 3) <> "032" Then Range("cw" & i) = "101011"
Case Else
Range("cw" & i) = "101011"
End Select
Next i
End Sub

关于vba - 根据长度和前 3 个字符替换数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44582853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com