我正在尝试替换
等单元格中的值10.1
10.2
10.2.1
10.2.2
10.3
对于输入值,例如,我想将第一行和第二行从10.1替换为5.1,然后为10.2 10.2.1等启动不同的替换序列,这也可能是不同的字符长度(例如10.10.1) 3.1.1 3.1.2 ...因此,从本质上讲,它会将第一个“。”之前的整个第一个数字替换为给定的输入值,然后用另一个输入值重新编号“。”之间的中间数字。 x-2等,这是我正在使用的代码:
Sub replace ()
Dim rowstrt as string, rowstp as string
Dim i as integer
Dim oldsection as string
Dim newsection as string
Dim srt as string
Dim replacements as string
Dim numberofreplacements as long
Dim cell as variant
Rowstrt = inputbox(“please enter starting row number:”, “section numbering”)
Rowstp = inputbox(“please enter ending row number:”, “section numbering”)
Oldsection = inputbox(“current section number on worksheet for rows “ & rowstrt & “ through “ & rowstp & “:”, “section numbering”)
Newsection = inputbox(“replacement section number on worksheet for rows “ & rowstrt & “ through “ & rowstp & “:”, “section numbering”)
Startingposition = 1
Numberofreplacements = 1
Activesheet.cells(rowstrt,1).select
i = 1
For i = rowstrt to rowstp
Mycell = vba.replace(mycell, oldsection, newsection, startingposition, numberofreplacements)
Activecell.offset(1,0).select
Mycell = activecell
Next I
End sub
我的问题是我的输入值没有取代我的单元格值
答案 0 :(得分:-1)
下面的代码应做您想要的。
Sub ReplaceSectionNumber()
' "Replace" is a VBA object.
' don't use such names to name your procedures or variables.
' to check, select the word and press F1
Dim rowStrt As String, rowStp As String
Dim oldSection As String
Dim newSection As String
' Dim Srt As String
' Dim Replacements As String
' Dim NumberOfReplacements As Long
' Dim Cell As Variant
Dim Tmp() As String
Dim R As Long
rowStrt = InputBox("Please enter starting row number:", "Section numbering")
rowStp = InputBox("Please enter ending row number:", "Section numbering")
oldSection = InputBox("Current section number on worksheet for rows " & rowStrt & _
" through " & rowStp, "Section numbering")
newSection = InputBox("Replacement section number on worksheet for rows " & rowStrt & _
" through " & rowStp, "Section numbering")
Application.ScreenUpdating = False
For R = rowStrt To rowStp
Tmp = Split(Cells(R, "A").Value, ".")
If UBound(Tmp) > 0 Then ' ignore like "10"
If Tmp(0) = oldSection Then ' ignore non-matching
Tmp(0) = newSection
Cells(R, "A").Value = Join(Tmp, ".")
End If
End If
Next R
Application.ScreenUpdating = True
' startingposition = 1
' NumberOfReplacements = 1
' ActiveSheet.Cells(rowStrt, 1).Select
' i = 1
' For i = rowStrt To rowStp
' Mycell = VBA.replace(Mycell, oldSection, newSection, startingposition, NumberOfReplacements)
' ActiveCell.Offset(1, 0).Select
' Mycell = ActiveCell
' Next i
End Sub
但是,我的代码不会修改周期之间的任何数字,因为我不理解可能会发生这种变化的规则。