Search/Replace 一列影响整个工作表
Search/Replace in One Column Impacting Entire Worksheet
在 L 列(仅)中,我想用 "True" 替换任何数据实例,而不管 L 列的任何单元格中最初是什么。我试过的代码是:
With ActiveSheet
intLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Let strSelectRange = "L2" & ":" & "L" & intLastRow
Range(strSelectRange).Select
Cells.Replace What:="*", Replacement:="True", LookAt:=xlPart _
, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End With
首先,我使用了 .Rows.Count、"A",因为在那一列中每一行都有数据,所以我知道在 L 列中有多少行要往下。在 L 列中,许多单元格将是空白的.
当我 运行 这样做时,整个工作表中包含任何内容的每个单元格都会更改为 True,而不仅仅是 L 列中的数据。
我尝试的另一种方法是:
Range("L2:L1200").Select
Selection.Replace What:="*", Replacement:="True", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
我不喜欢的是,我选择 L1200 作为行数只是为了确保我搜索的距离比可以包含数据的实际最后一行更远。我担心这种方法有时会引起某种问题。
我真正想知道的是我在第一个代码示例中做错了什么。
感谢您提供的任何帮助!!!
Range(strSelectRange).Select
选择一个范围(尽管最好选择 avoid Select),但随后您的代码对该选择不执行任何操作,因为 Cells
是整个 sheet.
也许您想要:
Range(strSelectRange).Replace What:="*", Replacement:="True", LookAt:=xlPart
在列中搜索和替换
- 始终使用 Option Explicit,以更快地了解正在发生的
错误并被迫声明变量。
- 您应该始终将您的行声明为 Long。
- 当您使用 With 语句时,您会在所有内容上使用点,甚至
在 .Range 和 .Cells 等上。代码在这种情况下可能有效
(ActiveSheet) 无论如何,但它是不正确的。
- 避免使用 ActiveSheet,使用工作表名称。
- 避免使用 Select。有很多关于这个的帖子(文章)。
- 当你在后面没有任何东西的情况下使用 Cells 时,它指的是所有
工作表中的单元格。
- 替换函数(查找函数)中的第一件事是范围
您要替换的位置(查找、搜索)。它可以是一列,它
可以是单元格或只是一个较小的范围。
代码
Sub SROneColumn()
Const cVntLRColumn As Variant = "A" ' Last Row Column Letter/Number
Const cVntCriteria As Variant = "L" ' Criteria Column Letter/Number
Const cLngFirstRow As Long = 2 ' First Row Number
Const cStrReplace As String = "True" ' Replace String
Dim lngLastRow As Long ' Last Row Number
Dim strSelectRange As String ' Select Range Address
With ActiveSheet
lngLastRow = .Cells(.Rows.Count, cVntLRColumn).End(xlUp).Row
strSelectRange = .Range(.Cells(cLngFirstRow, cVntCriteria), _
.Cells(lngLastRow, cVntCriteria)).Address
.Range(strSelectRange).Replace What:="*", Replacement:=cStrReplace, _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False
End With
End Sub
在不使用对象变量的情况下使用工作表的有趣方式:
Sub SRSheet()
Const cStrSheet As Variant = "Sheet1" ' Worksheet Name/Index
With ThisWorkbook.Worksheets(cStrSheet)
End With
End Sub
在 L 列(仅)中,我想用 "True" 替换任何数据实例,而不管 L 列的任何单元格中最初是什么。我试过的代码是:
With ActiveSheet
intLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Let strSelectRange = "L2" & ":" & "L" & intLastRow
Range(strSelectRange).Select
Cells.Replace What:="*", Replacement:="True", LookAt:=xlPart _
, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End With
首先,我使用了 .Rows.Count、"A",因为在那一列中每一行都有数据,所以我知道在 L 列中有多少行要往下。在 L 列中,许多单元格将是空白的.
当我 运行 这样做时,整个工作表中包含任何内容的每个单元格都会更改为 True,而不仅仅是 L 列中的数据。
我尝试的另一种方法是:
Range("L2:L1200").Select
Selection.Replace What:="*", Replacement:="True", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
我不喜欢的是,我选择 L1200 作为行数只是为了确保我搜索的距离比可以包含数据的实际最后一行更远。我担心这种方法有时会引起某种问题。
我真正想知道的是我在第一个代码示例中做错了什么。
感谢您提供的任何帮助!!!
Range(strSelectRange).Select
选择一个范围(尽管最好选择 avoid Select),但随后您的代码对该选择不执行任何操作,因为 Cells
是整个 sheet.
也许您想要:
Range(strSelectRange).Replace What:="*", Replacement:="True", LookAt:=xlPart
在列中搜索和替换
- 始终使用 Option Explicit,以更快地了解正在发生的 错误并被迫声明变量。
- 您应该始终将您的行声明为 Long。
- 当您使用 With 语句时,您会在所有内容上使用点,甚至 在 .Range 和 .Cells 等上。代码在这种情况下可能有效 (ActiveSheet) 无论如何,但它是不正确的。
- 避免使用 ActiveSheet,使用工作表名称。
- 避免使用 Select。有很多关于这个的帖子(文章)。
- 当你在后面没有任何东西的情况下使用 Cells 时,它指的是所有 工作表中的单元格。
- 替换函数(查找函数)中的第一件事是范围 您要替换的位置(查找、搜索)。它可以是一列,它 可以是单元格或只是一个较小的范围。
代码
Sub SROneColumn()
Const cVntLRColumn As Variant = "A" ' Last Row Column Letter/Number
Const cVntCriteria As Variant = "L" ' Criteria Column Letter/Number
Const cLngFirstRow As Long = 2 ' First Row Number
Const cStrReplace As String = "True" ' Replace String
Dim lngLastRow As Long ' Last Row Number
Dim strSelectRange As String ' Select Range Address
With ActiveSheet
lngLastRow = .Cells(.Rows.Count, cVntLRColumn).End(xlUp).Row
strSelectRange = .Range(.Cells(cLngFirstRow, cVntCriteria), _
.Cells(lngLastRow, cVntCriteria)).Address
.Range(strSelectRange).Replace What:="*", Replacement:=cStrReplace, _
LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False
End With
End Sub
在不使用对象变量的情况下使用工作表的有趣方式:
Sub SRSheet()
Const cStrSheet As Variant = "Sheet1" ' Worksheet Name/Index
With ThisWorkbook.Worksheets(cStrSheet)
End With
End Sub