vba - Word VBA 查找和替换
问题描述
我正在尝试在表格的第 2 列中找到具有特定文本“0.118”的所有单元格,并为该行执行命令列表我还尝试从该选定文本的第 5 列中获取值行并减去我在该行输入框中输入的值。
我遇到的问题是它只改变了我找到的“0.118”之一,而不是每一行中的所有。
而且我不知道如何搜索该选定行的列(5)。
任何帮助将不胜感激。
谢谢你。
Sub ConvertTo_3MM()
Dim oTable As Table
Dim stT As Long, enT As Long
Dim stS As Long, enS As Long
With Selection.Find
.Forward = True
.MatchPhrase = True
.Execute FindText:="0.118"
End With
For Each oTable In ActiveDocument.Tables
Do While Selection.Find.Execute = True
stT = oTable.Range.Start
enT = oTable.Range.End
stS = Selection.Range.Start
enS = Selection.Range.End
If stS < stT Or enS > enT Then Exit Do
Selection.Collapse wdCollapseStart
If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(1).Cell(nRow, 2).Range
.Text = "3 MM" & vbCrLf & "-" & vbCrLf & "6 MM"
End With
End If
Selection.MoveRight Unit:=wdCell
If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(1).Cell(nRow, 3).Range
.InsertAfter Text:=vbCrLf & "-" & vbCrLf & "SHANK"
End With
End If
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
response = InputBox("Cut Length For 3 MM")
If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(1).Cell(nRow, 5).Range
.Text = response & vbCrLf & "-" & vbCrLf & (column(5).value - response)
End With
End If
Selection.Find.Execute Replace:=wdReplaceAll
Loop
Selection.Collapse wdCollapseEnd
Next
Application.ScreenUpdating = True
End Sub
解决方案
如果您问题中的代码实际上做了任何事情,因为它甚至没有编译,我会感到非常惊讶。
您的代码相当混乱,所以我不完全确定我是否正确理解了您正在尝试做的事情,但试试这个:
Sub ConvertTo_3MM()
Application.ScreenUpdating = False
Dim oTable As Table
Dim response As String
For Each oTable In ActiveDocument.Tables
With oTable.Range
With .Find
.Forward = True
.MatchPhrase = True
.Text = "0.118"
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found = True
.Text = "3 MM" & vbCr & "-" & vbCr & "6 MM"
With .Rows(1)
.Cells(3).Range.InsertAfter Text:=vbCr & "-" & vbCr & "SHANK"
response = Val(InputBox("Cut Length For 3 MM"))
With .Cells(5).Range
.Text = response & vbCr & "-" & vbCr & (Val(.Text) - response)
End With
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Next
Application.ScreenUpdating = True
End Sub
推荐阅读
- matlab - MATLAB:垂直对齐子图/轴的底边
- python - AttributeError:“列表”对象没有属性“行”
- r - 在 R 中移除 NA 后,类从 data.frame 更改为整数
- javascript - 从 Google 地球引擎上的 NEX-GDDP 产品导出每日气候数据
- sql - 如何在没有 GroupBy、Distinct 的情况下查找唯一行
- r - 在ggplot中变换x,y坐标空间
- github - GitHub 页面应该 https:
.github.io/ 指向阅读文档构建? - c# - 如何匹配任何重复的字符块?
- python - 如何在 python 中将超时和重试装饰器函数应用于 google-cloud-storage 客户端?
- c - 为什么在将整数除法存储在浮点变量中后,我在 C 中得到错误的结果?