简体   繁体   English

VBA Excel查找范围内的单词并替换

[英]VBA Excel find words in a range and replace

I want to replace words in a column with another word, however my code does not seem to be working, I'm still trying to get a grip on the coding language, but it's just not intuitive to me. 我想用另一个单词替换列中的单词,但是我的代码似乎不起作用,我仍然试图控制编码语言,但这对我来说并不直观。

Situation: 情况:

Global Macro - Equities
Global Macro - Bonds
Global Macro - FX
.
.
.
And so on...

Desired outcome: 期望的结果:

GM - Equities
GM - Bonds
GM - FX
.
.
.

My code 我的代码

Sub ReplaceFundNames()

    Dim FundName As String
    Dim CorrectedName As String
    Dim ws As Worksheet
    Dim LastRow As Long

    Set ws = ActiveSheet
    LastRow = Range("B" & Rows.Count).End(xlDown).Row

    FundName = Range(LastRow)
    CorrectedName = Replace(FundName, "Global Macro", "GM")

    Range("B" & LastRow).Value = CorrectedName


End Sub

Thank you! 谢谢!

The Replace function in VBA is designed to work on a single string, whereas the Range.Replace method is designed to work on a Range . VBA中的Replace函数用于处理单个字符串,而Range.Replace方法用于处理Range

As such, you can perform your replacements in a single statement: 因此,您可以在一个语句中执行替换:

ActiveSheet.Range("B:B").Replace "Global Macro", "GM"

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

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