我的vba代码似乎不起作用,即使没有错误

时间:2018-06-08 23:07:36

标签: excel vba

我试图遍历指定范围内的所有单元格,并替换单元格中函数的两个不同部分。

然而,代码似乎运行,当我放入一个msgbox LastCol时,它返回行中的最后一个单元格,但该函数不会更改。

代码:

Sub Function1()

 Dim LastCol As Integer
 Dim i As Integer

    Application.ScreenUpdating = False
     Application.DisplayAlerts = False
    With ActiveSheet
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column


    For i = 1 To LastCol

     ActiveWorkbook.Sheets("Data").Range("D3" & i).Replace "4. Apr", "5. May", xlPart
     ActiveWorkbook.Sheets("Data").Range("D3" & i).Replace "April", "May", xlPart


    Next i


End With

Application.ScreenUpdating = True
 Application.DisplayAlerts = True

1 个答案:

答案 0 :(得分:1)

如果我是1,那么Range(" D3"& i)指的是D31。尝试,

 with ActiveWorkbook.Sheets("Data")
     .Range(.cells(3, "D"), .cells(3, LastCol)).Replace What:="4. Apr", replacement:="5. May", LookAt:=xlPart, MatchCase:=false
     .Range(.cells(3, "D"), .cells(3, LastCol)).Replace What:="April", replacement:="May", LookAt:=xlPart, MatchCase:=false
 end with

tbh,它完全不清楚你指定的范围'。