string - Excel VBA 对大型文档进行字符串操作导致运行时错误 "1004"

标签 string vba excel

所以我正在 RenPy 上制作一本视觉小说,我不得不交换整个文档中的句子。

由于修订版本是在 Excel 文件中,因此我决定使用 Excel 宏来自动进行修复。 有三列。原始行、建议的修复和原始脚本。

example

所以我敲出了一个如下所示的脚本:

Sub MacroVOID()
Dim x As Integer
Dim y As Long
x = 2
y = 2

Do While x < 298
If StrComp(Cells(x, 1).Value, Cells(y, 3).Value, vBinaryCompare) = 0 Then
    Cells(y, 3).Value = Cells(x, 2).Value
    x = x + 1
    Else
    y = y + 1
End If
Loop

End Sub

这导致了运行时错误“1004”。

我在工作表级别保存了宏。

我是这方面的新手,因此我们将不胜感激。

最佳答案

试试这个

<小时/>
Option Explicit

Public Sub ReplaceStrings()
    Dim ws As Worksheet, cel As Range

    Set ws = ThisWorkbook.Worksheets("Sheet1")  '<-- Update Sheet Name

    With ws.UsedRange
        For Each cel In .Columns(1).Cells       'iterate through all used cells in col A
            If Len(cel.Value2) > 0 Then         'if the cell is not empy

                'in column 3: replace all instances of values in current cell
                'with the value in (current cell).offset by one column to its right (col B)
                .Columns(3).Replace cel.Value2, cel.Offset(0, 1).Value2, xlWhole

            End If
        Next
    End With
End Sub
<小时/>

Range.Replace 方法具有以下参数:

替换(什么、替换、LookAt、SearchOrder、MatchCase、MatchByte、SearchFormat、ReplaceFormat)”

帮助中的更多详细信息:

  • 内容:必需变体 您希望 Microsoft Excel 搜索的字符串。
  • 替换:必需变体替换字符串。
  • LookAt:可选变体 可以是以下 XlLookAt 常量之一:xlWhole 或 xlPart。
  • SearchOrder:可选 Variant 可以是以下 XlSearchOrder 常量之一:xlByRows 或 xlByColumns。
  • MatchCase:可选 Variant True 使搜索区分大小写。
  • MatchByte:可选变体 仅当您在 Microsoft Excel 中选择或安装了双字节语言支持时才可以使用此参数。 True 表示双字节字符仅匹配双字节字符。如果双字节字符与其单字节等效字符匹配,则错误。
  • SearchFormat:可选变体方法的搜索格式。
  • ReplaceFormat:可选变体方法的替换格式。

关于string - Excel VBA 对大型文档进行字符串操作导致运行时错误 "1004",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46644001/

相关文章:

javascript - 如果同一变量等于字符串,则更改变量的值

c# - 如何检查多个参数的 null 或空字符串? - C#

vba - 无需打开Lotus Notes即可发送邮件的VBA宏

vba - 使用 VBA 从选择中选择重复的最大数字

Java 转换 : char[] array --> String

python - python中的字符串比较

excel - 如何使用宏将数字格式化为文件名中至少包含 5 位数字?

excel - 返回有效数字位数的函数

vba - 从 VBA 中的命名范围获取值

vba - 计算包含大量文本的 Excel 列中最常用的单词?