使用 VBscript 在 Excel 中搜索和替换多个字符 [英] Search and Replace a number of characters in Excel using VBscript

查看:14
本文介绍了使用 VBscript 在 Excel 中搜索和替换多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Excel 工作表中搜索并替换字符串的特定部分.

I need to search and to replace a specific part of a string in an Excel sheet.

这是我的代码,我不知道如何在每个 Cell.value 中准确搜索这部分.

Here is my code and I don't know how I can exactly search this part in each Cell.value.

my_new_string = "abc"
For each objSheet1 in objworkbook2.sheets
    If objSheet1.Name = "Name1" Then
        LastRow = objsheet1.UsedRange.Rows.Count + objsheet1.UsedRange.Row - 1
        For i = 1 To LastRow Step 1
            For j = 1 To 15 Step 1
                If objExcel1.Cells(i, j).value = "xyz" Then 'Here I have to check if the Cell value contains xyz and to replace it by **my_new_string**
                End if
            Next
        Next
    End If
Next

有什么帮助吗?

推荐答案

谢谢大家,

这对我来说很好.

For Each objsheet1 In objworkbook2.Sheets
    With objsheet1
        If .Name = "BatchRun" Then
            On error resume next
            For i = 1 To 15 Step 1
                For j = 1 To 10 Step 1
                    If InStr(1, .Cells(i, j).Value, my_old_string) > 0 Then
                        .Cells(i, j).Value = Replace(.Cells(i, j).Value, my_old_string, my_new_string)
                    End If
                Next
            Next 
        End If
    End with
Next

这篇关于使用 VBscript 在 Excel 中搜索和替换多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆