3

我有一个带有模板数据的 Excel 表。我希望能够A1:A5用我B1:B5在文档的其余部分输入的内容替换出现的单词。

现在我要离开这个(而且我不太了解VBA):

Sub UpdatePartial()
With ActiveSheet.UsedRange
.Replace "ZIPCODE", "B1", xlPart
.Replace "NAME", "B2", xlPart
.Replace "CITY", "B3", xlPart
.Replace "STATE", "B4", xlPart
.Replace "PHONE", "B5", xlPart
End With
End Sub

但我想用As 的内容B1-B5代替文字"B1","B2", ..., "B5"

4

2 回答 2

1

而不是

.Replace "ZIPCODE", "B1", xlPart
采用
.Replace [a1].Value, [b1].Value, xlPart

如果您只想替换单元格内的整个单词(即,如果您想用dog替换cat但避免将catscan更改为dogscan ,则在字符串之前或之后构建空格检查)

采用
.Replace " " & [a1].Value, " " & [b1].Value, xlPart
.Replace [a1].Value & " ", [b1].Value & " ", xlPart

您更新的问题

Sub UpdatePartial()
    Dim rng1 As Range
    ActiveSheet.UsedRange
    Set rng1 = Intersect(ActiveSheet.UsedRange, Range(Cells(6, "a"), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count)))
    With rng1
        .Replace [a1].Value, [b1].Value, xlPart
    End With
End Sub
于 2012-11-17T01:20:21.987 回答
1

您将能够引用单元格(地址)值

Range("B1").value
于 2012-11-17T01:22:16.777 回答