gpt4 book ai didi

excel - 增强 excel 多个查找和替换脚本

转载 作者:行者123 更新时间:2023-12-04 20:13:59 27 4
gpt4 key购买 nike

我正在使用以下代码在 Sheet2 的 A:H 范围内查找和替换 Sheet1 中存在的单词列表(ColA 用于 FIND 单词,ColB 用于 REPLACE 单词)。它执行这项工作,但非常缓慢。可以修改此脚本以更快地运行吗?

Option Explicit
Option Base 1
Sub FindReplace()
Dim LR&, Ctr&
Dim ArrayInsen As Variant
Application.ScreenUpdating = False
With Sheets("Sheet1")
LR& = Cells(Rows.Count, "A").End(xlUp).Row
ArrayInsen = Worksheets("Sheet1").Range("A2:B" & LR&)
End With
With Sheets("Sheet2").Columns("A:H")
Application.DisplayAlerts = False
For Ctr& = LBound(ArrayInsen) To UBound(ArrayInsen)
.Replace What:=ArrayInsen(Ctr&, 1), Replacement:=ArrayInsen(Ctr&, 2), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next Ctr&
Application.DisplayAlerts = True
End With
Application.ScreenUpdating = True
End Sub

感谢您对此问题的任何帮助。

最佳答案

难道你不能通过一个带有 if 语句而不是“查找/替换”函数的数组来完成整个事情吗?

例如,如果 arr1 是一个二维数组,在 arr1(i,1) 中查找值并在 arr1(i,2) 中替换,则只需比较范围中的值,将范围放入arr2。

sub replacement()

dim arr1(), arr2() as variant
dim i,j,k,m as long

arr1=Range("A2:B")
arr2=Range("...") 'the values you want to compare and replace

for i=1 to num 'dimension of the 1st array
for j=1 to num2 'dimension of 2nd array
for k=1 to num3 '2nd dimension of arr2
if arr2(j,k)=arr1(i,1) then
arr2(j,k)=arr1(i,2)
next
next
next

然后只需在此处将数组发布到您想要的任何范围内。

关于excel - 增强 excel 多个查找和替换脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32310995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com