您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python在Excel(.xlsx)中查找和替换字符串

使用Python在Excel(.xlsx)中查找和替换字符串

我会将您的文本文件内容复制到excel文件中的新工作表中,并将该工作表命名为“ Lookup”。然后使用文本列,以获取该新表的前两列中从第一行开始的数据。

将以下代码粘贴到Excel中的模块中并运行它:

Sub Replacer()
    Dim w1 As Worksheet
    Dim w2 As Worksheet

    'The sheet with the words from the text file:
    Set w1 = ThisWorkbook.Sheets("Lookup")
    'The sheet with all of the data:
    Set w2 = ThisWorkbook.Sheets("Data")

    For i = 1 To w1.Range("A1").CurrentRegion.Rows.Count
        w2.Cells.Replace What:=w1.Cells(i, 1), Replacement:=w1.Cells(i, 2), LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Next i

End Sub
python 2022/1/1 18:39:32 有254人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶