代码仅在活动工作表上有效



所以我试图使用以下代码来填写工作表上的空白。它根据空白行上方的第一个值填充空白。

当我在特定的工作表上时,代码工作正常。然而,当我尝试从不同的表的代码不工作。它将保留某些行为空并给出以下错误:"未发现细胞">

这很奇怪,因为当我在特定的工作表上时它不会返回任何错误

Sub Sample()

Dim wsOutput As Worksheet
Set wsOutput = ThisWorkbook.Sheets("Output")

With wsOutput
With .Range("A2:F" & Range("G" & Rows.Count).End(xlUp).Row)
.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
End With

End Sub

我建议您充分限定您的对象

Sub Sample()

Dim wsOutput As Worksheet
Set wsOutput = ThisWorkbook.Sheets("Output")

With wsOutput.Range("A2:F" & wsOutput.Range("G" & wsOutput.Rows.Count).End(xlUp).Row)
.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With

End Sub

相关内容

最新更新