将公式应用到excelstring中

我想添加50到这个string中间的数字部分

我有10000个string来更新。 所有这些开始像Smart/ ,比数字。

智能/ 6420 / CD-案例

会成为

智能/ 6470 / CD-案例

谢谢,如果可行的话,感谢帮助

通过使用变体数组的用户select来运行RegExp是一种快速replace结果的方法

 Sub RegexReplace() Dim rng1 As Range Dim rngArea As Range Dim lngRow As Long Dim lngCol As Long Dim lngCalc As Long Dim objReg As Object Dim lngTemp As Long Dim X() On Error Resume Next Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8) If rng1 Is Nothing Then Exit Sub On Error GoTo 0 'See Patrick Matthews excellent article on using Regular Expressions with VBA Set objReg = CreateObject("vbscript.regexp") With objReg .Pattern = "(Smart\/)(\d+)(.*)" .ignorecase = True .Global = False End With 'Speed up the code by turning off screenupdating and setting calculation to manual 'Disable any code events that may occur when writing to cells With Application lngCalc = .Calculation .ScreenUpdating = False .Calculation = xlCalculationManual .EnableEvents = False End With 'Test each area in the user selected range For Each rngArea In rng1.Areas 'The most common outcome is used for the True outcome to optimise code speed If rngArea.Cells.Count > 1 Then 'If there is more than once cell then set the variant array to the dimensions of the range area 'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks X = rngArea.Value2 For lngRow = 1 To rngArea.Rows.Count For lngCol = 1 To rngArea.Columns.Count lngTemp = CLng(objReg.Replace(X(lngRow, lngCol), "$2")) + 50 X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), "$1" & lngTemp & "$3") Next lngCol Next lngRow 'Dump the updated array back over the initial range rngArea.Value2 = X Else 'caters for a single cell range area. No variant array required lngTemp = CLng(objReg.Replace(rngArea.Value, "$2")) + 50 rngArea.Value = objReg.Replace(rngArea.Value, "$1" & lngTemp & "$3") End If Next rngArea 'cleanup the Application settings With Application .ScreenUpdating = True .Calculation = lngCalc .EnableEvents = True End With Set objReg = Nothing End Sub 

正确的解决scheme取决于您的数据中有多less变化。 如果他们都以“智能/”开头,每个数字都是4位数字,那么下面就会做你想要的。 我假设数据从A1开始。 你可以把它复制到你正在处理的1000行。

 =VALUE(MID(A1,7,4))+50 

如果有变化,那么你必须通过使用FINDLEN公式来查找/字符并修剪数字。 这是一个例子: http : //www.mrexcel.com/forum/excel-questions/444266-extract-string-between-two-characters.html 。

如果您可以select使用macros,那么使用VBA会更容易,您可以使用Splitfunction将每个值分隔在/字符处,并抓住中间的一个。 这看起来像这样:

 Public Sub AddFifty() Dim rng As Range Set rng = Range("A1:A1000") Dim tmp() As String For Each cell in rng.Cells tmp = Split(cell.Value2, "/") cell.Offset(0,1).Value2 = CLng(tmp(1)) + 50 Next cell End Sub 

请尝试:

 =MID(A1,1,FIND("/",A1))&MID(A1,FIND("/",A1)+1,LEN(A1)-FIND("/",A1,FIND("/",A1)+1)-4)+50&MID(A1,FIND("/",A1,FIND("/",A1)+1),LEN(A1)-FIND("/",A1)) 

仅在所提供的单个示例上进行testing。

另一种select是使用“文本到列”作为分隔符,将源代码分成三部分,然后将中间部分添加到50 ,然后再与CONCATENATE再次拼接在一起。