用于替换单元格值并保持格式化的VBA脚本

问题描述 投票:0回答:1

我有下面的表格,我正在尝试编写一个脚本,用不同的客户付款替换下面单元格的内容(即用2,000英镑替换1,100英镑)。下面是我的脚本片段,但是当我写回单元格时,它会丢失所有格式和编号列表。

如何使用非常相似的数据替换单元格数据并仍保留格式?

PS。我已经简化了单元格的内容以便于阅读,因此代码不会完全适用于该内容

Before After

DescPlan = Trim(t1.Cell(2, 2).Range.Text)
DescTest = InStr(1, DescPlan, ":")
finalString = Left(DescPlan, DescTest)

t1.Cell(2, 2).Range.Text = Replace(DescPlan, finalString, "Payment by the customer of " + Format(v, "Currency") + " will be due upon completion of items below:")
excel vba excel-vba ms-word word-vba
1个回答
0
投票

不确定这是否有帮助,但你正在使用一个表,所以适用于excel也适用于你。

Sub replace_keep_format()

Dim t1 As Range
Dim sStrng As String, rStrng As String
Dim i As Integer

sStrng = "£1,100"
rStrng = "£2,000"
i = 1

Do Until ThisWorkbook.Sheets(1).Range("a" & i) = ""
Set t1 = ThisWorkbook.Sheets(1).Range("a" & i)
t1 = Replace(Expression:=t1, Find:=sStrng, Replace:=rStrng)
i = i + 1
Loop

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.