Excel VBA选择.replace,如果替换了,则在被替换行的A列中放置文本。

4

我有一些宏,例如:

Columns("F:M").Select
Selection.Replace What:=",", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
    ReplaceFormat:=False

但是我希望将当前日期(或只是一串文本)放在发生替换的行的单元格A中。

1个回答

4
我想你需要将替换操作改为查找和替换。类似这样的操作:
Dim c As Range
Columns("F:M").Select
Set c = Selection.Find(What:=",", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False)
If Not c Is Nothing Then
    Do
        c.Replace What:=",", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
            ReplaceFormat:=False
        Cells(c.Row, 1).Value = Date
        Set c = Selection.FindNext(c)
    Loop While Not c Is Nothing
End If

在2003版本中添加了+1 SearchFormatReplaceFormat,但我认为这两个参数可以安全地删除适用于早期版本的Excel。 - Fionnuala
@Remou,我实际上在Excel2000上进行了测试,没有它们,只是因为它们存在于问题中才添加了它们。所以,是的,它可以工作^_^ - jswolf19

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接