Excel宏:设置单元格的公式

时间:2013-06-13 18:03:52

标签: excel excel-vba vba

我正在编写一个宏来在所有选定的工作表中插入一行,然后将某些值设置为等于另一个工作表中的值。我已经设法使用以下代码插入行,但是我试图设置值时遇到困难。如果没有宏,我只需输入=InputC7输入作为工作簿中第一张工作表的名称。

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 个答案:

答案 0 :(得分:4)

如果您只想要名为“输入”的工作表中的值,您可以这样做:

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

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

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

答案 1 :(得分:2)

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

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