Excelmacros:设置单元格的公式

我正在写一个macros,将行插入到所有选定的工作表中,然后将其中一些值设置为等于另一个工作表中的值。 我已经设法插入使用下面的代码行,但我卡住试图设置值。 没有macros,我只需input=InputC7input是工作簿中第一个工作表的名称。

 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 

如果您只需要名为“Input”的表格中的值,可以这样做:

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

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

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

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

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