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

时间:2021-02-10 20:54:17

标签: excel vba

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

当我在特定工作表上时,代码工作正常。但是,当我尝试从不同的工作表中获取代码时,它不起作用。它会将某些行留空并给出以下错误: “没有找到细胞”

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

     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

1 个答案:

答案 0 :(得分:1)

我建议你完全限定你的对象

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
相关问题