首页 文章

Excel宏:设置单元格的公式

提问于
浏览
2

我'm writing a macro to insert a row into all selected sheets and then set some of the values to equal the values in another sheet. I' ve设法使用以下代码插入行,但我只需输入'm getting stuck trying to set the values. Without a macro, I'输入是工作簿中第一个工作表的名称 .

Sub InsertRows()
'
' InsertRows Macro
' Inserts rows into all selected sheets at the same position
'

    Dim CurrentSheet As Object

    ' Loop through all selected sheets.
    For Each CurrentSheet In ActiveWindow.SelectedSheets
        ' Insert 1 row at row 7 of each sheet.
        CurrentSheet.Range("a7:a7").EntireRow.Insert
        CurrentSheet.Range("c7").Value =Input!C7 'this is not working
    Next CurrentSheet
End Sub

2 回答

  • 2

    如果您只想要名为“输入”的工作表中的值,则可以执行以下操作:

    CurrentSheet.Range("C7").Value = Sheets("Input").Range("C7").Value
    

    如果你想要公式“=输入!C7”你可以这样做:

    CurrentSheet.Range("C7").Formula = "=Input!C7"
    
  • 4

    您需要使用 Formula 而不是 Value 属性,并且应引用文本:

    CurrentSheet.Range("c7").Formula "=Input!C7"
    

相关问题