我試圖在包含客戶端的移動號碼的範圍上替換數字。我想更改不是6位數,7位數,10位數和11位數的數字,我也想更改其前3位數不是「032」的10位數字。這是我到目前爲止:根據長度和前3個字符替換數字
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
該代碼確實會更改單元格,但問題是它不準確。我想知道這將如何來正確地完成像下面的示例圖像:
請告訴我你的問題?你的問題? – UGP
(1)「*我想改變數字*」並不能很好地描述你想達到的目標。你需要更具體。 [編輯]你的問題,並添加你的代碼實際上做什麼和你期望它做什麼,如果有任何錯誤(哪裏和哪些錯誤)。你可能會讀[問]。 (2)附註(不是解決方案):如果你這樣做'Dim lastrow,i,currentcell As Long'只有最後一個變量是'Long'其他是'Variant'你需要指定一個類型**每個* *變量:'Dim lastrow As Long,I As Long,currentcell As Long' –
如果這些是**數字**(而不是文本),第一個字符將如何成爲「0」?如果您有具有特定單元格格式的數字,您可能需要考慮使用'.Text'屬性。 – YowE3K