VBA : 在文本框中查找字符串并将其更改为某个字符串
VBA : find strings in the textbox and change them into a certain string
我想制作一个宏,每个月在文本框中更改为在对话框中输入的特定月份。
这是我写的。没有错误,但它也没有完成工作。能帮忙找出问题吗?谢谢。
Sub ChangeMonth()
Dim xWs As Worksheet
Dim shp As Shape
Dim xFindStr As String
Dim xReplace As String
Dim z As Integer
Set xWs = Application.Sheets("Mail")
xReplace = Application.InputBox("Replace to :", "Replacement", "", Type:=2)
'On Error Resume Next
For Each shp In xWs.Shapes
xValue = shp.TextFrame.Characters.Text
For x = 1 To 12
shp.TextFrame.Characters.Text = VBA.Replace(xValue, MonthName(x), "xxxx")
Next x
shp.TextFrame.Characters.Text = VBA.Replace(xValue, "xxxx", xReplace)
Next
End Sub
这个主要问题是您将所有 MonthName 替换为 "xxxx",但您将该值直接放在形状的文本中,但您使用了 xValue
:
Sub ChangeMonth()
Dim xwS As Worksheet
Dim ShP As Shape
Dim xFindStr As String
Dim xReplace As String
Dim z As Integer
Set xwS = ThisWorkbook.Sheets("Mail")
xReplace = InputBox("Replace to :", "Replacement", "", Type:=2)
'On Error Resume Next
For Each ShP In xwS.Shapes
xValue = ShP.TextFrame.Characters.Text
For x = 1 To 12
xValue = VBA.Replace(xValue, MonthName(x), xReplace)
Next x
ShP.TextFrame.Characters.Text = xValue
Next ShP
End Sub
我想制作一个宏,每个月在文本框中更改为在对话框中输入的特定月份。
这是我写的。没有错误,但它也没有完成工作。能帮忙找出问题吗?谢谢。
Sub ChangeMonth()
Dim xWs As Worksheet
Dim shp As Shape
Dim xFindStr As String
Dim xReplace As String
Dim z As Integer
Set xWs = Application.Sheets("Mail")
xReplace = Application.InputBox("Replace to :", "Replacement", "", Type:=2)
'On Error Resume Next
For Each shp In xWs.Shapes
xValue = shp.TextFrame.Characters.Text
For x = 1 To 12
shp.TextFrame.Characters.Text = VBA.Replace(xValue, MonthName(x), "xxxx")
Next x
shp.TextFrame.Characters.Text = VBA.Replace(xValue, "xxxx", xReplace)
Next
End Sub
这个主要问题是您将所有 MonthName 替换为 "xxxx",但您将该值直接放在形状的文本中,但您使用了 xValue
:
Sub ChangeMonth()
Dim xwS As Worksheet
Dim ShP As Shape
Dim xFindStr As String
Dim xReplace As String
Dim z As Integer
Set xwS = ThisWorkbook.Sheets("Mail")
xReplace = InputBox("Replace to :", "Replacement", "", Type:=2)
'On Error Resume Next
For Each ShP In xwS.Shapes
xValue = ShP.TextFrame.Characters.Text
For x = 1 To 12
xValue = VBA.Replace(xValue, MonthName(x), xReplace)
Next x
ShP.TextFrame.Characters.Text = xValue
Next ShP
End Sub