宏可以在所有工作簿中使用但只有一个

时间:2015-01-21 19:00:15

标签: vba excel-vba compiler-errors excel

我有一个已经工作的替换宏!它只是停止工作!我得到了

Compile error:

Wrong number of arguments or invalid property assignment

我制作了运行Macro的工作表的工作簿并将其保存为xlsm文件,然后我将我的代码放在一个模块中并在新工作簿中尝试它并且它有效!

但它在原始工作簿中不起作用。

这对我原来的工作簿有什么看法?我担心,我无法理解我可能做错了什么。

感谢您的任何见解

Sub iReplace()
Dim ws As Worksheet
Dim rngCell As Range, rng As Range
Dim LastRow As Long

Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
    LastRow = ws.Range("A" & .Rows.Count).End(xlUp).Row

Set rng = ws.Range("A2:A" & LastRow)

 If Not rng Is Nothing Then
   For Each rngCell In rng
      rngCell.Value = Replace(rngCell, " ", "  ", xlWhole)
   Next
  End If
  End With
End Sub

此外,这可以在原始工作簿中使用,也可以在与无效工作簿相同的工作表上使用,它们非常相似

Sub Replace2()
Dim ws As Worksheet
Dim rngCell As Range, rng As Range
Dim LR As Long

Set ws = ActiveWorkbook.Sheets("Sheet1")
  With ws
      LR = ws.Range("A" & .Rows.Count).End(xlUp).Row
      Set rng = ws.Range("A2:B" & LR)

      If Not rng Is Nothing Then
        For Each rngCell In rng
           rngCell.Replace " ", "  ", xlPart
        Next rngCell
      End If
 End With
End Sub

0 个答案:

没有答案