[英]Applying a formula into an excel string
我想在字符串中間的數字部分添加50
我有10000個字符串要更新。 所有這些都像Smart/
一樣開始,而不是數字。
Smart / 6420 / CD盒
會成為
Smart / 6470 / CD盒
感謝並感謝您的幫助(如果可行)
使用變量數組對用戶選擇運行RegExp是一種就地替換結果的快速方法
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
正確的解決方案取決於您的數據有多少變化。 如果它們都以“ Smart /”開頭,並且每個數字都為4位數字,那么以下內容將滿足您的要求。 我假設數據從A1開始。 您可以將其復制到要處理的1000行中。
=VALUE(MID(A1,7,4))+50
如果存在差異,則必須使用FIND
和LEN
公式來查找/
字符並修剪掉數字,以解決這一問題。 這是一個示例: http : //www.mrexcel.com/forum/excel-questions/444266-extract-string-between-two-characters.html 。
如果您可以選擇使用宏,則使用VBA會容易得多,在VBA中,您可以使用Split
函數在/
字符處拆分每個值並在中間獲取一個值。 看起來像這樣:
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))
僅在提供的單個示例上進行了測試。
另一個選擇是使用帶有/
文本到列作為分隔符,將源分成三部分,然后在中間部分添加50
,然后使用CONCATENATE將所有內容重新縫合在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.