简体   繁体   English

查找并替换VBA中的特定范围

[英]find and replace for specific range in VBA

I am using the following code for finding the curr_symbol and replacing it with new_symbol. 我正在使用以下代码来查找curr_symbol并将其替换为new_symbol。 Even though I have specified in the code that it should do so within the specific row numbers, it applies the find and replace to all the rows in the selected sheet, which is not what I want. 即使我在代码中指定了应该在特定的行号内执行此操作,它也会将查找和替换应用于所选工作表中的所有行,这不是我想要的。

Sub find_replace()
Dim curr_symbol, new_symbol As String
Dim row_num, last_row As Integer
row_num = ActiveSheet.Cells(1, "A").Value 'row_num=9
last_row = ActiveSheet.Cells(2, "A").Value 'last_row=11

Do While row_num < last_row
      curr_symbol = ".ABC130301"
    new_symbol = ActiveSheet.Cells(row_num, "E").Value 'new_symbol=".BAC130306"
    With ActiveSheet.UsedRange
        .Replace curr_symbol, new_symbol, xlPart
    End With
row_num = row_num + 1
Loop
End Sub

I even tried replacing the With ActiveSheet.UsedRange....End With with the following statement, but that also replaces the curr_symbol in all the rows. 我什至尝试With ActiveSheet.UsedRange....End With以下语句替换With ActiveSheet.UsedRange....End With ,但这也替换了所有行中的curr_symbol。

ActiveSheet.Cells.Replace What:=curr_symbol, Replacement:=new_symbol, LookAt:=xlPart, _
                              SearchOrder:=xlByRows, MatchCase:=False, _
                              SearchFormat:=False, ReplaceFormat:=False

How can I fix this so it only replaces the string in the specified rows. 如何解决此问题,使其仅替换指定行中的字符串。

I'm assuming the range you're interested in replacing is column A, from row_num to last_row . 我假设您有兴趣替换的范围是A列,从row_numlast_row Try replacing this: 尝试替换此:

With ActiveSheet.UsedRange
    .Replace curr_symbol, new_symbol, xlPart
End With

With this: 有了这个:

Cells(row_num, 1).Replace curr_symbol, new_symbol, xlPart

No matter where you place ActiveSheet.UsedRange it always refers to the entire used area of the active sheet. 无论您将ActiveSheet.UsedRange放在何处,它都始终引用活动工作表的整个已使用区域。 It is a collection containing all used cells and other cells in the same rows and columns as the used cells. 它是一个包含所有使用的单元格以及与使用的单元格相同的行和列中的其他单元格的集合。 It's not contextual in the way you've used it (ie it doesn't refer to the range relevant to your loop). 它与您使用它的方式无关(即,它不引用与您的循环相关的范围)。 The Replace method will be applied to every individual cell in the UsedRange . Replace方法将应用于UsedRange中的每个单个单元格。

Similarly with ActiveSheet.Cells ; ActiveSheet.Cells类似; this always refers to the collection of every cell in the active sheet. 这始终是指活动工作表中每个单元格的集合。 My suggestion is the same as ActiveSheet.Cells(row_num, 1) , specifying an individual cell to apply the Replace method to. 我的建议与ActiveSheet.Cells(row_num, 1) ,它指定一个单独的单元格来应用Replace方法。

No longer answering your question: 不再回答您的问题:

As an aside, the following line: 顺便说一句,以下行:

Dim curr_symbol, new_symbol As String

declares curr_symbol as a Variant, and new_symbol as a String. 将curr_symbol声明为Variant,将new_symbol声明为String。 The same applies for your next line of declarations (the Integers). 下一行声明(整数)也是如此。 No, I don't like this declaration behaviour either. 不,我也不喜欢这种声明行为。 You have to specify a type for every variable. 您必须为每个变量指定一种类型。 Here's how I tend to declare: 我倾向于这样声明:

Dim curr_symbol As String, _
    new_symbol As String

Additionally, Excel internally stores all Integer types as Long types internally. 此外,Excel在内部将所有Integer类型存储为Long类型。 Declaring as an Integer only restricts their values, not the amount of memory they consume. 声明为Integer仅限制其值,而不限制其消耗的内存量。 Unless you specifically want to limit the value of your integers, I recommend changing all Integer declarations to Long: 除非您特别想限制整数的值,否则建议将所有Integer声明都更改为Long:

Dim last_row As Integer

Is just as memory consuming as: 就像消耗内存一样:

Dim row_num as Long

But probably a little slower, if there's any speed difference at all. 但是,如果完全没有速度差异,可能会稍微慢一些。

The "UsedRange" range that you are using is the entire spreadsheet that you have used. 您正在使用的“ UsedRange”范围是所使用的整个电子表格。 Your replace is being applied to your entire spreadsheet. 您的替换将应用于整个电子表格。

I would create a range that you want to do the replace in like: 我将创建一个您要替换的范围,例如:

Dim MyRange as Range
Set MyRange = Range("B7:C9")

Then I would do the replace on this range. 然后,我将在此范围内进行替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM