如何将A列中的特定字符替换为Excel中B列中的值?

时间:2013-11-12 02:25:08

标签: excel copy paste

我在Excel工作。下面的代码是A列中的4行。在B列中,我有一系列从0开始的整数。我希望B列中的值替换“?”在A栏中,我搜索了Google的高级复制和粘贴以及在Excel中查找和替换技术,但结果只是告诉我使用paste special。我尝试在“查找和替换”工具中尝试引用列B,但无法执行此操作。任何帮助将不胜感激。

"<class type=""sItemManager::cITEM_PARAM_DATA"">
<s16 name=""data.mNum"" value=""900""/>
<s16 name=""data.mItemNo"" value=""?""/>
<u32 name=""data.mFlag"" value=""1""/>
<u16 name=""data.mChgNum"" value=""0""/>
<u16 name=""data.mDay1"" value=""0""/>
<u16 name=""data.mDay2"" value=""0""/>
<u16 name=""data.mDay3"" value=""0""/>
<s8 name=""data.mMutationPool"" value=""0""/>
<s8 name=""data.mOwnerId"" value=""4""/>
<u32 name=""data.mKey"" value=""0""/>
</class>"
"<class type=""sItemManager::cITEM_PARAM_DATA"">
<s16 name=""data.mNum"" value=""900""/>
<s16 name=""data.mItemNo"" value=""?""/>
<u32 name=""data.mFlag"" value=""1""/>
<u16 name=""data.mChgNum"" value=""0""/>
<u16 name=""data.mDay1"" value=""0""/>
<u16 name=""data.mDay2"" value=""0""/>
<u16 name=""data.mDay3"" value=""0""/>
<s8 name=""data.mMutationPool"" value=""0""/>
<s8 name=""data.mOwnerId"" value=""4""/>
<u32 name=""data.mKey"" value=""0""/>
</class>"
"<class type=""sItemManager::cITEM_PARAM_DATA"">
<s16 name=""data.mNum"" value=""900""/>
<s16 name=""data.mItemNo"" value=""?""/>
<u32 name=""data.mFlag"" value=""1""/>
<u16 name=""data.mChgNum"" value=""0""/>
<u16 name=""data.mDay1"" value=""0""/>
<u16 name=""data.mDay2"" value=""0""/>
<u16 name=""data.mDay3"" value=""0""/>
<s8 name=""data.mMutationPool"" value=""0""/>
<s8 name=""data.mOwnerId"" value=""4""/>
<u32 name=""data.mKey"" value=""0""/>
</class>"
"<class type=""sItemManager::cITEM_PARAM_DATA"">
<s16 name=""data.mNum"" value=""900""/>
<s16 name=""data.mItemNo"" value=""?""/>
<u32 name=""data.mFlag"" value=""1""/>
<u16 name=""data.mChgNum"" value=""0""/>
<u16 name=""data.mDay1"" value=""0""/>
<u16 name=""data.mDay2"" value=""0""/>
<u16 name=""data.mDay3"" value=""0""/>
<s8 name=""data.mMutationPool"" value=""0""/>
<s8 name=""data.mOwnerId"" value=""4""/>
<u32 name=""data.mKey"" value=""0""/>
</class>"

1 个答案:

答案 0 :(得分:0)

尝试以下操作(您可以删除为调试目的而包含的一些变量):

Sub test()
    Dim rng As Range, cl As Range
    Set rng = Range("A1:A4")
    Dim cl1 As Range, cl2 As Range
    For Each cl1 In rng
      Set cl2 = cl1.Offset(0, 1)
      Dim ival As Integer
      ival = cl2.Value
      Dim res As String
      res = Application.WorksheetFunction.Substitute(cl1.Text, "?", Trim(Str(ival)))
      cl1 = res
    Next cl1
End Sub

或者

Sub test()
    Dim rng As Range, cl As Range
    Set rng = Range("A1:A4")
    Dim cl1 As Range, cl2 As Range
    For Each cl1 In rng
      Set cl2 = cl1.Offset(0, 1)
      Dim ival As Integer
      ival = cl2.Value
      cl1.Replace What:="~?", Replacement:=Str(ival)
    Next cl1
End Sub